
Q-Bits : Setting Up Lambda Functions in CDK
Infrastructure as Code (IaC) and AI-assisted development - I will explore creating a simple Lambda function that integrates with an API Gateway and deployed using AWS CDK leveraging the power of Amazon Q Developer.
Deep Roy
Amazon Employee
Published Feb 24, 2025
Last Modified Mar 2, 2025
Welcome to another installment of Q-Bits, our regular series showcasing cool ways Amazon employees are leveraging Amazon Q Developer. In this article, we'll explore how Amazon Q enhances developer productivity, provides contextual recommendations, and helps maintain AWS best practices when deploying an AWS Lambda function with AWS CDK in Python. Amazon Q Developer aids in generating Lambda function constructs, explaining code asset bundling, demonstrating integration with API Gateway, and providing examples of Lambda event source mappings.
Introduction
Infrastructure as Code (IaC) has transformed how we build and manage AWS cloud resources programmatically. AWS Cloud Development Kit (CDK) automates AWS resource management using modern programming languages. Now, with Amazon Q Developer, we're witnessing another leap forward in Infrastructure development and DevOps productivity. Let's explore how Amazon Q can assist in CDK development. I have enabled the Amazon Q in VS Code IDE and the screenshots are taken from VS Code.
Prerequisites
To get started you will need the following:
- An AWS Account
- AWS CLI configured with your credentials
- An IDE for your programming language, in this case I am using Visual Studio Code
- Amazon Q Developer plugin
- Python 3.7 or later
Streamline Code Generation
One of the primary ways Amazon Q enhances your CDK workflow is through the generation of sample/generic code for CDK constructs. At first, I would prompt Q “Help me write a code for a sample Lambda function that integrates with AWS API Gateway. The code should follow AWS CDK constructs and written in python. Show me detailed steps of initializing CDK environment”

Amazon Q developer lists the steps, including suggesting a local directory creation and CDK initialization in Python. It further lists installation of dependencies and required packages including bootstrapping and deploying the code.




With this comprehensive recommendation, I just quickly reviewed suggested code and customized it a bit to deploy the Lambda function using CDK. Here is the Lambda stack and
app.py
generated by Amazon Q Developer.

To ensure the code is aligned to best practices and scan for any vulnerabilities, I asked Amazon Q to review the code using
/review
prompt at the workspace level:
In the above, you can see Amazon Q flagged a High severity issue with the Lambda function code of not having tracing enabled and also suggests a quick fix. I accepted the quick fix and the following is the resultant code:

I went ahead and deployed the changes using
cdk deploy
as suggested by Amazon Q Developer.

A quick verification of the completion status of the resources in my AWS account specified during bootstrapping.

Verification
To verify, I use
curl
to issue a GET request to the API Gateway endpoint that is in front of the Lambda function and expect a hello response.Conclusion and Cleanup
In the above example, you saw how powerful an ally Amazon Q Developer can be in your CDK development workflow. It significantly enhances the developer experience by:
- Generating code aligned with best practices
- Providing real-time code review function to optimize generated code
- Offering contextual troubleshooting assistance
- End to end deployment recommendations
To cleanup, you can run
cdk destroy
to delete all the resources that are created.In conclusion, Amazon Q Developer can help elevate your development experience. Happy coding!
Have you tried using Amazon Q for your CDK development? Share your experiences in the comments below!
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.