(Illustration: In those days, integrated circuits (IC) organized some commonly used and reused circuits, and then reduced the size, standing on the shoulders of predecessors, and driving the progress of the times. The AWS CDK has done a similar sorting. Image source: Image by Gerd Altmann from Pixabay。)
Briefing
The AWS Cloud Development Kit (AWS CDK) allows us to define our own cloud infrastructure as code (IaC) in a programming language we are familiar with (currently 5 supported).
AWS CDK is composed of various large and small constructs to create stacks and apps.
The neighbor of AWS CDK downstairs is AWS CloudFormation. Use cdk synth
command tool to convert to AWS CloudFormation Template, and then use cdk deploy
command to easily deploy various AWS products/resources, or use cdk destroy
command to enjoy one-click restoration.
For the first getting started, I will recommend to watch Pahud Dev’s AWS CDK series of videos, or CDK Workshop to experience the whole process which is simple (and enjoyable).
- Hands-on faction can refer to examples in official AWS CDK docs(Serverless, ECS, CodePipeline), or official CDK example collections (At present, TypeScript and Python examples are relatively complete, and there are also examples covering Java, JavaScript, .NET), Find an example that suits your application scenario and play it first. (Find a clean AWS Region or use AWS Organizations to create a clean AWS Root Account to play.)
- Academic faction can refer to this note (this article you’re reading) or AWS CDK official document Concepts and other paragraphs.
- Creative faction can get started from AWS Construct library and the source aws/aws-cdk on GitHub, or refer to How to Create CDK Constructs (by AWS Hero Matthew Bonig).
- Social faction please hurry up and attend all the CDK meetups around the world :p For example: Taiwan CDK Meetup #TaiwanCDKMeetup starts monthly meetups since July 2020.
In the very beginning of playing with AWS CDK, most of time are using the default settings and default environment. We will gradually use more detailed settings when further use on the mass production environment. Sometimes the delicate link is not yet ready, we can consider to have contribution on L2/L3 constructs. Find more contribution resources at the Reference section below.
History
From ancient to modern times. Get through all the context.
- 2017-10-05: First git repo initial commit.
- 2018-06-13: First GitHub Release: v0.7.0-beta
- 2018-08-27: AWS CDK Developer Preview
- 2019-07-11: The AWS Cloud Development Kit (AWS CDK) is Now Generally Available; GA Release: v1.0.0 🎉🎉🥂🥂🍾🍾
- 2020-07-16: CDK for Terraform (experimental project from HashiCorp)
- 2021-01-27: eks: Graduate to stable
Terms
Here is a list of nouns that appear on the scene, and the full name of the original text, noun definition and source are noted.
- AWS Cloud Development Kit (AWS CDK) is a software development framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation.
- Constructs
- Constructs are the basic building blocks of AWS CDK apps. A construct represents a “cloud component” and encapsulates everything AWS CloudFormation needs to create the component.
- the basic building blocks.
- Can be big or small.
- Small enough to correspond to a cloud product (for example, an Amazon S3 bucket).
- Big to be able to span multiple AWS accounts and multiple regions.
- In the world of AWS CDK, everything is a Construct.
- Similar to the Flutter world, everything is Widget.
- AWS Construct library
- The Matrix: The Construct Explained
- Apps
- App is also one kind of Construct.
- The App construct doesn’t require any initialization arguments, because it’s the only construct that can be used as a root for the construct tree.
- Stacks
- The unit of deployment in the AWS CDK is called a stack.
- All AWS resources defined within the scope of a stack, either directly or indirectly, are provisioned as a single unit.
- Environments
- Each Stack instance in your AWS CDK app is explicitly or implicitly associated with an environment (env).
- An environment is the target AWS account and AWS Region into which the stack is intended to be deployed.
Use Cases
- Hope to implement IaC (Infrastructure as Code).
- Hope to include the infrastructure/architecture also to be under version control.
- Suitable for front-end personnel, for example, can deploy S3 / API Gateway freely(Refer to Pahud’s Youtube video: 【Pahud Dev】EP17 - 立即開箱 Amazon API Gateway HTTP API with AWS CDK (4K 2160p))
- Don’t want to learn/don’t want to use CloudFormation in a direct way.
- Don’t want to learn/don’t want to use AWS CLI / CLI v2.
- Recommended using with AWS Organizations.
Limits
- When using the AWS CDK, you must also consider the limitations of AWS CloudFormation. Such as:
- Each AWS CloudFormation template is limited to.
- 500 resources (It can be bypassed by using nested stacks / multiple stacks). (previously 200)
- 200 parameters. (previously 60)
- 200 mappings. (previously 100)
- 200 outputs. (previously 60)
- The AWS CloudFormation resource name must be less than 255 characters.
- AWS CloudFormation (default) can create up to 200 stacks.
- Each AWS CloudFormation template is limited to.
- Memo: 2020/10 AWS CloudFormation now supports increased limits on five service quotas。
Pricing
- Free!
- AWS CDK itself is a development tool.
- Please refer to the latest official document. This is just shorthand.
- The invocation/use of AWS CDK corresponds to various AWS Products and charges are based on the charging rules of each product.
Frequently Information
Prerequisites
- Node.js
- 10.3.0 or later.
- even those working in languages other than TypeScript or JavaScript.
- The AWS CDK Toolkit (cdk command-line tool) and the AWS Construct Library are developed in TypeScript and run on Node.js.
- AWS suggests the latest LTS version.
- 13.0.0 through 13.6.0 are not compatible with the AWS CDK.
- AWS credentials
- AWS CLI
Frequently Commands: Install/Update AWS CDK Toolkit
Install AWS CDK Toolkit
npm install -g aws-cdk
Install a specific version (lock version) AWS CDK Toolkit
npm install -g aws-cdk@1.50.0
Update AWS CDK Toolkit
npm update -g aws-cdk
Check version
cdk --version
Frequently Commands: Operation
mkdir hello-cdk
cd hello-cdk
cdk init --language typescript
npm run build
(compile typescript to js)npm install @aws-cdk/aws-s3
- Edit your code
npm run build
cdk bootstrap
(Bootstrap once, per account per region)cdk synth
(Synthesizing an AWS CloudFormation Template. Generate file cdk.context.json.)cdk deploy
- Do some extra editing on your code.
npm run build
cdk diff
cdk deploy
cdk destroy
Concepts
Constructs
Constructs are the basic building blocks of AWS CDK apps. A construct represents a “cloud component” and encapsulates everything AWS CloudFormation needs to create the component.
The basic building blocks
Can be big or small.
- Small enough to correspond to a cloud product (for example, an Amazon S3 bucket).
- Big to be able to span multiple AWS accounts and multiple regions.
In the world of AWS CDK, everything is a Construct.
- Similar to the Flutter world, everything is a Widget.
AWS Construct library
- Docs
- This library includes constructs that represent all the resources available on AWS.
- For example, the
s3.Bucket
class represents an Amazon S3 bucket, and thedynamodb.Table
class represents an Amazon DynamoDB table.
- For example, the
- There are different levels of constructs in AWS Construct Library.
- Bottom level: CFN Resources. (AWS CloudFormation-only) (L1) (Level 1)
- These constructs represent all of the AWS resources that are available in AWS CloudFormation. CFN Resources are generated from the AWS CloudFormation Resource Specification on a regular basis. They are named
CfnXyz
, where Xyz represents the name of the resource. - For example,
s3.CfnBucket
represents theAWS::S3::Bucket
CFN Resource.
- These constructs represent all of the AWS resources that are available in AWS CloudFormation. CFN Resources are generated from the AWS CloudFormation Resource Specification on a regular basis. They are named
- Higher level: AWS constructs. (Curated) (L2)
- They provide the same functionality, but handle much of the details, boilerplate, and glue logic required by CFN constructs. AWS constructs offer convenient defaults and reduce the need to know all the details about the AWS resources they represent, while providing convenience methods that make it simpler to work with the resource.
- Example:
s3.Bucket
.
- Even higher-level: Patterns. (Patterns) (L3)
- These constructs are designed to help you complete common tasks in AWS, often involving multiple kinds of resources.
- For example, the
aws-ecs-patterns.ApplicationLoadBalancedFargateService
construct represents an architecture that includes an AWS Fargate container cluster employing an Application Load Balancer (ALB).
- Bottom level: CFN Resources. (AWS CloudFormation-only) (L1) (Level 1)
Composition
The key pattern for defining higher-level abstractions through constructs is called composition.
Initialization
Constructs are implemented in classes that extend the
Construct
base class. You define a construct by instantiating the class. All constructs take three parameters when they are initialized:- scope
- Usually using
this
- Usually using
- id
- identifier
- namespace
- unique
- resource names
- AWS CloudFormation logical IDs
- props
- A set of properties or keyword arguments
Apps
App is also one kind of Construct.
The App construct doesn’t require any initialization arguments, because it’s the only construct that can be used as a root for the construct tree.
You can use the App instance as a scope for defining a single instance of your stack.
App lifecycle:
- The following diagram shows the phases that the AWS CDK goes through when you call the cdk deploy.
(Image source: Official docs)
To work with the CDK CLI, you need to let it know how to execute an AWS CDK app.
cdk --app executable cdk-command
cdk.json:
1 2 3
{ "app": "node bin/my-app.js" }
Example: The following example lists the stacks defined in the cloud assembly stored under ./my-cloud-assembly.
cdk --app ./my-cloud-assembly ls
Stacks
The unit of deployment in the AWS CDK is called a stack.
All AWS resources defined within the scope of a stack, either directly or indirectly, are provisioned as a single unit.
When you run the cdk synth command for an app with multiple stacks, the cloud assembly includes a separate template for each stack instance.
Because AWS CDK stacks are implemented through AWS CloudFormation stacks, they have the same limitations as in AWS CloudFormation.
1 2 3 4 5 6
const app = new App(); new MyFirstStack(app, 'stack1'); new MySecondStack(app, 'stack2'); app.synth();
To list all the stacks in an AWS CDK app, run the
cdk ls
command, which for the previous AWS CDK app would have the following output.stack1 stack2
Nested stacks
- class NestedStack (construct)
- A CloudFormation nested stack.
- The NestedStack construct offers a way around the AWS CloudFormation 500-resource limit for stacks.
- The scope of a nested stack must be a
Stack
orNestedStack
construct. - parent stacks v.s nested stacks
- Nested stacks are bound to their parent stack and are not treated as independent deployment artifacts; they are not listed by
cdk list
nor can they be deployed bycdk deploy
.
- Nested stacks are bound to their parent stack and are not treated as independent deployment artifacts; they are not listed by
Environments
Each Stack instance in your AWS CDK app is explicitly or implicitly associated with an environment (env).
An environment is the target AWS account and AWS Region into which the stack is intended to be deployed.
For production stacks, AWS recommends that you explicitly specify the environment for each stack in your app using the env property.
Example: specifies different environments for its two different stacks.
1 2 3 4 5
const envEU = { account: '2383838383', region: 'eu-west-1' }; const envUSA = { account: '8373873873', region: 'us-west-2' }; new MyFirstStack(app, 'first-stack-us', { env: envUSA }); new MyFirstStack(app, 'first-stack-eu', { env: envEU });
To make the stack deployable to a different target, but to determine the target at synthesis time, your stack can use two environment variables provided by the AWS CDK CLI:
CDK_DEFAULT_ACCOUNT
andCDK_DEFAULT_REGION
.- These variables are set based on the AWS profile specified using the
--profile
option, or the default AWS profile if you don’t specify one.
1 2 3 4 5
new MyDevStack(app, 'dev', { env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION }});
- These variables are set based on the AWS profile specified using the
You can set env however you like, using any valid expression.
- Example: you might write your stack to support two additional environment variables to let you override the account and region at synthesis time.
1 2 3 4 5
new MyDevStack(app, 'dev', { env: { account: process.env.CDK_DEPLOY_ACCOUNT || process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEPLOY_REGION || process.env.CDK_DEFAULT_REGION }});
With your stack’s environment declared this way, you can now write a short script or batch file like the following to set the variables from command line arguments, then call
cdk deploy
:cdk-deploy-to.sh
:
1 2 3 4 5 6
#!/bin/bash export CDK_DEPLOY_ACCOUNT=$1 shift export CDK_DEPLOY_REGION=$1 shift cdk deploy "$@"
cdk-deploy-to-test.sh
:
1 2
#!/bin/bash bash cdk-deploy-to.sh 123457689 us-east-1 "$@"
cdk-deploy-to-prod.sh
:
1 2 3
#!/bin/bash bash cdk-deploy-to.sh 135792468 us-west-1 "$@" || exit bash cdk-deploy-to.sh 246813579 eu-west-1 "$@"
Resources
The AWS CDK provides a rich class library of constructs, called AWS constructs, that represent all AWS resources.
Resource attributes
Most resources in the AWS Construct Library expose attributes, which are resolved at deployment time by AWS CloudFormation.
1 2 3 4
import * as sqs from '@aws-cdk/aws-sqs'; const queue = new sqs.Queue(this, 'MyQueue'); const url = queue.queueUrl; // => A string representing a deploy-time value
Referencing resources
Many AWS CDK classes require properties that are AWS CDK resource objects (resources). To satisfy these requirements, you can refer to a resource in one of two ways:
- By passing the resource directly
- By passing the resource’s unique identifier, which is typically an ARN, but it could also be an ID or a name
If a construct property represents another AWS construct, its type is that of the interface type of that construct. Examples:
- the Amazon ECS service takes a property
cluster
of typeecs.ICluster
; - the CloudFront distribution takes a property
sourceBucket
(Python:source_bucket
) of types3.IBucket
.
- the Amazon ECS service takes a property
Example: define an Amazon ECS cluster and then uses it to define an Amazon ECS service.
1 2 3
const cluster = new ecs.Cluster(this, 'Cluster', { /*...*/ }); const service = new ecs.Ec2Service(this, 'Service', { cluster: cluster });
Physical names
The logical names of resources in AWS CloudFormation are different from the physical names of resources that are shown in the AWS Management Console after AWS CloudFormation has deployed the resources. The AWS CDK calls these final names physical names.
For example, AWS CloudFormation might create the Amazon S3 bucket with the logical ID
Stack2MyBucket4DD88B4F
from the previous example with the physical namestack2mybucket4dd88b4f-iuv1rbv9z3to
.You can specify a physical name when creating constructs that represent resources by using the property
<resourceType>Name
. The following example creates an Amazon S3 bucket with the physical namemy-bucket-name
.1 2 3
const bucket = new s3.Bucket(this, 'MyBucket', { bucketName: 'my-bucket-name', });
(Disadvantage) Assigning physical names to resources has some disadvantages in AWS CloudFormation.
Most importantly, any changes to deployed resources that require a resource replacement, such as changes to a resource’s properties that are immutable after creation, will fail if a resource has a physical name assigned.
(Can only be deleted and re-deploy once.) If you end up in a state like that, the only solution is to delete the AWS CloudFormation stack, then deploy the AWS CDK app again.
(Some cross-environment needs to specify fixed physical names, which can be named by AWS CDK.) In some cases, such as when creating an AWS CDK app with cross-environment references, physical names are required for the AWS CDK to function correctly. In those cases, if you don’t want to bother with coming up with a physical name yourself, you can let the AWS CDK name it for you by using the special value
PhysicalName.GENERATE_IF_NEEDED
1 2 3
const bucket = new s3.Bucket(this, 'MyBucket', { bucketName: core.PhysicalName.GENERATE_IF_NEEDED, });
Importing existing external resources
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Construct a resource (bucket) just by its name (must be same account) s3.Bucket.fromBucketName(this, 'MyBucket', 'my-bucket-name'); // Construct a resource (bucket) by its full ARN (can be cross account) s3.Bucket.fromArn(this, 'MyBucket', 'arn:aws:s3:::my-bucket-name'); // Construct a resource by giving attribute(s) (complex resources) ec2.Vpc.fromVpcAttributes(this, 'MyVpc', { vpcId: 'vpc-1234567890abcde', }); // quert by default ec2.Vpc.fromLookup(this, 'DefaultVpc', { isDefault: true }); // query by tag ec2.Vpc.fromLookup(this, 'PublicVpc', {tags: {'aws-cdk:subnet-type': "Public"} });
Permission grants
- The grant methods return an
iam.Grant
object.
1 2 3 4 5
if (bucket.grantReadWrite(func).success) { // ... } table.grant(func, 'dynamodb:CreateBackup');
Metrics and alarms
- Docs
- AWS constructs have metric methods that allow easy access to the metrics without having to look up the correct name to use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import * as cw from '@aws-cdk/aws-cloudwatch'; import * as sqs from '@aws-cdk/aws-sqs'; import { Duration } from '@aws-cdk/core'; const queue = new sqs.Queue(this, 'MyQueue'); const metric = queue.metricApproximateNumberOfMessagesNotVisible({ label: 'Messages Visible (Approx)', period: Duration.minutes(5), // ... }); metric.createAlarm(this, 'TooManyMessagesAlarm', { comparisonOperator: cw.ComparisonOperator.GREATER_THAN_THRESHOLD, threshold: 100, // ... });
Network traffic
- Docs
- Resources that establish or listen for connections expose methods that enable traffic flows, including setting security group rules or network ACLs.
- IConnectable resources have a
connections
property that is the gateway to network traffic rules configuration.
Event handling
Some resources can act as event sources. Use the
addEventNotification
method (Python:add_event_notification
) to register an event target to a particular event type emitted by the resource.In addition to this,
addXxxNotification
methods offer a simple way to register a handler for common event types.The following example shows how to trigger a Lambda function when an object is added to an Amazon S3 bucket:
1 2 3 4 5
import * as s3nots from '@aws-cdk/aws-s3-notifications'; const handler = new lambda.Function(this, 'Handler', { /*…*/ }); const bucket = new s3.Bucket(this, 'Bucket'); bucket.addObjectCreatedNotification(new s3nots.LambdaDestination(handler));
Removal policies
Resources that maintain persistent data, such as databases and Amazon S3 buckets, have a removal policy that indicates whether to delete persistent objects when the AWS CDK stack that contains them is destroyed.
The values specifying the removal policy are available through the
RemovalPolicy
enumeration in the AWS CDKcore
module.Example:
1 2 3 4 5 6 7 8 9 10 11 12
import * as cdk from '@aws-cdk/core'; import * as s3 from '@aws-cdk/aws-s3'; export class CdkTestStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); const bucket = new s3.Bucket(this, 'Bucket', { removalPolicy: cdk.RemovalPolicy.DESTROY, }); } }
Example: You can also apply a removal policy directly to the underlying AWS CloudFormation resource via the applyRemovalPolicy() method.
1 2
const resource = bucket.node.findChild('Resource') as cdk.CfnResource; resource.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY);
Identifiers
The AWS CDK deals with many types of identifiers and names.
Identifiers must be unique within the scope in which they are created; they do not need to be globally unique in your AWS CDK application.
Construct IDs
- The most common identifier,
id
, is the identifier passed as the second argument when instantiating a construct object.
Paths
We refer to the collection of IDs from a given construct, its parent construct, its grandparent, and so on to the root of the construct tree, as a path.
The AWS CDK typically displays paths in your templates as a string, with the IDs from the levels separated by slashes, starting at the node just below the root App instance, which is usually a stack. For example, the paths of the two Amazon S3 bucket resources in the previous code example are
Stack1/MyBucket
andStack2/MyBucket
.1
const path: string = myConstruct.node.path;
Unique IDs
Since the AWS CDK already has paths that are globally unique, the AWS CDK generates these unique identifiers by concatenating the elements of the path, and adds an 8-digit hash. The hash is necessary, as otherwise two distinct paths, such as
A/B/C
andA/BC
would result in the same identifier. The AWS CDK calls this concatenated path elements and hash the unique ID of the construct.1
const uid: string = myConstruct.node.uniqueId;
Logical IDs
- Unique IDs serve as the logical identifiers, which are sometimes called logical names, of resources in the generated AWS CloudFormation templates for those constructs that represent AWS resources.
- Avoid changing the logical ID of a resource between deployments. Since AWS CloudFormation identifies resources by their logical ID, if you change the logical ID of a resource, AWS CloudFormation deletes the existing resource, and then creates a new resource with the new logical ID.
- The most common identifier,
Tokens
- Tokens represent values that can only be resolved at a later time in the lifecycle of an app (see App lifecycle). For example, the name of an Amazon S3 bucket that you define in your AWS CDK app is only allocated by AWS CloudFormation when you deploy your app. If you print the bucket.bucketName attribute, which is a string, you see it contains something like
${TOKEN[Bucket.Name.1234]}
.
Parameters
- AWS CloudFormation templates can contain parameters—custom values that are supplied at deployment time and incorporated into the template. Since the AWS CDK synthesizes AWS CloudFormation templates, it too offers support for deployment-time parameters.
- In general, we recommend against using AWS CloudFormation parameters with the AWS CDK. Parameter values are not available at synthesis time and thus cannot be easily used in other parts of your AWS CDK app, particularly for control flow.
Tagging
The following example applies the tag key with the value value to a construct:
1
Tag.add(myConstruct, 'key', 'value');
The following example deletes the tag key from a construct:
1
Tag.remove(myConstruct, 'key');
By default, applying a tag has a priority of 100 and removing a tag has a priority of 200. To change the priority of applying a tag, pass a
priority
property toTag.add()
orTag.remove()
.The following applies a tag with a priority of 300 to a construct:
1 2 3
Tag.add(myConstruct, 'key', 'value', { priority: 300 });
Tag.add()
1 2 3 4 5 6
Tag.add(myConstruct, 'tagname', 'value', { applyToLaunchedInstances: false, includeResourceTypes: ['AWS::Xxx::Yyy'], excludeResourceTypes: ['AWS::Xxx::Zzz'], priority: 100, });
- These properties have the following meanings.
applyToLaunchedInstances
- By default, tags are applied to instances launched in an Auto Scaling group. Set this property to false to not apply tags to instances launched in an Auto Scaling group
includeResourceTypes
/excludeResourceTypes
- Use these to apply tags only to a subset of resources, based on AWS CloudFormation resource types. By default, the tag is applied to all resources in the construct subtree, but this can be changed by including or excluding certain resource types. Exclude takes precedence over include, if both are specified.
priority
- Use this to set the priority of this operation with respect to other Tag.add() and Tag.remove() operations. Higher values take precedence over lower values. The default is 100.
- These properties have the following meanings.
Assets
Assets are local files, directories, or Docker images that can be bundled into AWS CDK libraries and apps; for example, a directory that contains the handler code for an AWS Lambda function. Assets can represent any artifact that the app needs to operate.
You typically reference assets through APIs that are exposed by specific AWS constructs. For example, when you define a lambda.Function construct, the code property lets you pass an asset (directory).
Function
uses assets to bundle the contents of the directory and use it for the function’s code.Similarly, ecs.ContainerImage.fromAsset uses a Docker image built from a local directory when defining an Amazon ECS task definition. Practical!
Docker image assets
The AWS CDK supports bundling local Docker images as assets through the aws-ecr-assets module.
The following example defines a docker image that is built locally and pushed to Amazon ECR. Images are built from a local Docker context directory (with a Dockerfile) and uploaded to Amazon ECR by the AWS CDK CLI or your app’s CI/CD pipeline, and can be naturally referenced in your AWS CDK app.
The
my-image
directory must include a Dockerfile. The AWS CDK CLI builds a Docker image from my-image, pushes it to an Amazon ECR repository, and specifies the name of the repository as an AWS CloudFormation parameter to your stack. Docker image asset types expose deploy-time attributes that can be referenced in AWS CDK libraries and apps. The AWS CDK CLI commandcdk synth
displays asset properties as AWS CloudFormation parameters.1 2 3 4 5
import { DockerImageAsset } from '@aws-cdk/aws-ecr-assets'; const asset = new DockerImageAsset(this, 'MyBuildImage', { directory: path.join(__dirname, 'my-image') });
Amazon ECS task definition example
A common use case is to create an Amazon ECS TaskDefinition to run docker containers. The following example specifies the location of a Docker image asset that the AWS CDK builds locally and pushes to Amazon ECR.
1 2 3 4 5 6 7 8 9 10 11
import * as ecs from '@aws-cdk/aws-ecs'; import * as path from 'path'; const taskDefinition = new ecs.FargateTaskDefinition(this, "TaskDef", { memoryLimitMiB: 1024, cpu: 512 }); taskDefinition.addContainer("my-other-container", { image: ecs.ContainerImage.fromAsset(path.join(__dirname, "..", "demo-image")) });
Deep Dive
The general direction is a path, but beware of pits on the ground.
How to deploy multiple CfnResource in a specific/given order?
The deployment order of CfnResource we wrote in TypeScript may not necessarily be the order of AWS CloudFormation. If you want to establish the dependency (sequential correlation) between multiple CfnResources, you can use cfnResource.addDependsOn
method.
#asynchronous #synchronous #await #dependsOn #resourceDependencies
|
|
The order in the case will be:
resourceA = deployment 1st
resourceB = deployment 2nd
Reference:
Reference
Docs
- AWS CDK Developer Guide
- AWS CDK API Reference
- AWS Construct Library reference
- CDK Construct Catalog, by source code (an index of multi-language AWS CDK libraries.)
Awesome Lists
- eladb/awesome-cdk, by Elad Ben-Israel (the amazing #awscdk team!!)
Community
Workshops
Getting Started
- AWS CDK Introduction, by Pahud (Developer Advocate, AWS) (There are at least 12+ videos in 4K high resolution, so that you can see the source code very clearly, and lead step by step. (Chinese) (Video))
- CFH partner workshop - Manage your infrastructure as code on AWS, 2020-06-22, by Darko Mesaroš (Developer Advocate, AWS), at Web Summit
Examples
- AWS CDK Examples (At present, TypeScript and Python examples are relatively complete, and there are also examples covering Java, JavaScript, .NET)
- DevOps Taiwan: Kubernetes Jenkins workshop, by John Chen (陳志强) & Rico Chen (陳缺牙)
- cdk-lambda-bash (Deploy Bash Lambda Functions with AWS CDK), by Pahud (Developer Advocate, AWS)
- CDK Serverless Examples
- CDK ECS Examples
- CDK CodePipeline Examples
Source Code
Articles & Talks
- AWS Blog about AWS CDK
- AWS CDK on DEV.to
- CDK Weekly (Newsletter)
- AWS CDK 初探, 2019-07-30, by Azole (in Chinese)
- Infrastructure IS Code - Intro to AWS CDK, 2019-12-11, by GDG Cyprus
- Infrastructure Is Code with the AWS Cloud Development Kit (CDK), 2020-02-10, by Richard Boyd, at QCon
- What I learned using the AWS CDK in the past year, 2020-11-12, by RAFAEL ZAMANA KINEIPPE
- CDK Corner – January 2021, 2021-01-14, by Richard H Boyd, at AWS DevOps Blog
- Exploring CDK Internals, 2021-04-19, by Mauricio Klein
- Topics: CDK construct levels, CDK tree, Construct path, How logical and unique IDs are calculated, How to fine tune the resulting CloudFormation template generated by CDK.
- Accelerating development with AWS CDK plugin – CfnGuardValidator, 2023-09-15, by Xiaoxue Xu and Kanwar Bajwa
Comparison
- CDK vs. CloudFormation vs. Terraform vs. Pulumi, 2020-02-28, by David Krohn, at globaldatanet
- Rollback capability is the advantage of CDK/CloudFormation.
Contribution
- (English) Create Your First AWS CDK Pull Request (4K 2160p), 2020-08-11, by Pahud (Developer Advocate, AWS)
- (English) Create Your First AWS CDK Pull Request: Study Notes, 2020-08-10, by Ernest Chiang (AWS Community Hero)
- (Chinses) 創建你的第一個 AWS CDK pull request (4K 2160p), 2020-08-03, by Pahud (Developer Advocate, AWS)
- (Chinses) 創建你的第一個 AWS CDK pull request: 學習筆記, 2020-08-05, by Ernest Chiang (AWS Community Hero)
Trivia
- Q: How many files are included in the first init commit of the git repo of the CDK project?
- Q: What is the first version number released in the GitHub repo of the CDK project?