Q-Bits: Setting Up Lambda Functions in CloudFormation with Amazon Q Developer
Discover how Amazon Q Developer can help with generating Lambda function resources in CloudFormation for rapid development and deployment
Harish Vaswani
Amazon Employee
Published Jan 28, 2025
Welcome to another installment of Q-Bits, our regular series showcasing cool ways Amazon employees are leveraging Amazon Q Developer. Today, we're diving into how Q Developer can assist with generating Lambda function resources in AWS CloudFormation.
Writing Infrastructure as Code (IaC) with AWS CloudFormation is something I always do when creating serveless applications. However, the manual process of writing them can be tedious and error prone. I often find myself spending more time in fixing things like indentation issues or missing dependent resources. I've been using Amazon Q Developer now to help me generate a template that I can quickly use and deploy to create the infrastructure I need.
This article focuses on how one could use Amazon Q Developer for generating AWS Lambda function resources in CloudFormation. I will show how Q can be used to:
- Generate the template
- Explain the resources
- Assist with deployment
My application is simple. I have a need for a Lambda function that gets triggered by a scheduled event and needs to read objects from an Amazon S3 bucket and write items to an Amazon DynamoDB table.
For example, I can prompt Q by simply saying:
Create a CloudFormation template for a Lambda function. The function needs to be triggered from a scheduled EventBridge event and needs to read objects from an S3 bucket. It also needs permissions to write to a DynamoDB table.
Based on the prompt above, Q generated this template:
As seen above, Q generated all the resources needed, as a starting point, for what I need the Lambda function to do. It also generated code for my Lambda function to read objects from my S3 bucket and write data to the DynamoDB table. Of course, I may need to customize this and move the code to a separate Python file but this saves me a ton of time as compared to writing this template manually.
As part of Q's output, it also explained the generated code for the resources and suggested improvements I could make:
Key components of this template:
- Parameters :
- S3 bucket name
- DynamoDB table name
- Schedule expression for EventBridge
- IAM Roles :
- Lambda execution role with permissions for:
- CloudWatch Logs
- S3 read access
- DynamoDB write access
- EventBridge schedule role for invoking Lambda
- Lambda Function :
- Python 3.11 runtime
- Reads from S3
- Writes to DynamoDB
- Includes error handling
- Environment variables for configuration
- EventBridge Schedule :
- Configurable schedule expression
- Triggers Lambda function
- Uses IAM role for permissions
The Lambda function:
- Lists objects in the S3 bucket
- Reads each object's content
- Writes the content and metadata to DynamoDB
- Handles errors and provides detailed logging
Important notes:
- Prerequisites :
- S3 bucket must exist
- DynamoDB table must exist
- Table should have 'id' as the partition key
- Schedule Expression Examples :
rate(1 hour)
rate(5 minutes)
cron(0 12 * * ? *)
(daily at noon UTC)
- Security Considerations :
- Follows least privilege principle
- Uses specific resource ARNs
- Separates roles for Lambda and EventBridge
- Monitoring and Debugging :
- CloudWatch Logs enabled
- Error handling with detailed messages
- Output values for resource ARNs
To enhance this template, you could:
- Add CloudWatch Alarms
- Include DynamoDB table creation
- Add S3 bucket creation
- Implement dead-letter queues
- Add custom metrics
- Include backup strategies
Remember to:
- Adjust the timeout and memory based on your needs
- Modify the DynamoDB schema as required
- Test the schedule expression before deployment
- Monitor the function's execution time
- Review CloudWatch Logs for errors
Finally, Q also generated a AWS CLI command to deploy this stack:
Having Amazon Q Developer assist me in generating CloudFormation templates like these for Serverless applications has made my day-to-day builder experience much more efficient.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.