What does CDK Deploy do in AWS CDK


Borislav Hadzhiev

Wed Apr 13 20222 min read


Photo by Andreas Dress

Updated - Wed Apr 13 2022

CDK deploy - introduction #

The CDK deploy command deploys our CDK stack(s) as CloudFormation template(s).

The syntax of the command looks like:

npx aws-cdk deploy

When deploying more than one stack, it's better to explicitly specify the stack names:

npx aws-cdk deploy \ my-stack-dev \ my-stack-prod

CDK is just an abstraction level above CloudFormation. The whole idea behind CDK is to improve developer experience by allowing us to use a programming language, rather than a configuration language like json or yaml.

Our CDK code eventually gets compiled down to CloudFormation code before a deployment is ran.

CDK deploy - in depth #

When we call the cdk deploy command a couple of things happen behind the scenes:

The cdk synth command is called. It runs our CDK app and synthesizes a CloudFormation template in the cdk.out directory:

cdk out folder

The cdk.out directory contains our asset files and the CloudFormation template. An example of asset files would be the code required for a Lambda function:

cdk out lambda

We can look at the cdk.out directory as a preparation step before deployment.

Next, our assets code and the CloudFormation template will be deployed to an S3 bucket that was provisioned with the cdk bootstrap command.

We only have to run cdk bootstrap once per environment (account and region) and it provisions a CloudFormation stack, called CDKToolkit.

The CDKToolkit stack consists of an S3 bucket that stores our asset files:

cdk toolkit stack

If we have our environment bootstrapped the CDK CLI then deploys our asset files to the S3 bucket:

assets directory

After our assets are globally available via an S3 bucket our CloudFormation template is deployed / updated.

Conclusion #

The cdk deploy command generates and then deploys the CloudFormation equivalent of our CDK code.

Note that it's not necessary to run the cdk synth command before we run cdk deploy, because the CDK CLI will automatically synthesize a CloudFormation template for each stack before deployment.

Further Reading #

Use the search field on my Home Page to filter through my more than 1,000 articles.