This article summarizes the basic commands frequently used in AWS CDK.
Introduction
AWS CDK is a framework that allows you to define and deploy AWS infrastructure resources using languages like TypeScript and Python.
It works by generating CloudFormation templates from CDK code, which are then used to deploy resources to AWS.
CDK is easier to write than CloudFormation templates written directly in YAML and operates at a higher level of abstraction (requiring less code). For these reasons, I personally prefer using it.
CDK provides several commands for tasks such as creating projects, deploying and deleting resources, and more.
Since I often find myself forgetting these commands and having to look them up, I've decided to compile this reference guide of basic CDK commands.
Note: This article was translated from my original post.
Environment
The working environment for this article is as follows:
CDK version:
$ cdk --version 1.31.0 (build 8f3ac79)
Testing was performed on Amazon Linux Cloud9.
$ cat /etc/system-release Amazon Linux AMI release 2018.03
CDK Commands Summary
I'll organize the necessary commands following a typical workflow.
Check CDK Version
Check the installed CDK version:
cdk --version
Install CDK
If CDK is not installed, use the following npm command to install it:
npm install -g aws-cdk
Create CDK Project
Create a new CDK project.
Here are the main examples (for Python):
- To create a CDK project from a simple template:
cdk init app --language=python
- To create a CDK project with sample code:
cdk init sample-app --language=python
- To create a clean CDK project without virtual environment or git files:
cdk init --generate-only --language=python
The detailed syntax is:
cdk init [template] --language=[python|typescript|csharp|fsharp|java|javascript]
For the [template] part, optionally specify one of app, sample-app, lib,
and for the [language] part, specify a language such as python or typescript.
You can check the characteristics of each template (app, lib, sample-app) with cdk init --list:
$ cdk init --list Available templates: * app: Template for a CDK Application └─ cdk init app --language=[csharp|fsharp|java|javascript|python|typescript] * lib: Template for a CDK Construct Library └─ cdk init lib --language=typescript * sample-app: Example CDK Application with some constructs └─ cdk init sample-app --language=[csharp|fsharp|java|javascript|python|typescript]
app: A bare CDK template
sample-app: A CDK template with pre-created Stack samples and test folders
lib: A template for CDK Construct Libraries
If you want to create a clean CDK project without a virtual environment or git setup, use --generate-only without specifying a template.
Preparation Before Running CDK Commands
While not directly related to CDK commands, here are some steps you need to complete before starting work within a CDK project.
※These examples are for Python.
- Activate virtual environment
Activate the Python virtual environment .env created during cdk init:
source .env/bin/activate
- Install required packages
Install the Python packages listed in requirements.txt:
pip install -r requirements.txt
List CDK Stacks
Display a list of CDK Stacks included in your CDK code:
cdk ls
Generate CloudFormation Template
Generate and display CloudFormation templates from the CDK Stacks in your CDK code:
cdk synth
You can also specify a particular Stack to generate and display its CloudFormation template:
cdk synth [Stack-name]
※Stack names can be checked with cdk ls.
Preparation Before CDK Deployment: bootstrap
Before deploying AWS resources defined in CDK, set up the CDK deployment environment with the following command:
cdk bootstrap
Running cdk bootstrap creates a CloudFormation Stack called CDKToolkit and an S3 bucket named cdktoolkit-stagingbucket-[random].
The CDKToolkit Stack must exist in the target deployment region.
Deploy CDK
Deploy the AWS resources defined in your CDK code:
cdk deploy [Stack-name]
If there's only one Stack in your CDK code, you can deploy without specifying the Stack name:
cdk deploy
To deploy all Stacks, add the --all option:
cdk deploy --all
Display CDK Stack Differences
When you make changes to your code, display the differences in CDK Stack resources:
cdk diff [Stack-name]
If you don't specify a Stack name, differences for all Stacks will be displayed:
cdk diff
Delete CDK Resources
Delete resources deployed with CDK:
cdk destroy [Stack-name]
If there's only one Stack in your CDK code, you can delete resources without specifying the Stack name:
cdk destroy
Conclusion
This article summarized the basic CDK commands.
CDK offers better readability than CloudFormation templates and provides benefits typical of general-purpose programming languages, such as the ability to write test code.
I plan to continue using it extensively.
[Related Articles]
https://en.bioerrorlog.work/entry/cdk-nested-stacken.bioerrorlog.work