Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new-ex): custom logical names #120

Merged
merged 12 commits into from
Jan 8, 2020
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ $ cdk destroy
| [stepfunctions-job-poller](https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/stepfunctions-job-poller/) | A simple StepFunctions workflow |
| [ecs-service-with-logging](https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/ecs/ecs-service-with-logging/) | Starting a container fronted by a load balancer on ECS |
| [fargate-service-with-logging](https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/ecs/fargate-service-with-logging/) | Starting a container fronted by a load balancer on Fargate |
| [custom-logical-names](https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/custom-logical-names/) | Example of how to override logical name allocation |

## Java examples <a name="Java"></a>

Expand Down
52 changes: 52 additions & 0 deletions typescript/custom-logical-names/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Custom Logical Names

This sample shows how you can override the behavior for allocating
logical names for CloudFormation resources in the CDK.

It implements a feature that allows users to specify a prefix for
all logical names using the `prefix` context key.

## Usage

1. Extend your stacks from `BaseStack` instead of from `Stack`.
2. Specify context when calling the CLI through `--context prefix=PREFIX`.

## Implementation

The `BaseStack` class implements this custom behavior. Using a base stack is a a
common and recommended pattern for reusing policy within an organization.

Then, any stack that derives from `BaseStack` will automatically have this
behavior.

## Example

```shell
$ cdk synth -j
{
"Resources": {
"MyTopic86869434": {
"Type": "AWS::SNS::Topic"
},
"MyBucketF68F3FF0": {
"Type": "AWS::S3::Bucket"
}
}
}
```

Now with prefix:

```shell
$ cdk synth -j -c prefix="MyTeam"
{
"Resources": {
"MyTeamMyTopic86869434": {
"Type": "AWS::SNS::Topic"
},
"MyTeamMyBucketF68F3FF0": {
"Type": "AWS::S3::Bucket"
}
}
}
```
16 changes: 16 additions & 0 deletions typescript/custom-logical-names/base-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Stack, CfnElement } from '@aws-cdk/core';

/**
* A base stack class that implements custom logical name
* allocation. Adds a prefix if it is defined in the "prefix"
* context key.
*
* Use `cdk --context prefix=PREFIX` to set the prefix.
*/
export class BaseStack extends Stack {
public allocateLogicalId(element: CfnElement) {
const orig = super.allocateLogicalId(element);
const prefix = this.node.tryGetContext('prefix');
return prefix ? prefix + orig : orig;
}
}
3 changes: 3 additions & 0 deletions typescript/custom-logical-names/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "npx ts-node index.ts"
}
18 changes: 18 additions & 0 deletions typescript/custom-logical-names/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env node
import { BaseStack } from './base-stack';
import { App, Construct, StackProps } from '@aws-cdk/core';
import s3 = require('@aws-cdk/aws-s3');
import sns = require('@aws-cdk/aws-sns');

class MyStack extends BaseStack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

new sns.Topic(this, 'MyTopic');
new s3.Bucket(this, 'MyBucket');
}
}

const app = new App();
new MyStack(app, 'MyStack');
app.synth();
23 changes: 23 additions & 0 deletions typescript/custom-logical-names/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "custom-logical-names",
"version": "0.1.0",
"bin": {
"custom-logical-names": "bin/custom-logical-names.js"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"cdk": "cdk"
},
"devDependencies": {
"aws-cdk": "^1.7.0",
"ts-node": "^8.1.0",
"typescript": "~3.6.2"
},
"dependencies": {
"@aws-cdk/aws-s3": "^1.8.0",
"@aws-cdk/aws-sns": "^1.8.0",
"@aws-cdk/core": "^1.7.0",
"source-map-support": "^0.5.9"
}
}
23 changes: 23 additions & 0 deletions typescript/custom-logical-names/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target":"ES2018",
"module": "commonjs",
"lib": ["es2016", "es2017.object", "es2017.string"],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization":false,
"typeRoots": ["./node_modules/@types"]
},
"exclude": ["cdk.out"]
}
2 changes: 1 addition & 1 deletion typescript/ecs/fargate-load-balanced-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BonjourFargate extends cdk.Stack {
const cluster = new ecs.Cluster(this, 'Cluster', { vpc });

// Instantiate Fargate Service with just cluster and image
const fargateService = new ecs_patterns.LoadBalancedFargateService(this, "FargateService", {
const fargateService = new ecs_patterns.ApplicationLoadBalancedFargateService(this, "FargateService", {
cluster,
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
});
Expand Down
2 changes: 1 addition & 1 deletion typescript/ecs/fargate-service-with-auto-scaling/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AutoScalingFargateService extends cdk.Stack {
const cluster = new ecs.Cluster(this, 'fargate-service-autoscaling', { vpc });

// Create Fargate Service
const fargateService = new ecs_patterns.LoadBalancedFargateService(this, 'sample-app', {
const fargateService = new ecs_patterns.ApplicationLoadBalancedFargateService(this, 'sample-app', {
cluster,
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample")
});
Expand Down