Skip to content

Commit c81fc6f

Browse files
authored
Merge pull request #1 from julicjung/main
Sample code contribution
2 parents 16c0038 + be61295 commit c81fc6f

File tree

21 files changed

+1407
-9
lines changed

21 files changed

+1407
-9
lines changed

.gitignore

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/client/client
2+
/server/server
3+
4+
5+
# Created by https://www.toptal.com/developers/gitignore/api/go,terraform,intellij+all,visualstudiocode,macos
6+
# Edit at https://www.toptal.com/developers/gitignore?templates=go,terraform,intellij+all,visualstudiocode,macos
7+
8+
### Go ###
9+
# If you prefer the allow list template instead of the deny list, see community template:
10+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
11+
#
12+
# Binaries for programs and plugins
13+
*.exe
14+
*.exe~
15+
*.dll
16+
*.so
17+
*.dylib
18+
19+
# Test binary, built with `go test -c`
20+
*.test
21+
22+
# Output of the go coverage tool, specifically when used with LiteIDE
23+
*.out
24+
25+
# Dependency directories (remove the comment below to include it)
26+
# vendor/
27+
28+
# Go workspace file
29+
go.work
30+
31+
### Go Patch ###
32+
/vendor/
33+
/Godeps/
34+
35+
### Intellij+all ###
36+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
37+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
38+
39+
# User-specific stuff
40+
.idea/**/workspace.xml
41+
.idea/**/tasks.xml
42+
.idea/**/usage.statistics.xml
43+
.idea/**/dictionaries
44+
.idea/**/shelf
45+
46+
# AWS User-specific
47+
.idea/**/aws.xml
48+
49+
# Generated files
50+
.idea/**/contentModel.xml
51+
52+
# Sensitive or high-churn files
53+
.idea/**/dataSources/
54+
.idea/**/dataSources.ids
55+
.idea/**/dataSources.local.xml
56+
.idea/**/sqlDataSources.xml
57+
.idea/**/dynamic.xml
58+
.idea/**/uiDesigner.xml
59+
.idea/**/dbnavigator.xml
60+
61+
# Gradle
62+
.idea/**/gradle.xml
63+
.idea/**/libraries
64+
65+
# Gradle and Maven with auto-import
66+
# When using Gradle or Maven with auto-import, you should exclude module files,
67+
# since they will be recreated, and may cause churn. Uncomment if using
68+
# auto-import.
69+
# .idea/artifacts
70+
# .idea/compiler.xml
71+
# .idea/jarRepositories.xml
72+
# .idea/modules.xml
73+
# .idea/*.iml
74+
# .idea/modules
75+
# *.iml
76+
# *.ipr
77+
78+
# CMake
79+
cmake-build-*/
80+
81+
# Mongo Explorer plugin
82+
.idea/**/mongoSettings.xml
83+
84+
# File-based project format
85+
*.iws
86+
87+
# IntelliJ
88+
out/
89+
90+
# mpeltonen/sbt-idea plugin
91+
.idea_modules/
92+
93+
# JIRA plugin
94+
atlassian-ide-plugin.xml
95+
96+
# Cursive Clojure plugin
97+
.idea/replstate.xml
98+
99+
# SonarLint plugin
100+
.idea/sonarlint/
101+
102+
# Crashlytics plugin (for Android Studio and IntelliJ)
103+
com_crashlytics_export_strings.xml
104+
crashlytics.properties
105+
crashlytics-build.properties
106+
fabric.properties
107+
108+
# Editor-based Rest Client
109+
.idea/httpRequests
110+
111+
# Android studio 3.1+ serialized cache file
112+
.idea/caches/build_file_checksums.ser
113+
114+
### Intellij+all Patch ###
115+
# Ignores the whole .idea folder and all .iml files
116+
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
117+
118+
.idea/*
119+
120+
# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
121+
122+
*.iml
123+
modules.xml
124+
.idea/misc.xml
125+
*.ipr
126+
127+
# Sonarlint plugin
128+
.idea/sonarlint
129+
130+
### macOS ###
131+
# General
132+
.DS_Store
133+
.AppleDouble
134+
.LSOverride
135+
136+
# Icon must end with two \r
137+
Icon
138+
139+
140+
# Thumbnails
141+
._*
142+
143+
# Files that might appear in the root of a volume
144+
.DocumentRevisions-V100
145+
.fseventsd
146+
.Spotlight-V100
147+
.TemporaryItems
148+
.Trashes
149+
.VolumeIcon.icns
150+
.com.apple.timemachine.donotpresent
151+
152+
# Directories potentially created on remote AFP share
153+
.AppleDB
154+
.AppleDesktop
155+
Network Trash Folder
156+
Temporary Items
157+
.apdisk
158+
159+
### Terraform ###
160+
# Local .terraform directories
161+
**/.terraform/*
162+
163+
# .tfstate files
164+
*.tfstate
165+
*.tfstate.*
166+
167+
# Crash log files
168+
crash.log
169+
crash.*.log
170+
171+
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
172+
# password, private keys, and other secrets. These should not be part of version
173+
# control as they are data points which are potentially sensitive and subject
174+
# to change depending on the environment.
175+
*.tfvars
176+
177+
# Ignore override files as they are usually used to override resources locally and so
178+
# are not checked in
179+
override.tf
180+
override.tf.json
181+
*_override.tf
182+
*_override.tf.json
183+
184+
# Include override files you do wish to add to version control using negated pattern
185+
# !example_override.tf
186+
187+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
188+
# example: *tfplan*
189+
190+
# Ignore CLI configuration files
191+
.terraformrc
192+
terraform.rc
193+
194+
### VisualStudioCode ###
195+
.vscode/*
196+
!.vscode/settings.json
197+
!.vscode/tasks.json
198+
!.vscode/launch.json
199+
!.vscode/extensions.json
200+
!.vscode/*.code-snippets
201+
202+
# Local History for Visual Studio Code
203+
.history/
204+
205+
# Built Visual Studio Code Extensions
206+
*.vsix
207+
208+
### VisualStudioCode Patch ###
209+
# Ignore all local history of files
210+
.history
211+
.ionide
212+
213+
# Support for Project snippet scope
214+
215+
# End of https://www.toptal.com/developers/gitignore/api/go,terraform,intellij+all,visualstudiocode,macos

README.md

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,73 @@
1-
## My Project
1+
# Using AWS Cloud Map as gRPC name resolver
22

3-
TODO: Fill this README out!
3+
This repository show how AWS Cloud Map can be used as a name resolver in gRPC clients. Using the AWS Cloud Map HTTP API instead of DNS based resolution allows to create custom client-side loadbalancing policies leveraging the metadata associated with registered instances of AWS Cloud Map services.
44

5-
Be sure to:
5+
Potential benefits of this approach are:
6+
* Cutting out one or more loadbalancers which would be fronting Fargate services. This reduces cost, removes a network hop and may reduce efforts to maintain the loadbalancer.
7+
* Options for advanced loadbalancing scenarios. This sample shows, as an example, how AWS Cloud Map metadata can be used to prefer calling service instances which reside in the same az as the client.
68

7-
* Change the title in this README
8-
* Edit your repository description on GitHub
9+
## Running the sample
910

10-
## Security
11+
Build the client and server:
1112

12-
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
13+
```bash
14+
#---
15+
cd client
16+
# generate gRPC code
17+
mkdir pb
18+
go generate
19+
# build executable
20+
GOOS=linux GOARCH=amd64 go build -o client .
1321

14-
## License
22+
#---
23+
cd server
24+
# generate gRPC code
25+
mkdir pb
26+
go generate
27+
# build executable
28+
GOOS=linux GOARCH=amd64 go build -o server .
29+
```
1530

16-
This library is licensed under the MIT-0 License. See the LICENSE file.
31+
Provision the CDK app. Make sure docker is running as the Docker images are built as CDK assets during CDK app execution.
1732

33+
Account and Region will be picked up during CDK app execution.
34+
35+
**Important**: Region **must** be eu-central-1 currently, as this region is used in the coding.
36+
37+
```bash
38+
cd cdk
39+
cdk deploy
40+
```
41+
42+
## Create sample data
43+
44+
After the CDK app is deployed, test drive with a couple of calls. It is best to use tools like `hey` or `ab` to provide a large enough number of calls to see the loadbalancing preference show up.
45+
46+
Obtain the Public IP of the single task of the client service from the ECS console
47+
48+
```bash
49+
# with curl
50+
curl http://<pubip>:8080/describe
51+
52+
# with hey, runs a total of 200 requests by default over 10s
53+
hey -c 2 -q 10 -m GET http://<pubip>:8080/describe
54+
# this will take a couple of seconds to run without giving an output
55+
```
56+
57+
## Observe custom loadbalancing policy
58+
59+
We route approx 50% of requests to the same availability zone (see balancer.go:53).
60+
61+
Use CloudWatch Insights for the log group and run the following query on log group `grpcdemo`:
62+
63+
```
64+
fields @timestamp, @message
65+
| limit 1000
66+
| filter @message like 'server response from az'
67+
| parse 'server response from az: *' as az
68+
| stats count(az) as az_calls by az
69+
```
70+
71+
**Be sure to allow a couple of minutes for all logsto arrive, otherwise the count will not be correct.**
72+
73+
You should see approx. 50% of calls going to the AZ in which the single task of service `client` is located in.

cdk/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.js
2+
!jest.config.js
3+
*.d.ts
4+
node_modules
5+
6+
# CDK asset staging directory
7+
.cdk.staging
8+
cdk.out
9+
10+
cdk.context.json

cdk/.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.ts
2+
!*.d.ts
3+
4+
# CDK asset staging directory
5+
.cdk.staging
6+
cdk.out

cdk/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Welcome to your CDK TypeScript project!
2+
3+
This is a blank project for TypeScript development with CDK.
4+
5+
The `cdk.json` file tells the CDK Toolkit how to execute your app.
6+
7+
## Useful commands
8+
9+
* `npm run build` compile typescript to js
10+
* `npm run watch` watch for changes and compile
11+
* `npm run test` perform the jest unit tests
12+
* `cdk deploy` deploy this stack to your default AWS account/region
13+
* `cdk diff` compare deployed stack with current state
14+
* `cdk synth` emits the synthesized CloudFormation template

cdk/bin/cdk.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env node
2+
/*
3+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
* SPDX-License-Identifier: MIT-0
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
7+
* software and associated documentation files (the "Software"), to deal in the Software
8+
* without restriction, including without limitation the rights to use, copy, modify,
9+
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
10+
* permit persons to whom the Software is furnished to do so.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
16+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
17+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
import 'source-map-support/register';
20+
import * as cdk from 'aws-cdk-lib';
21+
import { DemoStack } from '../lib/demo-stack';
22+
23+
const app = new cdk.App();
24+
new DemoStack(app, 'DemoStack', {
25+
/* If you don't specify 'env', this stack will be environment-agnostic.
26+
* Account/Region-dependent features and context lookups will not work,
27+
* but a single synthesized template can be deployed anywhere. */
28+
29+
/* Uncomment the next line to specialize this stack for the AWS Account
30+
* and Region that are implied by the current CLI configuration. */
31+
env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
32+
33+
/* Uncomment the next line if you know exactly what Account and Region you
34+
* want to deploy the stack to. */
35+
// env: { account: '123456789012', region: 'us-east-1' },
36+
37+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
38+
});

0 commit comments

Comments
 (0)