Today, we are going to explore the world of serverless computing with a beginner-friendly AWS Lambda Tutorial. When I first stumbled across AWS Lambda back in 2018, I was skeptical. Coming from a traditional server-based architecture background, the idea of running code without worrying about servers seemed almost magical—and too good to be true. Fast-forward to today, and I can’t imagine building certain applications any other way.
Serverless computing has absolutely revolutionized how we deploy and scale applications. Despite the name, servers still exist (of course), but AWS handles all the infrastructure management, scaling, and maintenance. This lets us developers focus entirely on writing code that delivers business value. That’s the real game-changer.
Lambda is AWS’s cornerstone serverless compute service. It lets you run code without provisioning or managing servers. You upload your code package, and Lambda handles EVERYTHING else – from execution to scaling to high availability. You only pay for the compute time you actually consume, not a penny more!
I remember managing a fleet of EC2 instances before Lambda – constantly worrying about capacity planning, load balancing, and those dreaded 3 AM alerts when servers crashed. Lambda eliminates ALL of these headaches! 🙌
Lambda excels at numerous workloads, but these are the ones I’ve personally had the most success with:
For API backends specifically (which we’ll focus on today), Lambda paired with API Gateway creates a powerful, scalable solution that handles everything from authentication to request validation to business logic.
Before we dive in, make sure you have:
Tip 💡: If you are new to AWS, I highly recommend the AWS Learning Roadmap
Let’s start with the console approach – it’s more visual and great for beginners:
hello-world-apiYou’ll be taken to the function configuration page. In the code source section, you’ll see a default lambda_function.py file. Replace its contents with:
def lambda_handler(event, context):
name = event.get('queryStringParameters', {}).get('name', 'World')
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json'
},
'body': f'{{"message": "Hello, {name}!"}}'
}Code language: JavaScript (javascript) This function will return a greeting message, using a name from the query string if provided, or defaulting to “World”.
Click “Deploy” to save your changes.
{
"queryStringParameters": {
"name": "AWS Beginner"
}
}Code language: JSON / JSON with Comments (json) You should see a response like:
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"message\": \"Hello, AWS Beginner!\"}"
}Code language: JSON / JSON with Comments (json) Congratulations! You’ve created and tested your first Lambda function! 🎉
The console is great, but as you grow more experienced, you’ll likely want to automate deployments. Here’s how to create the same function using the AWS CLI:
mkdir hello-lambda-api && cd hello-lambda-api cat > lambda_function.py << 'EOF'
#Same code as shown previously in the AWS console stepCode language: PHP (php) zip -r function.zip lambda_function.pyCode language: CSS (css) aws iam create-role \
--role-name lambda-basic-execution \
--assume-role-policy-document '{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}'Code language: PHP (php) aws iam attach-role-policy \
--role-name lambda-basic-execution \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
aws lambda create-function \
--function-name hello-world-api-cli \
--zip-file fileb://function.zip \
--handler lambda_function.lambda_handler \
--runtime python3.12 \
--role <ROLE_ARN_FROM_STEP_4>Code language: HTML, XML (xml) aws lambda invoke \
--function-name hello-world-api-cli \
--cli-binary-format raw-in-base64-out \
--payload '{"queryStringParameters":{"name":"CLI User"}}' \
response.jsonCode language: JavaScript (javascript) cat response.jsonCode language: CSS (css) You should see an output similar to what we got in the console test.
Let’s connect our Lambda function to the outside world with API Gateway:
Note: We will only cover console-based steps for this section, as this usually needs to be done less frequently. Please refer to the API Gateway documentation if you are interested in using CLI instead.
Test your API by navigating to: https://[your-api-id].execute-api.[region].amazonaws.com/prod/hello?name=YourName
Here are some of the most common lambda issues and how to fix them:
Lambda functions have a default timeout of 3 seconds. If your function takes longer, increase the timeout in the Configuration tab.
Lambda needs permission to access other AWS services. Check your execution role if you’re getting access denied errors.
Your first invocation might be slow. This is normal! Lambda needs to initialize your function. For latency-sensitive applications, consider provisioned concurrency.
There’s a 50MB limit for direct uploads, 250MB unzipped. If you’re using large dependencies, consider layers or optimize your package.
If your API returns a 502 error, check your Lambda response format. API Gateway expects specific formatting (which we’ve used in our example).
Lambda is powerful but not perfect. Here are some limitations I’ve personally encountered:
I learned about statelessness the hard way when I tried to use the Lambda /tmp directory to cache data between invocations. It worked in testing but failed unpredictably in production when AWS recycled the execution environment. Always design for statelessness!
Once you’re comfortable with basic Lambda functions, explore these advanced concepts:
Read our post on managing and deploying lambdas effortlessly with AWS SAM, along with GitHub Actions automation
Looking back at my serverless journey, Lambda has fundamentally changed how I approach building applications. What once required complex infrastructure now needs just a few lines of code and configuration.
Is Lambda perfect? Absolutely not. Does it solve many traditional infrastructure headaches? Absolutely yes.
My biggest piece of advice: start small, experiment, and gradually migrate non-critical workloads. You’ll quickly see where Lambda shines for your specific use cases.
Remember, serverless isn’t just about technology – it’s a mindset shift. Embrace event-driven architecture, design for statelessness, and think in terms of functions rather than applications.
What could you build with Lambda today? How would removing infrastructure management change your development process? The possibilities are limitless, and the learning curve is gentler than you might think.
Happy serverless coding! 🚀
Tired of repetitive tasks eating up your time? Python can help you automate the boring stuff — from organizing files to scraping websites and sending…
Learn python file handling from scratch! This comprehensive guide walks you through reading, writing, and managing files in Python with real-world examples, troubleshooting tips, and…
You've conquered the service worker lifecycle, mastered caching strategies, and explored advanced features. Now it's time to lock down your implementation with battle-tested service worker…
This website uses cookies.