# Create needed AWS resources for CDK environment:
$ cdk bootstrap
# Project (app) is created using cli:
$ cdk init <app|lib|sample-app> --language=<language>
$ cdk init app --language=typescript
$ cdk init --list
Available templates:
* app: Template for a CDK Application
└─ cdk init app --language=[csharp|fsharp|go|java|javascript|python|typescript]
* lib: Template for a CDK Construct Library
└─ cdk init lib --language=typescript
* sample-app: Example CDK Application with some constructs
└─ cdk init sample-app --language=[csharp|fsharp|java|javascript|python|typescript]
# Compile typescript to js
$ npm run build
# Watch for changes and compile
$ npm run watch
# Perform the jest unit tests
$ npm run test
# Creates the cdk.out directory and generate CloudFormation template (in JSON), the assets, other meta data
$ cdk synth
# Generate CloudFormation in YAML format:
$ cdk synth --no-staging > cdk.out/Stack.template.yaml
# Templates are launched by CloudFormation or CDK stacks are deployed:
$ cdk deploy
# using a specific profile
$ cdk deploy --profile PROFILE_NAME
# deploy a specific stack
$ cdk deploy STACK_NAME
# deploy multiple stacks
$ cdk deploy STACK_1 STACK_2
# deploy all stackes in the app
$ cdk deploy "*"
# List the stacks in the CDK app:
$ cdk list
# Delete the stack:
$ cdk destroy
# Compare local and deployed stack:
$ cdk diff
Check for problem:
$ cdk doctor
# Open https://docs.aws.amazon.com/cdk/api/latest/
$ cdk docs
Documentation
- https://docs.aws.amazon.com/cdk/latest/guide/stack_how_to_create_multiple_stacks.html
- https://docs.aws.amazon.com/cdk/latest/guide/get_context_var.html
- https://docs.aws.amazon.com/cdk/latest/guide/testing.html
Best Practices
Examples
github.com/kolomied/awesome-cdk
- github.com/cloudcomponents/cdk-constructs/tree/master/examples
- github.com/aws-samples/aws-cdk-examples
- github.com/blndspt/aws-full-stack-template
- github.com/awslabs/aws-full-stack-template
- github.com/aws-samples/aws-bookstore-demo-app
- bobbyhadz.com/blog/how-does-aws-cdk-work
- bobbyhadz.com/blog/cdk-constructs-tutorial
- bobbyhadz.com/blog/aws-cdk-identifiers
- bobbyhadz.com/blog/how-to-use-context-aws-cdk
- bobbyhadz.com/blog/aws-cdk-parameters-example
- bobbyhadz.com/blog/aws-cdk-vpc-example
- bobbyhadz.com/blog/aws-cdk-security-group-example
Some Gotchas
When synthesizing an AWS CDK stack, I get the message --app is required either in command-line, in cdk.json or in ~/.cdk.json
This message usually means that you aren’t in the main directory of your AWS CDK project when you issue cdk synth
. The file cdk.json
in this directory, created by the cdk init
command, contains the command line needed to run (and thereby synthesize) your AWS CDK app. For a TypeScript app, for example, the default cdk.json
looks something like this:
{
"app": "npx ts-node bin/my-cdk-app.ts"
}
Also see: docs.aws.amazon.com/cdk/latest/guide/troubleshooting.html#troubleshooting_app_required