forked from aws-samples/aws-cdk-examples
-
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.
feat(new-ex): custom logical names (aws-samples#120)
* custom logical names an example for how to implement custom logical name allocation. related: aws/aws-cdk#4045 * Use `ApplicationLoadBalancedFargateService`
- Loading branch information
Showing
7 changed files
with
163 additions
and
0 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
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,79 @@ | ||
# Custom Logical Names | ||
<!--BEGIN STABILITY BANNER--> | ||
--- | ||
|
||
![Stability: Stable](https://img.shields.io/badge/stability-Stable-success.svg?style=for-the-badge) | ||
|
||
> **This is a stable example. It should successfully build out of the box** | ||
> | ||
> This examples does is built on Construct Libraries marked "Stable" and does not have any infrastructure prerequisites to build. | ||
--- | ||
<!--END STABILITY BANNER--> | ||
|
||
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. | ||
|
||
## Build | ||
|
||
To build this app, you need to be in this example's root folder. Then run the following: | ||
|
||
```bash | ||
npm install -g aws-cdk | ||
npm install | ||
npm run build | ||
``` | ||
|
||
This will install the necessary CDK, then this example's dependencies, and then build your TypeScript files and your CloudFormation template. | ||
|
||
## Deploy | ||
|
||
Run `cdk deploy --context prefix=PREFIX`. This will deploy / redeploy your Stack to your AWS Account. | ||
|
||
## 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" | ||
} | ||
} | ||
} | ||
``` |
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,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; | ||
} | ||
} |
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,3 @@ | ||
{ | ||
"app": "npx ts-node index.ts" | ||
} |
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 @@ | ||
#!/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(); |
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,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": "*", | ||
"ts-node": "^8.1.0", | ||
"typescript": "~3.7.2" | ||
}, | ||
"dependencies": { | ||
"@aws-cdk/aws-s3": "*", | ||
"@aws-cdk/aws-sns": "*", | ||
"@aws-cdk/core": "*", | ||
"source-map-support": "^0.5.9" | ||
} | ||
} |
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,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"] | ||
} |