How to use AWS CDK to deploy Python Lambda function

Andrei Maksimov

Andrei Maksimov


The AWS Cloud Development Kit (AWS CDK) is an open-source development framework to model and provision your cloud application resources using popular programming languages like Typescript, Javascript, Python, Java, C#. This article will take a quick look at how to deploy the Lambda function using AWS CDK.

Every AWS resource in AWS CDK is implemented in a module called “construct.” The construct is a building block in your code, which may is used to declare VPC, Subnet, Security Group, Lambda function, DynamoDB, and other AWS resources.

Here’s a great a bit more in-depth explanation of What is the AWS CDK.

I personally like AWS CDK because:

  • AWS CDK constructs defaulted to AWS best practices – for example, if you declare a VPC without any parameters, AWS CDK will use the latest AWS best practices recommendations to build the VPC and all related infrastructure.
  • I can prototype any infrastructure in minutes.
  • In the end, I can get a CloudFormation template that may be used independently without AWS CDK.

As I’m a big fan of Python, we’ll concentrate on this beautiful programming language.

Before we begin, several must-know resources:

Example CDK Python project (Cron Lambda Job)

Serverless CRON Jobs

In the Cloud CRON – Scheduled Lambda Functions article, we covered how to create a Serverless Cron job using CloudFormation and Terraform. Let’s build the same functionality (the Cron job to delete outdated AMIs), but with the help of AWS CDK.

Source code

I’m assuming that you already installed AWS CDK. If not, here’s how to install it.

Here’s our project structure:

├── cdk.json
└── requirements.txt

0 directories, 4 files
  • – our Lambda function code from the previous example.
  • requirements.txt – required constructs, which we need to build an example.
  • cdk.json – AWS CDK runtime context configuration.
  • – AWS CDK application module where we’re describing infrastructure.

Now, let’s take a look at the files one by one.

requirements.txt file contains the following content:

Those dependencies allow you to use AWS CloudWatch Events and AWS Lambda constructs in your AWS CDK application code.

from aws_cdk import (
    aws_events as events,
    aws_lambda as lambda_,
    aws_events_targets as targets,

class LambdaCronStack(core.Stack):
    def __init__(self, app: core.App, id: str) -> None:
        super().__init__(app, id)

        with open("", encoding="utf8") as fp:
            handler_code =

        lambdaFn = lambda_.Function(
            self, "ami-cleanup-lambda",

        rule = events.Rule(
            self, "Rule",

app = core.App()
LambdaCronStack(app, "Lambda-Cron-Delete-AMIs")

The code itself is pretty straightforward. We’re importing the required modules, and declaring the Python class, which contains the description of Lambda function and CloudWatch Events.

cdk.json contains default context for python CDK apps:

    "app": "python3"


To deploy the AWS CDK application, you need to install the virtual environment will require python modules first:

python3 -m venv .env
source .env/bin/activate
pip install -r requirements.txt

Now you’re ready to deploy the application:

cdk deploy

As soon as you changed something in your application, you may deploy your changes again using the same command.

That’s it! So simple. Maybe you do not see all the benefits of using AWS CDK from this simple example, but the amount of code you’re writing is significantly less for larger projects.

To destroy your changes simply run:

cdk destroy


In this article, we took a look at the ability to use AWS CDK to deploy Lambda functions. I hope this article will save you some amount of time.

If you find this article useful, please, help to spread it to the world!

How useful was this post?

Click on a star to rate it!

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Want to be an author of another post?

We’re looking for skilled technical authors for our blog!

Leave a comment

If you’d like to ask a question about the code or piece of configuration, feel free to use or a similar tool as Facebook comments are breaking code formatting.