Deploying Lambda Functions with AWS CDK Python
This is the way!

Infrastructure as code (IaC) enables management and provisioning of infrastructure through machine-readable definition files. IaC provides consistency by having files as descriptive models be the single source of truth.
For cloud providers such as AWS, CloudFormation is the IaC enabler. CloudFormation helps model resources by describing them in templates that can be deployed as stacks. It provides the ability to easily build, manage, change, and destroy resources in your infrastructure through resource definition files.
Unfortunately, despite all the advantages CloudFormation bring to the table, designing complex infrastructures using CloudFormation templates becomes a tedious process with manual actions and long yaml/json files that are difficult to review and maintain.
That is where AWS Cloud Development Kit (AWS CDK) comes in. AWS CDK is a software development framework to define your cloud application resources using familiar programming languages.
AWS CDK provides high-level components called constructs that preconfigure cloud resources with proven defaults. Under the hood, it enables the provision of resources in a safe, repeatable manner through AWS CloudFormation.
Let’s see how simply it is to provision an AWS resource such as Lambda using AWS CDK.
Building with AWS CDK
The AWS CDK development workflow is similar to the workflow already used by developers, with the exception of a few extra steps to synthesize your stack to an AWS CloudFormation template and deploy it.
Steps to build and deploy your AWS CDK implementation:
- Create the app from a template provided by the AWS CDK.
- Add code to the app to create resources within stacks.
- Synthesize one or more stacks in the app to create an AWS CloudFormation template.
- Deploy one or more stacks to your AWS account.
Prerequisites
To work with the AWS CDK, you must have an AWS account and credentials. Additional you need to install Node.js and the AWS CDK Toolkit.
Python AWS CDK applications require Python 3.6 or later. If you don’t already have it installed, download a compatible version.
The Python package installer, pip, and virtual environment manager, virtualenv, are also required.
Initialize AWS CDK
Once the requirements are met, we can start by creating the App.
mkdir demo-python-cdk
cd demo-python-cdkPlease keep in mind that you need an empty directory in order to initialize cdk.
Now initialize the app using the cdk init command, specifying the desired template (“app”) and programming language.
cdk init app --language pythonThe cdk init command generates a number of files and folders inside the demo-python-cdk directory to help you organize the source code for your AWS CDK app.
After initializing the project, activate the project’s virtual environment. This allows the project’s dependencies to be installed locally in the project folder, instead of globally.
source .venv/bin/activateInstall the app’s standard dependencies:
python -m pip install -r requirements.txtManaging AWS Construct Library modules
Use the Python package installer, pip, to install and update AWS Construct Library modules for use by your apps, as well as other packages you need.
python -m pip PIP-COMMANDThe AWS CDK core module is named aws-cdk.core. AWS Construct Library modules are named like aws-cdk.SERVICE-NAME. For instance, let’s install 2 services that will be needed by our application. The command below installs the modules for AWS IAM and AWS Lambda.
python -m pip install aws-cdk.aws-lambda aws-cdk.aws-iamCreating Lambda Stack
Now let’s start creating our Lambda stack.
To start with a clean project I normally delete the folder and content automatically created by cdk. Delete the folder demo_python_cdk.
Create a folder called lambda and add a file lambda_code.py with the content:






