AWS Logo
Menu

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

Generating the template

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.

Explaining the resources and recommendations

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:
  1. Parameters :
    • S3 bucket name
    • DynamoDB table name
    • Schedule expression for EventBridge
  2. IAM Roles :
    • Lambda execution role with permissions for:
      • CloudWatch Logs
      • S3 read access
      • DynamoDB write access
    • EventBridge schedule role for invoking Lambda
  3. Lambda Function :
    • Python 3.11 runtime
    • Reads from S3
    • Writes to DynamoDB
    • Includes error handling
    • Environment variables for configuration
  4. EventBridge Schedule :
    • Configurable schedule expression
    • Triggers Lambda function
    • Uses IAM role for permissions
The Lambda function:
  1. Lists objects in the S3 bucket
  2. Reads each object's content
  3. Writes the content and metadata to DynamoDB
  4. Handles errors and provides detailed logging
Important notes:
  1. Prerequisites :
    • S3 bucket must exist
    • DynamoDB table must exist
    • Table should have 'id' as the partition key
  2. Schedule Expression Examples :
    • rate(1 hour)
    • rate(5 minutes)
    • cron(0 12 * * ? *) (daily at noon UTC)
  3. Security Considerations :
    • Follows least privilege principle
    • Uses specific resource ARNs
    • Separates roles for Lambda and EventBridge
  4. Monitoring and Debugging :
    • CloudWatch Logs enabled
    • Error handling with detailed messages
    • Output values for resource ARNs
To enhance this template, you could:
  1. Add CloudWatch Alarms
  2. Include DynamoDB table creation
  3. Add S3 bucket creation
  4. Implement dead-letter queues
  5. Add custom metrics
  6. Include backup strategies
Remember to:
  1. Adjust the timeout and memory based on your needs
  2. Modify the DynamoDB schema as required
  3. Test the schedule expression before deployment
  4. Monitor the function's execution time
  5. Review CloudWatch Logs for errors

Deploying the stack

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.

Comments