If you want to learn how to deploy an AWS Lambda function using the AWS Cloud Development Kit (CDK) in Python, you’ve landed in the right place! In this post, I’ll walk you through the prerequisites, setup, and exact steps you need to follow. I’ll also share some code snippets and give a reference to my YouTube video tutorial for better understanding.
🎥 Watch the complete tutorial on YouTube here: AWS CDK Tutorial in Python | Deploy Lambda from S3
Before we get started, make sure you have the following ready:
Programming Language Setup – Install Python (preferably 3.10+).
Free-tier AWS Account – Sign up at AWS.
Node.js and NPM Installed – Required for CDK installation.
Steps to Deploy Lambda with AWS CDK
Step 1) Configure AWS CLI: Run below command on CMD and provide your AWS Access Key, Secret Key, Region and output format.
aws configure
Step 2) Install AWS CDK: Run below NPM command to install AWS SDK globally:
npm install -g aws-cdk
Step 3) Verify installation: Run the below command in CMD and if it is giving correct response then installation is complete:
cdk --version
Step 4) Create a New CDK Project: Go to the folder where you want to create your project and run the below command in CMD to create CDK project structure in Python
cdk init app --language python
Step 5) Open Project in VSCode: Navigate into your project directory and open it in Visual Studio Code.
Step 6) Add Lambda Deployment Code: In your Stack file, add the infrastructure code as follows:
from aws_cdk import (
Duration,
Stack,
aws_lambda as _lambda,
aws_lambda,
aws_s3 as s3,
aws_logs as logs,
RemovalPolicy,
aws_iam as iam,
aws_scheduler as scheduler,
aws_apigateway as apigwv,
CfnOutput
)
from constructs import Construct
from typing import cast
class Hello_Lambda_CDK_Stack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
bucket_name = "jitendra-lambda-bucket"
object_key = "mylambda.zip"
# Define a log group for the Lambda function
log_group = logs.LogGroup(
self, f"HelloLambdaFN-LogGroup-ID",
log_group_name=f"/aws/lambda/HelloLambdaFN-LogGroup",
removal_policy=RemovalPolicy.DESTROY,
retention=logs.RetentionDays.THREE_DAYS
)
# Define a Lambda function resource here
my_lambda = _lambda.Function(
self, f"HelloLambdaFN-ID",
runtime=_lambda.Runtime.PYTHON_3_13,
handler="lambda_function.lambda_handler",
code=_lambda.Code.from_bucket(
bucket = s3.Bucket.from_bucket_name(
self, f"HelloLambdaFN-Bucket-ID",
bucket_name
),
key = object_key
),
log_group=log_group,
timeout=Duration.seconds(30),
memory_size=128,
function_name="HelloLambdaFN"
)
# Add permissions for the Lambda function to write to the log group
log_group.grant_write(my_lambda)
# Define an API Gateway HTTP API to trigger the Lambda function
apigatewayobj = apigwv.LambdaRestApi(
self, "HelloLambdaFN-API-Gateway",
rest_api_name="HelloLambdaFN-API",
handler=cast(aws_lambda.IFunction, my_lambda),
proxy=True
)
CfnOutput(
self, "APIEndpoint",
value=apigatewayobj.url,
description="The endpoint URL of the HelloLambdaFN API Gateway",
)
Step 7) Bootstrap AWS Environment: This prepares your AWS account to use CDK.
cdk bootstrap
Step 8) Build Your Application (Optional): For Python, explicit build isn’t needed.
Step 9) Synthesize CloudFormation Template: This generates the underlying CloudFormation template and highlight any error if there in your infrastructure code.
cdk synth
Step 10) Deploy the Stack: Run the below command in CMD and it will start creating your AWS infrastructure as per your code in step by step. Before final deployment, it will ask will confirmation and if you confirm then it will start final deployment:
Deploy the Stack
Step 11) Delete the Application: Now, if you want to delete your deployed stack, then you can delete it by running the below CDK command:
cdk destroy