How to use AWS CDK to deploy Python Lambda function

This article will take a quick look at how to deploy the Lambda function using AWS CDK.

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#.

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 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 Project (Cron Lambda Job)

Serverless CRON Jobs

In 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 straight forward. We’re importing 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 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!