I'm Sarvar, a Cloud Architect with a passion for transforming complex technological challenges into elegant solutions. With extensive experience spanning Cloud Operations (AWS & Azure), Data Operations, Analytics, DevOps, and Generative AI, I've had the privilege of architecting solutions for global enterprises that drive real business impact. Through this article series, I'm excited to share practical insights, best practices, and hands-on experiences from my journey in the tech world. Whether you're a seasoned professional or just starting out, I aim to break down complex concepts into digestible pieces that you can apply in your projects.
"Servers are like pets you feed them, nurse them, and cry when they die. Lambda functions are like cattle spin them up, use them, forget them."
🎯 Welcome Back!
Remember in )
✅ AWS credentials configured
✅ Understand VPCs and Security Groups ( /
•
📚 Series Overview
This repository contains ONLY Terraform code examples for the Terraform By Sarvar tutorial series.
⚠️ IMPORTANT: This repo contains only .tf files and infrastructure code. Articles are published on dev.to, not stored here.
📖 Read the series on dev.to:
- ✅ Published
- ✅ Published
- ✅ Published
📗 Real Infrastructure (Articles 6-10)
Getting Started
Follow these steps to run the project on your local machine:
cd terraform-by-sarvar/articles/08-lambda-serverless
🔧 Step 1: Write the Lambda Functions
Before Terraform, we need the actual code. Create a lambda/ directory:
CODE
mkdir-p lambda
lambda/hello.py - Simple API handler:
CODE
importjson
deflambda_handler(event,context): """
Simple Hello World Lambda function """ print(f"Event received: {json.dumps(event)}")
# Get name from query parameters or use default name="World" ifevent.get('queryStringParameters'): name=event['queryStringParameters'].get('name','World')
The archive_file data source creates zip files at plan time. When you change the Python code, Terraform detects the hash change and redeploys automatically.
Critical detail: The aws_lambda_permission must come before the bucket notification. The depends_on ensures this. Without it, S3 can't invoke your Lambda because the permission doesn't exist yet.
The filter_suffix = ".txt" means only .txt file uploads trigger the function. Upload a .jpg? Nothing happens. This prevents accidental infinite loops if your Lambda writes back to the same bucket.
🔧 Step 7: API Gateway
CODE
resource"aws_api_gateway_rest_api""main"{ name="${var.project_name}-api" description="API Gateway for Lambda functions"
# Test Lambda directly (bypass API Gateway)
aws lambda invoke \ --function-name terraform-lambda-hello \ --payload'{"queryStringParameters":{"name":"Test"}}'\
response.json
cat response.json
Issue 4: "AccessDeniedException" on S3
Cause: IAM policy doesn't include the bucket or action needed.
Fix: Verify the custom policy references the correct bucket ARN with /* suffix for object-level actions.
💡 Best Practices
Set source_code_hash - Without it, Terraform won't redeploy when code changes.
Use archive_file data source - Let Terraform handle zipping. Manual zip files drift.
Set retention on log groups - Default is infinite. At $0.03/GB stored, this adds up.
Timeout appropriately - API handlers: 10-30s. Processing: 30-300s. Never use the 900s max unless you know why.
Memory = CPU - Lambda allocates CPU proportionally to memory. 128MB gets minimal CPU. 1024MB gets significantly more. If your function is slow, increase memory before optimizing code.
Environment variables for config - Never hardcode bucket names, table names, or URLs in your Lambda code. Pass them through environment variables in Terraform.
🧹 Cleanup
CODE
# Remove S3 objects first (bucket must be empty)
aws s3 rm s3://$(terraform output -raw s3_bucket_name)--recursive
# Destroy everything
terraform destroy
S3 buckets can't be deleted if they contain objects. Empty it first, then Terraform handles the rest.
✅ Summary
Today you learned:
✅ Deploy Lambda functions with Terraform
✅ Package Python code with archive_file
✅ Create API Gateway endpoints
✅ Trigger Lambda from S3 uploads
✅ Apply least-privilege IAM (Article 9 skills!)
✅ Manage CloudWatch logs with retention
✅ Test functions via API and S3
The serverless mindset: Stop paying for idle servers. Lambda runs your code only when needed, scales automatically, and costs nearly nothing for typical workloads.
🚀 What's Next?
In the next article, we'll add:
Terraform modules for reusability
Package your Lambda + API Gateway as a reusable module
Share infrastructure patterns across projects
Coming Up:
🛠️ Services I Offer
If you're looking for hands-on guidance or collaboration, I provide:
Cloud Architecture Consulting (AWS / Azure)
DevSecOps & Automation Design
FinOps Optimization Reviews
Technical Writing (Cloud, DevOps, GenAI)
Product & Architecture Reviews
Mentorship & 1:1 Technical Guidance
🤝 Let’s Connect
I’d love to hear your thoughts. Feel free to drop a comment or connect with me on:
🔗
Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
SOCIAL SHARE CARD GENERATOR