I will explain the basics of CDK in practice: how CDK works and how to deploy a CDK project from scratch.
In addition, I will share the source code of my CDK project used to create the infrastructure of my blog.
This section contains:
- What is CDK
- How CDK works
What is CDK
AWS CDK provisions your resources in a safe, repeatable manner through AWS CloudFormation.
To me, with a developer background, CloudFormation is complex and CDK fills the gap because it allows me to use a programming language to create the infrastructure easily, it’s wonderful.
How CDK works
To interact with CDK apps you will need the AWS CDK Toolkit (command-line tool).
In a nutshell:
- Add code: Add the desired AWS resources in the
appcode with your preferred programming language.
- Transform the code into a CloudFormation template: Run the
cdk synthcommand from the AWS CDK Toolkit to generate the
CloudFormation templatefrom the
- Deploy the infrastructure: Run the
cdk deploycommand from the AWS CDK Toolkit to
create a new stack on the CloudFormation service, which will deploy the
AWS resourcesto the configured AWS account.
AWS CDK Toolkit commands you need to know:
- AWS CLI
- IDE for your programming language
- AWS CDK Toolkit
npm install -g aws-cdk
- You must execute it once per environment (account and region) to allow CDK to create the resources it needs to run
How to create infrastructure with CDK
This section contains:
- How to create and deploy a basic CDK application
- Clean up
- Make changes to the default CDK application and deploy it
How to create and deploy a basic CDK application
I have chosen TypeScript as my programming language.
What is the fastest way to create a new CDK project and deploy it in your AWS account?
1 2 3 4 5 6 7 8 # Create an empty folder mkdir cdk-basic-example && cd cdk-basic-example # Creates an example of CDK Application with some constructs cdk init sample-app --language typescript # Deploy and generate the resources into your AWS account cdk deploy --require-approval never
And that’s all, we have deployed one topic and one queue in our AWS Account…
NOTE: What about the generation of the CloudFormation template in the
synthphase? We have executed only the
When you execute
cdk deploybehind the scenes also…
Let’s destroy the stack. I know this section maybe should be at the end, but where’s the fun in that? We are playing and we need to try different things.
If you try to do something different and on your own, you will learn faster!
1 2 # Delete the CloudFormation stack (so it will delete all resources related) cdk destroy --force
You have to know how to destroy a stack, so remember to do it at the end if you are playing with cdk…
Make changes to the default CDK application and deploy it
Perhaps we want to check what is to be deployed before we deploy it?
It makes sense to me.
1 2 3 4 # Synthesizes the CloudFormation template for the specified stack(s) # In the console, the template will be printed in yaml format # In the "cdk.out" folder, the template will be in json format cdk synth
What if we compare the local code with the stack deployed in the AWS account?
1 cdk diff
cdk diffneeds to connect to the AWS Account to check the CloudFormation stack against your local resources.
We can see all the new resources that will be created:
- [+] AWS::SQS::Queue
- [+] AWS::SQS::QueuePolicy
- [+] AWS::SNS::Subscription
- [+] AWS::SNS::Topic
Remember that we have destroyed the stack in the previous step so all resources are detected as new.
We deploy it again:
1 cdk deploy --require-approval never
And we run the diff command again to see the differences between the local code and the deployed stack:
1 cdk diff
Now let’s update the CDK code to generate some differences. First of all, we have to open the project with our IDE (you can also do it with a notepad but…)
This file contains the 2 AWS resources of my example, a queue (red) and a topic (yellow). I could add a new service but for simplicity, I will remove the topic (lines 15 to 17) and run the
cdk diff again.
1 cdk diff
We can see that we have deleted the topic in the code, and when we run the diff command CDK finds the changes and shows them to us.
And that’s all, keep practicing and learning!
Remember to destroy your stack when you are done playing
Bonus: My CDK blog code
The source code is available here
If you review it and think it can be improved, please let me know.
- If you need more information about CDK I recommend you to visit the AWS documentation here
- Next post: How to add CI/CD to my CDK project
- Comment this post