AWS CDK & Python – Creating a CDK Project


In part 1 of the AWS CDK & Python series, which you can find here, you learned what the Cloud Development Kit (CDK) is and why it’s important in today’s world. In this blog post, you’re going to learn how to get started with the AWS CDK and how you can get a project up and running quickly.


To follow along with this blog post, you should have the following:

  • An AWS account. If you don’t have one, you can create a free tier account here.
  • Read the first blog post on in the AWS CDK & Python series, which you can find here.
  • Have programmatic access to an AWS account via an IAM role. If you don’t have one set up, you can learn how to do so here.
  • A code editor. For the purposes of this blog post, you can use Visual Studio Code (VS Code), which you can find and install for free here.
  • JavaScript installed – the AWS CDK is installed via npm, which is the JavaScript package manager. Although you will not be learning about JavaScript in this series, installing it is still needed to install the AWS CDK. You can install JavaScript here.

Installing the AWS CDK and Packages

When installing the AWS CDK, the process is straightforward. You use npm, the JavaScript package manager to bring down the library to your computer. Once the package is available, you’ll be able to run cdk commands from the terminal for things like creating a new app and deploying the app.

If you want to interact with AWS services, like S3 for example, you’ll use the Python package manager pip to do so.

In this section, you’ll learn how to do both.

Installing the AWS CDK

Open up a terminal and from the command-line, run the following command:

You should see an output similar to the screenshot below. Depending on if you already have versions of CDK installed, you may see a different output.

To confirm the AWS CDK has been installed, run the following command in the terminal to check the version.

cdk version

Depending on when you’re reading this blog post, you’ll see an output similar to the screenshot below, which is version 1.00.0. Yours may show a newer version, however.

Installing a Python Package via Pip

Now that CDK is installed, you can use the Python package manager, pip, to install a specific AWS service library that you want to use to interact with AWS.

Run the following command to install the S3 library from AWS CDK via Pip.

The aws-s3 package is now installed and you can start interacting with S3 via the CDK (which you’ll do at a later time, or you can play around with it now at your leisure)

If you’d like to take a look at the other AWS CDK Python libraries available, you can find a list of them here:

Creating a New App

The CDK is now installed along with the S3 library for AWS CDK. In this section, you’ll learn how to create an AWS CDK app and look at its contents.

The first thing you’ll want to do is create a new directory that will store the AWS CDK app. You can create the directory anywhere that suits you.

To create a new directory, run the following command.

mkdir s3app

Once you have the new directory created, open it by running the following commands to change directory into the app and open it via VS Code.

cd s3app && code .

VS Code will open and the current directory will be s3app

Inside of VS Code, create a new directory called s3 to store the AWS CDK app.

It’s now time to create the AWS CDK app. Open up the terminal inside of VS Code, cd into the s3 directory, and run the following command to create a Python app.

cdk init app --language python

You will see an output similar to the screenshot below.

Open up the s3 directory in VS Code and you will see the app, including the contents, have been created.

Browsing the New App

Now that the new app is created, you can start browsing the contents of the app. Let’s talk about what is inside of the s3 directory now that the app is created.

  • The first directory you’ll see is the .venv directory, which is the Python virtual environment for the application.
    • Next, you’ll see an s3 directory (different from the s3 directory you created previously) that contains the file to ensure Python knows the app is a module and the file, which contains the Class that you’ll modify for the code you want to run.
    • The .gitignore file is created automatically so you don’t push the virtual environment and the CDK asset staging directory to source control.
    • The [](<>) file is for running the app. Although you don’t modify this file to much because the core code that’s created is inside of the file, the file is what’s used to run the application and synthesize the app.
    • The cdk.json configuration stores the AWS CDK core libraries and the app that should be run.
    • requirements.txt stores any Python requirements that are needed, for example, the library.
    • [](<>) contains the information and description about your app, for example, the name and version of the application.
    • Finally, the source.bat is the source for activating the Python virtual environment via the .venv directory.

Running the App

Once you understand the files and configurations created for the app, it’s time to run it. As the app stands right now, there is no configuration besides the defaults. In part 3 of the AWS CDK & Python blog post series, you’ll learn how to create an S3 configuration. For right now, you’ll learn how to synthesize and deploy the default app.

From the terminal, the first command you’ll run is:

cdk synth

Once you run that command, you’ll see an output similar to the screenshot above. What the command does is looks at the Python app and prepares it for deployment via a CloudFormation stack. In the background, the AWS CDK is using CloudFormation to deploy the AWS services that are created from the app. You never have to look at the JSON code in CloudFormation.

Once the cdk synth command is ran, you can now deploy the application by running the following command:

cdk deploy

The output inside of the terminal will show that a CloudFormation stack is being created and deployed, along with the ARN of the CloudFormation stack.

To clean up the environment, run the following command to delete the resources:

cdk destroy

The environment will now be destroyed.

Closing Thoughts

The AWS CDK is a powerful tool to create, manage, update, and delete any AWS service. It allows you to never have to create resources by clicking around the UI or write a bunch of JSON or YAML configurations for CloudFormation. Instead, you can use a programming language that you’re comfortable with to deploy resources.

In part 3 of the AWS CDK & Python blog post series, which is coming up next, you’ll learn how to create a new S3 bucket configuration and deploy it to an AWS account.

Leave a Reply

Your email address will not be published. Required fields are marked *