AWS CDK and Python | Step by Step Tutorial

Introduction

In this article, we will show you how to get started with AWS CDK and its new Python bindings to create AWS infrastructure.

AWS Cloud Development Kit uses AWS CloudFormation as engine to build and provision AWS resources. It gives you flexibility to define your architecture using any of the imperative programming language of your choice. It includes languages such as TypeScript, Java, C#, and now Python too!

You can now deploy new resources, make changes to existing infrastructure, or clean up resources using CDK similar to working with AWS CloudFormation with the concept of Infrastructure as Code (IaC).

In this example, we will be creating a S3 bucket using AWS CDK and Python. So let’s get started right away!

Prerequisites

  • NPM (easiest way to install AWS CDK)
  • PIP (as we are using python and we will need it to install some python dependencies)
  • AWS CLI (as CDK libraries rely on it in order to deploy its resources)

See how to install AWS CLI on Mac and Windows.

Installing AWS CDK

Once you have npm installed, use command below to install cdk globally:

npm install -g aws-cdk
AWS CDK: Installation

Once installed, you can use cdk –help to find more useful commands. See some of them listed below:

  • cdk ls to list all the stacks in the application.
  • cdk synth to generate CloudFormation template of your code.
  • cdk deploy to deploy resources via CloudFormation stack.
  • cdk destroy to clean up all resources created.
  • cdk diff to see the difference between the new code generated and existing code present on AWS.

Creating Project using AWS CDK

To generate a sample project in Python, run the command below:

cdk init sample-app –language python

This command will provision all the project files that you will need for the corresponding language. 

AWS CDK: Creating Project

Modify the default files created above according to your specific use-case.

Some of the important files generated includes:

  • The source.bat or source file runs a command to setup your virtual environment, to be able to install all package dependencies specific to this project.
Sample Project: Files
  • The setup.py file has setup parameters, mostly used is install_requires section which allows you to list any dependencies needed to be installed.
Sample Project: Files
  • The cdk.json file defines configuration for your cdk project.
Sample Project: Files
  • The app.py file is the entry-point for your cdk application. Typically, a project will have only one app and it is the top-level grouping of all the stacks in your project. This file will also outline all the stacks under the app.
Sample Project: Files
  • The stack file (in my case, it is cdk_getting_started_stack.py) contains init function that contains resources we are creating.
    • Modifying this file to my specific use-case
      • Importing dependency aws_s3 (line 6)
      • Calling aws_s3 to create S3 bucket with given name (line 16)
Sample Project: Files

Installing Requirements

Now, we have our stack code modified. Let’s create a virtual environment and install all dependencies.

Run source or source.bat file to activate our virtual environment using the command below:

.\source.bat
Creating Virtual Environment

Once the virtual environment is activated, install dependencies by running the command below: