What does CDK Deploy do in AWS CDK

avatar

Borislav Hadzhiev

Fri Apr 23 20212 min read

banner

Photo by Andreas Dress

Updated on Fri Apr 23 2021

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

CDK deploy - introduction #

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

The syntax of the command looks like:

shell
npx cdk deploy

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

shell
npx 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 executed.

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 executes 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 #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee