Creating a new CDK app with Python
In this post, we'll create a new CDK app that uses Python as the programming language.
Ensure that AWS CDK is installed & configured before proceeding.
Creating a new CDK app
a) Create a new directory for the CDK app and navigate to it:
mkdir cdk-app && cd cdk-app
b) To create a new CDK app, run the following command:
cdk init app --language python
The above command will create the following:
- CDK App: Create a new CDK app in the current directory. It includes a
cdk.jsonfile that includes the configuration for the CDK app
- Stacks: Create a folder called
cdk-appin the current directory that includes the
stacksthat will be deployed
- Python Virtual Environment: Create a Python virtual environment for the app in the .venv folder. It also creates a
requirements.txtfile that includes the Python dependencies for the app
- Git: Create a git repository for the app including a
c) Activate the Python virtual environment:
If you are using Windows, run the following command instead:
d) Install the Python dependencies:
pip install -r requirements.txt -r requirements-dev.txt
Modifying the CDK app
a) Open the
app.py file. This file includes the basic CDK app that includes a single stack called
import os import aws_cdk as cdk from cdk_app.cdk_app_stack import CdkAppStack app = cdk.App() CdkAppStack(app, "CdkAppStack",) app.synth()
In the above code, we are importing the
CdkAppStack class from the
cdk_app_stack.py file. This file is located in the
b) Open the
cdk_app_stack.py file. This file includes the
CdkAppStack class that extends the
from aws_cdk import ( Stack, ) from constructs import Construct class CdkAppStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # The code that defines your stack goes here
CdkAppStack class does not include any resources. Let's add a S3 bucket to the stack.
c) Modify the
cdk_app_stack.py file as follows:
from aws_cdk import ( Stack, aws_s3 as s3, ) from constructs import Construct class CdkAppStack(Stack): BUCKET_ID = "MyFirstBucket" def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # The code that defines your stack goes here s3.Bucket(self, id=self.BUCKET_ID)
BUCKET_NAME variable to a name of your choice. This is not the name of the S3 bucket but the ID by which the S3 bucket will be referenced in the CDK app.
Deploying the CDK app
cdk synth to synthesize the CDK app. Synth will generate a CloudFormation template for the CDK app. You can see the generated template in the
cdk deploy to deploy the CDK app. CDK will deploy the S3 bucket to your AWS account. You can see the deployed S3 bucket in the AWS Console.
You can see the deployed S3 bucket in the AWS Console.
Destroying the CDK app
To destroy the CDK app, run the following command:
However, this will not delete the S3 bucket. You have to manually delete the S3 bucket from the AWS Console. This is the default behaviour of CDK so you don't accidentally delete S3 buckets that contain important data.