-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
2,350 additions
and
3,270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,6 +263,7 @@ $RECYCLE.BIN/ | |
*.d.ts | ||
node_modules | ||
|
||
# CDK asset staging directory | ||
.cdk.staging | ||
cdk.out | ||
.env | ||
.env.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
*.ts | ||
!*.d.ts | ||
|
||
# CDK asset staging directory | ||
.cdk.staging | ||
cdk.out | ||
.env | ||
.env.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
"editor.formatOnSave": true, | ||
"[typescript]": { | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Jason Butz | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
# Welcome to your CDK TypeScript project | ||
# AWS Serverless Microservices Service Discovery Proof of Concept | ||
|
||
This is a blank project for CDK development with TypeScript. | ||
Proof of Concept showing how you can use AWS Cloud Map or AWS Systems Manager Parameter Store to maintian details on deployed services and use it to dynamically reference resources. | ||
|
||
The `cdk.json` file tells the CDK Toolkit how to execute your app. | ||
## Setup | ||
|
||
## Useful commands | ||
|
||
* `npm run build` compile typescript to js | ||
* `npm run watch` watch for changes and compile | ||
* `npm run test` perform the jest unit tests | ||
* `cdk deploy` deploy this stack to your default AWS account/region | ||
* `cdk diff` compare deployed stack with current state | ||
* `cdk synth` emits the synthesized CloudFormation template | ||
1. Ensure you have Node.js 18 installed on your machine | ||
2. Ensure you have the [AWS CLI](https://aws.amazon.com/cli/) installed on your machine | ||
3. Configure a terminal session with AWS programmatic credentials | ||
4. Install this repo's dependencies | ||
```bash | ||
npm ci | ||
``` | ||
5. Run the CDK's boostrap command to ensure you AWS account is configured correctly | ||
```bash | ||
npx cdk boostrap | ||
``` | ||
6. Deploy the AWS resources | ||
```bash | ||
npm run deploy | ||
``` | ||
7. In the output look for `ssm-poc-service.ParameterStorePOCUrl` and `cloudmap-poc-service.CloudMapPOCUrl`, these URLs will retrieve the URLs for our two mock microservices and call the API and return the output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,62 @@ | ||
#!/usr/bin/env node | ||
import 'source-map-support/register'; | ||
import * as cdk from 'aws-cdk-lib'; | ||
import { ProofOfConceptStack } from '../lib/poc-stack'; | ||
import 'source-map-support/register'; | ||
import { MockServiceStack } from '../lib/mock-app-stack'; | ||
import { CloudMapProofOfConceptStack } from '../lib/cloudmap-poc-stack'; | ||
import { SharedInfrastructureStack } from '../lib/shared-infrastructure'; | ||
import { ParameterStoreProofOfConceptStack } from '../lib/parameters-poc-stack'; | ||
|
||
const app = new cdk.App({}); | ||
|
||
const sharedInfrastructure = new SharedInfrastructureStack( | ||
app, | ||
'shared-infrastructure', | ||
{ | ||
namespace: 'poc-app', | ||
} | ||
); | ||
|
||
const app = new cdk.App(); | ||
new ProofOfConceptStack(app, 'PocServerlessMicroserviceDiscoveryStack', { | ||
/* If you don't specify 'env', this stack will be environment-agnostic. | ||
* Account/Region-dependent features and context lookups will not work, | ||
* but a single synthesized template can be deployed anywhere. */ | ||
const mockServiceOne = new MockServiceStack(app, 'service-one', { | ||
serviceName: 'service-one', | ||
sharedNamespace: { | ||
arn: sharedInfrastructure.namespace.namespaceArn, | ||
id: sharedInfrastructure.namespace.namespaceId, | ||
name: sharedInfrastructure.namespace.namespaceName, | ||
}, | ||
}); | ||
mockServiceOne.addDependency(sharedInfrastructure); | ||
|
||
/* Uncomment the next line to specialize this stack for the AWS Account | ||
* and Region that are implied by the current CLI configuration. */ | ||
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION }, | ||
const mockServiceTwo = new MockServiceStack(app, 'service-two', { | ||
serviceName: 'service-two', | ||
sharedNamespace: { | ||
arn: sharedInfrastructure.namespace.namespaceArn, | ||
id: sharedInfrastructure.namespace.namespaceId, | ||
name: sharedInfrastructure.namespace.namespaceName, | ||
}, | ||
}); | ||
mockServiceTwo.addDependency(sharedInfrastructure); | ||
|
||
/* Uncomment the next line if you know exactly what Account and Region you | ||
* want to deploy the stack to. */ | ||
// env: { account: '123456789012', region: 'us-east-1' }, | ||
const cloudMapPOCService = new CloudMapProofOfConceptStack( | ||
app, | ||
'cloudmap-poc-service', | ||
{ | ||
namespace: sharedInfrastructure.namespace.namespaceName, | ||
serviceOneName: mockServiceOne.serviceName, | ||
serviceTwoName: mockServiceTwo.serviceName, | ||
} | ||
); | ||
cloudMapPOCService.addDependency(sharedInfrastructure); | ||
cloudMapPOCService.addDependency(mockServiceOne); | ||
cloudMapPOCService.addDependency(mockServiceTwo); | ||
|
||
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */ | ||
}); | ||
const parameterStorePOCService = new ParameterStoreProofOfConceptStack( | ||
app, | ||
'ssm-poc-service', | ||
{ | ||
serviceOneName: mockServiceOne.serviceName, | ||
serviceTwoName: mockServiceTwo.serviceName, | ||
} | ||
); | ||
parameterStorePOCService.addDependency(sharedInfrastructure); | ||
parameterStorePOCService.addDependency(mockServiceOne); | ||
parameterStorePOCService.addDependency(mockServiceTwo); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { CfnOutput, Stack, StackProps } from 'aws-cdk-lib'; | ||
import { EndpointType, LambdaRestApi } from 'aws-cdk-lib/aws-apigateway'; | ||
import { Effect, Policy, PolicyStatement } from 'aws-cdk-lib/aws-iam'; | ||
import { Construct } from 'constructs'; | ||
import { InstrumentedNodeFunction } from './constructs/instrumented-node-function'; | ||
|
||
export type CloudMapProofOfConceptStackProps = StackProps & { | ||
namespace: string; | ||
serviceOneName: string; | ||
serviceTwoName: string; | ||
}; | ||
|
||
export class CloudMapProofOfConceptStack extends Stack { | ||
constructor( | ||
scope: Construct, | ||
id: string, | ||
props: CloudMapProofOfConceptStackProps | ||
) { | ||
super(scope, id, props); | ||
|
||
const lambda = new InstrumentedNodeFunction(this, 'poc-lambda', { | ||
environment: { | ||
service: 'service-discovery-poc', | ||
CLOUDMAP_NAMESPACE: props.namespace, | ||
CLOUDMAP_SERVICE_ONE_NAME: props.serviceOneName, | ||
CLOUDMAP_SERVICE_TWO_NAME: props.serviceTwoName, | ||
}, | ||
entry: 'src/cloudmap-poc-lambda.ts', | ||
}); | ||
|
||
lambda.role?.attachInlinePolicy( | ||
new Policy(this, 'service-discovery-policy', { | ||
statements: [ | ||
new PolicyStatement({ | ||
actions: ['servicediscovery:DiscoverInstances'], | ||
resources: ['*'], | ||
effect: Effect.ALLOW, | ||
}), | ||
], | ||
}) | ||
); | ||
|
||
const api = new LambdaRestApi(this, 'api', { | ||
restApiName: 'poc-api', | ||
handler: lambda, | ||
endpointConfiguration: { | ||
types: [EndpointType.REGIONAL], | ||
}, | ||
}); | ||
|
||
new CfnOutput(this, 'CloudMapPOCUrl', { | ||
value: api.url, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Stack } from 'aws-cdk-lib'; | ||
import { LayerVersion, Runtime, Tracing } from 'aws-cdk-lib/aws-lambda'; | ||
import { | ||
NodejsFunction, | ||
NodejsFunctionProps, | ||
} from 'aws-cdk-lib/aws-lambda-nodejs'; | ||
import { RetentionDays } from 'aws-cdk-lib/aws-logs'; | ||
import { Construct } from 'constructs'; | ||
|
||
export class InstrumentedNodeFunction extends NodejsFunction { | ||
constructor(scope: Construct, id: string, props: NodejsFunctionProps) { | ||
super(scope, id, { | ||
runtime: Runtime.NODEJS_18_X, | ||
tracing: Tracing.ACTIVE, | ||
bundling: { | ||
externalModules: [ | ||
'@aws-sdk/*', | ||
'@aws-lambda-powertools/commons', | ||
'@aws-lambda-powertools/logger', | ||
'@aws-lambda-powertools/metrics', | ||
'@aws-lambda-powertools/tracer', | ||
], | ||
}, | ||
logRetention: RetentionDays.ONE_DAY, | ||
...props, | ||
}); | ||
|
||
const powertoolsLayer = LayerVersion.fromLayerVersionArn( | ||
this, | ||
'PowertoolsLayer', | ||
`arn:aws:lambda:${ | ||
Stack.of(this).region | ||
}:094274105915:layer:AWSLambdaPowertoolsTypeScript:7` | ||
); | ||
|
||
this.addLayers(powertoolsLayer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
EndpointType, | ||
HttpIntegration, | ||
IRestApi, RestApi | ||
} from 'aws-cdk-lib/aws-apigateway'; | ||
import { Construct } from 'constructs'; | ||
|
||
export type MockMicroserviceProps = {}; | ||
|
||
|
||
export class MockMicroservice extends Construct { | ||
public apiGateway: IRestApi; | ||
constructor(scope: Construct, id: string, props?: MockMicroserviceProps) { | ||
super(scope, id); | ||
|
||
this.apiGateway = new RestApi(this, 'api', { | ||
endpointConfiguration: { | ||
types: [EndpointType.REGIONAL], | ||
}, | ||
deployOptions: { | ||
stageName: 'poc', | ||
}, | ||
}); | ||
|
||
this.apiGateway.root | ||
.addProxy({ | ||
anyMethod: false, | ||
}) | ||
.addMethod( | ||
'GET', | ||
new HttpIntegration('https://catfact.ninja/fact', { | ||
httpMethod: 'GET', | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { Stack, StackProps, Tags } from 'aws-cdk-lib'; | ||
import { | ||
DiscoveryType, | ||
HttpNamespace, | ||
Service, | ||
} from 'aws-cdk-lib/aws-servicediscovery'; | ||
import { StringParameter } from 'aws-cdk-lib/aws-ssm'; | ||
import { Construct } from 'constructs'; | ||
import { MockMicroservice } from './constructs/mock-microservice'; | ||
|
||
export type MockServiceProps = StackProps & { | ||
serviceName: string; | ||
sharedNamespace: { | ||
name: string; | ||
id: string; | ||
arn: string; | ||
}; | ||
}; | ||
|
||
export class MockServiceStack extends Stack { | ||
public readonly serviceName: string; | ||
private readonly service: MockMicroservice; | ||
appName = 'discovery-poc'; | ||
|
||
constructor(scope: Construct, id: string, props: MockServiceProps) { | ||
super(scope, id, props); | ||
this.serviceName = props.serviceName; | ||
this.service = new MockMicroservice(this, 'service'); | ||
|
||
this.setupCloudMap(props.sharedNamespace, props.serviceName); | ||
this.setupParameterStore(this.appName, props.serviceName); | ||
|
||
Tags.of(scope).add('app', this.appName); | ||
Tags.of(scope).add('app_service', props.serviceName); | ||
} | ||
|
||
private getServiceInfo() { | ||
return { | ||
type: 'AWS::ApiGateway::RestApi', | ||
arn: Stack.of(this).formatArn({ | ||
service: 'apigateway', | ||
account: '', | ||
resource: 'restapis', | ||
resourceName: this.service.apiGateway.restApiId, | ||
}), | ||
url: `https://${this.service.apiGateway.restApiId}.execute-api.${ | ||
Stack.of(this).region | ||
}.${Stack.of(this).urlSuffix}/${ | ||
this.service.apiGateway.deploymentStage.stageName | ||
}`, | ||
}; | ||
} | ||
|
||
private setupCloudMap( | ||
sharedNamespace: { | ||
name: string; | ||
id: string; | ||
arn: string; | ||
}, | ||
serviceName: string | ||
) { | ||
// Cloud Map Specific Implementation | ||
const namespace = HttpNamespace.fromHttpNamespaceAttributes( | ||
this, | ||
'namespace', | ||
{ | ||
namespaceArn: sharedNamespace.arn, | ||
namespaceId: sharedNamespace.id, | ||
namespaceName: sharedNamespace.name, | ||
} | ||
); | ||
const serviceRegistration = new Service(this, 'service-registration', { | ||
namespace, | ||
name: serviceName, | ||
discoveryType: DiscoveryType.API, | ||
}); | ||
|
||
serviceRegistration.registerNonIpInstance('service-registration-instance', { | ||
customAttributes: this.getServiceInfo(), | ||
}); | ||
} | ||
|
||
private setupParameterStore(appName: string, serviceName: string) { | ||
new StringParameter(this, 'url-parameter', { | ||
stringValue: this.getServiceInfo().url, | ||
parameterName: `/${appName}/${serviceName}`, | ||
}); | ||
} | ||
} |
Oops, something went wrong.