AWS CDK with Python — 3

Pratik
7 min readAug 11, 2023

--

First CDK App

Previous part

AWS CDK allows you to write infrastructure code using common programming languages. But can you use CDK with all languages out there?

Well, although the answer would be ‘Yes’ technically, only some of them have first-class support from CDK. Currently, these are: Python (version 3.6 or greater), Typescript (version 3.8 or later) , Javascript & Java (8 or above). The CDK Toolkit has a Node.js backend, so it doesn’t require additional prerequisites. You can use C#, which requires .NET Core version 3.1 or later . And lastly, Go is the most recent language fully supported by CDK and it requires standard Go toolchain version 1.18 or later.

We will use Python. Now, there are differences in the Python executable in operating systems. On Windows, the executable is ‘python’. So, you should use the ‘python’ command on Windows. But on Mac and Linux, the python command without any versions like this mostly corresponds to Python version 2, which is often used by the operating system. On these operating systems, you use ‘python3’ for the latest Python version 3 instead. You can check the Python version by ‘python — version’ command.

We will also need the Python package manager ‘pip’ to install the CDK library and other required packages. You can check pip version using ‘pip — version’ command

This may not be the latest pip version available because pip is managed as a separate package from your Python installation. So, to upgrade you can run following command.

CDK APP

Before creating a CDK app you need to create an empty folder with a name that will also be your new application’s name. So, let’s use the ‘mkdir’ command to create a new folder for our first CDK app and then provide its name as ‘my_first_cdk_app’ in snake_case as a convention.

mkdir my_first_cdk_app && cd my_first_cdk_app

CDK init

So, how do you create a new CDK application in this empty project folder? Well, you use CDK Toolkit’s ‘cdk init’ command. This command uses application templates provided by CDK to initialize your application folder to simplify your start. But please don’t confuse these templates with CloudFormation templates. They are just for initializing new CDK apps. The ‘cdk init’ command gets the name of the template you will use to initialize your CDK app as the first input.

If you run it without inputs or options it lists the application template choices available.

The first one is the ‘app’ template, which creates an empty CDK app with no constructs. We will use the ‘app’ template in the following sections.

The second option is the ‘lib’ template, which helps you initialize a CDK app that will be published as a custom construct to the Construct Hub.

And the last one is the ‘sample-app’ template, which creates a sample CDK application with some simple constructs defined. It is a helpful template to start learning CDK. So, let’s use it.

Let’s find the ‘cdk init’ command again and provide the application template we will use, ‘sample-app’, as the first input. Next, you must provide the programming language you want to use in your CDK app using the ‘ — language’ option. You can provide its value using an equals sign as the assignment operator or just after spaces. The language names are standard. For example, if you want to use Java, you can provide ‘java’ in lowercase. Or, for Python, the programming language used in this course, you type ‘python’ in lowercase. Please refer to the AWS CDK Toolkit reference for others. That’s it! Let’s press Enter.

cdk init sample-app --language python

Once the command is finished, it initialized our CDK app with new files and folders under my_first_cdk_app folder. You can open this folder structure in VS code by typing ‘code .’ in terminal.

First, we have the ‘.venv’ folder. Now, the ‘cdk init’ command creates a Python virtual environment to isolate your application’s Python version from your operating system to avoid any version conflicts between them in the future. So this ‘.venv’ folder is created for your CDK application’s virtual Python environment.

It contains the binaries to run Python or other packages like pip. And when you install a Python library after activating this virtual Python environment, they are installed under the ‘lib’ folder here to provide isolation. We will discuss how to activate it in a minute. Let’s continue.

Then, as you see, there is a folder with the same name as your CDK app. It is a Python package that encapsulates your CDK application’s code. It contains a Python module for your first CDK stack, whose name is also derived from the CDK app’s name by appending ‘stack’ to it, ‘my_first_cdk_app_stack’ in snake_case.

A CDK application can have multiple stacks for the CloudFormation stacks deployed. So, this is our first and only stack in our sample application.

As you see, it imports the ‘constructs’ and ‘aws_cdk’ libraries. These are the official CDK libraries used to define your CDK components, which we will also install into our virtual Python environment in a minute.

Then, there is a Python class definition(class MyFirstCdkAppStack) for our first CDK stack in PascalCase as a convention. And it has a standard Python ‘init’ method to initialize the objects from this class. A CDK stack is where you define your CDK constructs, which also correspond to one or more AWS resources deployed. And you place them in the ‘init’ method. So, whenever you instantiate a stack object from this class, it will contain the constructs defined here. So, we see our first construct definition for an SQS Queue. Then, we have another construct for an SNS topic. And finally, it subscribes the SQS queue to the SNS topic using the topic’s ‘add_subscription’ method.Please don’t worry about the details of these constructs. We will discuss them in more detail in the following parts.

Let’s continue with others.

The ‘tests’ folder is a Python package encapsulating all testing-related packages and modules. It has a sub-folder and package for unit tests with a Python module containing sample tests for our first stack.

Then, there is a standard ‘.gitignore’ file listing the files and folders to be ignored in Git commits.

And next, we have the ‘app.py’ file, the main of our CDK app.

As in the stack, it imports the official AWS CDK library as well as our first CDK stack’s class from its module. It defines a CDK app object as the root of our CDK application and instantiates a stack from the stack class we saw before in this app’s scope. In simple words, it defines your CDK app and your first CDK stack in it. And finally, it calls the ‘synth’ method of the CDK app to synthesize CloudFormation templates and other deployment assets from it. So, this is your CDK app’s main file.

After that, you see the ‘cdk.json’ file containing your CDK app’s settings in JSON. The defaults are OK for most scenarios.

And there is also a ‘README’ file describing this app which was also printed to the terminal after initializing it.

Next, the ‘requirement’ files contain the Python library dependencies for this app. The ‘requirements-dev’ text file is specific to development environments, which only contains the ‘pytest’ library by default, the Python library for testing. And the ‘requirements.txt’ file contains the dependencies for all environments, including production.

And the last file, ‘source.bat’, is a Windows batch file used to activate the Python virtual environment on Windows. So, this is the project structure.

Install requirements:

However, our CDK app is still not ready. To work with a CDK application, you must install the dependencies first. We won’t cover testing in this section, so let’s skip the development dependencies. But we must install the general dependencies defined in the requirements file that include the official CDK libraries. But to do this, we must activate our CDK app’s virtual Python environment beforehand because otherwise, they will be installed globally.

So, let’s open the terminal again.To activate your virtualenv on a Linux or MacOs platform:

source .venv/bin/activate

On a Windows platform, you would use this:

.venv\Scripts\activate.bat

Now that the virtual environment is activated, you can safely install the required python modules.

pip install -r requirements.txt

As the virtual environment was active, pip installed them under the ‘lib’ folder of the project’s ‘.venv’ folder.This is how to initialize a Python CDK app using the ‘sample-app’ application template.

Next Part

--

--