What is aws-cdk?
The AWS Cloud Development Kit (AWS CDK) is an open-source software development framework to define cloud infrastructure in code and provision it through AWS CloudFormation. It provides high-level components called constructs that preconfigure cloud resources with proven defaults, so you can build cloud applications without needing to be an expert in AWS services.
What are aws-cdk's main functionalities?
Defining Infrastructure
This feature allows you to define AWS infrastructure using familiar programming languages. The code sample demonstrates how to define a new VPC using the AWS CDK.
const vpc = new ec2.Vpc(this, 'VPC');
Creating a Lambda Function
With AWS CDK, you can create serverless functions such as AWS Lambda. The code sample shows how to define a Lambda function with the Node.js 12.x runtime.
const lambdaFunction = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda')
});
Hosting a Static Website
AWS CDK can be used to host static websites on Amazon S3. The code sample illustrates how to create an S3 bucket configured for website hosting and how to deploy website content to it.
const bucket = new s3.Bucket(this, 'WebsiteBucket', {
websiteIndexDocument: 'index.html'
});
new s3deploy.BucketDeployment(this, 'DeployWebsite', {
sources: [s3deploy.Source.asset('./website-dist')],
destinationBucket: bucket
});
Setting up an API Gateway
This feature enables the creation of Amazon API Gateway endpoints. The code sample shows how to set up a REST API with a GET method integrated with a Lambda function.
const api = new apigateway.RestApi(this, 'MyApi', {
restApiName: 'Service'
});
const getLambdaIntegration = new apigateway.LambdaIntegration(getLambda);
api.root.addMethod('GET', getLambdaIntegration);
Other packages similar to aws-cdk
serverless
The Serverless Framework is similar to AWS CDK in that it allows you to define and deploy cloud infrastructure using code. It supports multiple cloud providers and is focused on building serverless applications. It uses a configuration file approach rather than an object-oriented programming model.
terraform
Terraform by HashiCorp is an infrastructure as code tool that allows you to define both cloud and on-premises resources with a declarative configuration language. It is cloud-agnostic and supports multiple providers, whereas AWS CDK is focused on AWS services.
pulumi
Pulumi is an infrastructure as code tool that allows you to define infrastructure using general-purpose programming languages. Similar to AWS CDK, Pulumi provides an object-oriented approach but supports multiple cloud providers, not just AWS.
AWS CDK Toolkit
The AWS CDK Toolkit provides the cdk
command-line interface that can be used to work with AWS CDK applications.
Command | Description |
---|
cdk docs | Access the online documentation |
cdk init | Start a new CDK project (app or library) |
cdk list | List stacks in an application |
cdk synth | Synthesize a CDK app to CloudFormation template(s) |
cdk diff | Diff stacks against current state |
cdk deploy | Deploy a stack into an AWS account |
cdk destroy | Deletes a stack from an AWS account |
cdk bootstrap | Deploy a toolkit stack to support deploying large stacks & artifacts |
cdk doctor | Inspect the environment and produce information useful for troubleshooting |
This module is part of the AWS Cloud Development Kit project.
Commands
cdk docs
Outputs the URL to the documentation for the current toolkit version, and attempts to open a browser to that URL.
$
$ cdk docs
https://docs.aws.amazon.com/cdk/api/latest/
$
$ cdk docs --browser='chrome %u'
https://docs.aws.amazon.com/cdk/api/latest/
cdk init
Creates a new CDK project.
$
$ cdk init --list
Available templates:
* app: Template for a CDK Application
ββ cdk init app --language=[java|typescript]
* lib: Template for a CDK Construct Library
ββ cdk init lib --language=typescript
$
$ cdk init lib --language=typescript
cdk list
Lists the stacks modeled in the CDK app.
$
$ cdk list --app='node bin/main.js'
Foo
Bar
Baz
$
$ cdk list --app='node bin/main.js' --long
-
name: Foo
environment:
name: 000000000000/bermuda-triangle-1
account: '000000000000'
region: bermuda-triangle-1
-
name: Bar
environment:
name: 111111111111/bermuda-triangle-2
account: '111111111111'
region: bermuda-triangle-2
-
name: Baz
environment:
name: 333333333333/bermuda-triangle-3
account: '333333333333'
region: bermuda-triangle-3
cdk synthesize
Synthesize the CDK app and outputs CloudFormation templates. If the application contains multiple stacks and no
stack name is provided in the command-line arguments, the --output
option is mandatory and a CloudFormation template
will be generated in the output folder for each stack.
By default, templates are generated in YAML format. The --json
option can be used to switch to JSON.
$
$ cdk synthesize --app='node bin/main.js' MyStackName
$
$ cdk synth --app='node bin/main.js' MyStackName --output=template.yml
$
$ cdk synth --app='node bin/main.js' --output=templates
cdk diff
Computes differences between the infrastructure specified in the current state of the CDK app and the currently
deployed application (or a user-specified CloudFormation template). This command returns non-zero if any differences are
found.
$
$ cdk diff --app='node bin/main.js' MyStackName
$
$ cdk diff --app='node bin/main.js' MyStackName --template=path/to/template.yml
cdk deploy
Deploys a stack of your CDK app to it's environment. During the deployment, the toolkit will output progress
indications, similar to what can be observed in the AWS CloudFormation Console. If the environment was never
bootstrapped (using cdk bootstrap
), only stacks that are not using assets and synthesize to a template that is under
51,200 bytes will successfully deploy.
$ cdk deploy --app='node bin/main.js' MyStackName
cdk destroy
Deletes a stack from it's environment. This will cause the resources in the stack to be destroyed (unless they were
configured with a DeletionPolicy
of Retain
). During the stack destruction, the command will output progress
information similar to what cdk deploy
provides.
$ cdk destroy --app='node bin/main.js' MyStackName
cdk bootstrap
Deploys a CDKToolkit
CloudFormation stack into the specified environment(s), that provides an S3 bucket that
cdk deploy
will use to store synthesized templates and the related assets, before triggering a CloudFormation stack
update. The name of the deployed stack can be configured using the --toolkit-stack-name
argument.
$
$ cdk bootstrap --app='node bin/main.js'
$
$ cdk bootstrap --app='node bin/main.js' foo bar
cdk doctor
Inspect the current command-line environment and configurations, and collect information that can be useful for
troubleshooting problems. It is usually a good idea to include the information provided by this command when submitting
a bug report.
$ cdk doctor
βΉοΈ CDK Version: 1.0.0 (build e64993a)
βΉοΈ AWS environment variables:
- AWS_EC2_METADATA_DISABLED = 1
- AWS_SDK_LOAD_CONFIG = 1
Configuration
On top of passing configuration through command-line arguments, it is possible to use JSON configuration files. The
configuration's order of precedence is:
- Command-line arguments
- Project configuration (
./cdk.json
) - User configuration (
~/.cdk.json
)
JSON Configuration files
Some of the interesting keys that can be used in the JSON configuration files:
{
"app": "node bin/main.js",
"context": {
"key": "value",
},
"toolkitStackName": "foo",
"toolkitBucketName": "fooBucket",
"versionReporting": false,
}
1.18.0 (2019-11-25)
General Availability of AWS CDK for .NET and Java!! πππ₯π₯πΎπΎ
We are excited to announce the general availability of support for the .NET family of languages (C#,
F#, ...) as well as Java!
We want to express our gratitude to all of our early customers as well as the amazing contributors
for all the help and support in making this release possible. Thank you for all the feedback
provided during the Developer Preview of .NET and Java support, without which the product would not
be what it is today.
Special thanks go out to a handful of amazing people who have provided instrumental support in
bringing .NET and Java support to this point:
Of course, we continue to be amazed and thrilled by the community contributions we received besides
language support. The passion demonstrated by the CDK community is heartwarming and largely
contributes to making maintaining the CDK an enjoyable, enriching experience!
Features
- lambda: node12.x, python3.8 and java11 runtimes (#5107) (e62f9fb)
- lambda: unlock the lambda environment variables restriction in China regions (#5122) (cc13009)
Bug Fixes
- init/chsarp: correct README for sample-app C# template (#5144) (b2031f6)
- init/sample-app: numerous fixes and additions to the sample-app init templates (#5119) (02c3b05), closes #5130 #5130
- init/java: add -e to mvn command so errors aren't hidden (#5129) (5427106), closes #5128
- init/csharp: .NET semantic fixes for init templates (#5154) (04a1b32)
Known Issues
The following known issues were identified that specifically affect .NET and Java support in the CDK,
and which will be promptly addressed in upcoming CDK releases (in no particular order). See the
GitHub issues for more information and workarounds where applicable.
- .NET and Java: [
aws/jsii#1011
] - abstract members are not marked as such on their .NET and Java representations - .NET: [
aws/jsii#1029
] - user-defined classes implementing CDK interfaces must extend Amazon.Jsii.Runtime.Deputy.DeputyBase
- .NET: [
aws/jsii#1042
] - Parameters typed object accept only primitive types, instances of CDK types, Dictionary<string,?>
- .NET: [
aws/jsii#1044
] - Unable to pass interface instance through in a Dictionary<string,object>
- Java: [
aws/jsii#1034
] - Implementing or overriding overloaded methods in Java does not work consistently - Java: [
aws/jsii#1035
] - Returning Lazy.anyValue
from an method whose return type is java.lang.Object
may result in Resolution Errors - Java: [
aws/jsii#1005
] - property getter implementations (e.g: from an interface) may be ignored