Introduction
Serverless computing is completely changing how developers build and distribute applications. One of the most popular and efficient serverless cloud solutions for running code without worrying about server administration is AWS Lambda. In this blog we will explore what AWS Lambda is, Key benefits, how to create Lambda Functions and common use cases.
What is AWS Lambda?
AWS Lambda is a serverless compute service that lets you run code in response to events without provisioning or managing servers. You simply upload your code, and AWS handles the rest—from running and scaling the code to managing high availability.
Key Features of AWS Lambda
- The absence of managing servers: Write code and deploy; AWS takes care of the infrastructure.
- Event-driven: DynamoDB, API Gateway, SNS, and CloudWatch can be triggered by S3.
- Automatic scaling: Depending on demand, it may scale automatically from 0 to thousands of requests.
- Cost-effective: Pay only for the calculated time, expressed in milliseconds.
- Support for multiple languages: Python, Node.js, Java, Go,.NET, Ruby, and more.
When to Use AWS Lambda
AWS Lambda is ideal for:
- Task automation (such as file processing from S3)
- Using backend APIs without the need for servers
- Workflows are initiated by cloud events.
- Cron job replacement (planned chores)
- Real-time processing that is lightweight (IoT, analytics)
AWS Lambda Architecture Overview
- Trigger: An event source (S3 upload, HTTP request, etc.)
- Lambda Function: The logic that handles the event
- Execution Environment: AWS creates a secure, isolated environment to run your code
- Output: The result is passed back or forwarded to another service
Step by Step for Creating AWS Lambda Function
Step1: Go to AWS Lambda Console
- Go to the Services menu and click on Lambda under the Compute section.
- Click on the Create a function button.
- Choose Author from scratch
- Function name : Enter mylambdafunction
- Runtime : Select Python 3.13
3. Role: In the permissions section, click on Create a new role with basic Lambda permissions.
4. Click on the Create function button.
5. If you scroll down a little bit, you can see the Code source section.
6. Remove the existing code in AWS lambda lambda_function.py. Copy the below code and paste it into your lambda_function.py file.
def lambda_handler(event, context):
print(“Hello from Lambda!”)
return {
‘statusCode’: 200,
‘body’: ‘Lambda executed successfully!’
}
7. Save the function by clicking on the Deploy button.
Step 2: Test the Function
- Click “Test” on the Lambda page.
- Name the test event (e.g., test1) and leave the default JSON event.
- Click “Test”.
4. You should see the output:
- “statusCode”: 200
- “body”: “Lambda executed successfully!”
- And a log line: “Hello from Lambda!”
5. Understanding the Code
- lambda_handler is the entry point AWS Lambda looks for.
- event contains the event data (e.g., S3 info, API request).
- context provides runtime information (e.g., timeout, memory).
6. The function returns a JSON response with:
- statusCode – standard HTTP code
- body – your message
Step3: Triggering Lambda from S3
1. Create an S3 Bucket
Go to S3 Console → Create a bucket (e.g., lambda-trigger-bucket).
2. Add Trigger to Lambda
- Go to your Lambda function → “Add trigger”
- Choose S3
- Select your bucket and choose Event type: PUT
- Click Add
Now upload a file to your S3 bucket and see the Lambda logs
3. Viewing Logs in CloudWatch
AWS Lambda automatically sends logs to Amazon CloudWatch.
To view logs:
- Go to the CloudWatch Console
- Click Logs → Log groups
- Find /aws/lambda/mylambdafunction
4. View logs for each invocation
Best Practices for AWS Lambda
- Keep functions short and focused:
- One function = one responsibility.
- One function = one responsibility.
- Use environment variables:
- Don’t hardcode secrets or configs.
- Don’t hardcode secrets or configs.
- Enable retries carefully:
- Prevent duplicate processing.
- Prevent duplicate processing.
- Monitor with CloudWatch:
- Set alarms for errors or slow execution.
- Set alarms for errors or slow execution.
- Use layers for shared code:
- Import common libraries (like boto3 or pandas) via Lambda Layers.
- Import common libraries (like boto3 or pandas) via Lambda Layers.
- Set appropriate timeouts and memory:
- Avoid unnecessary costs or failures.
Conclusion
One of the most effective ways to create modern, scalable, and economical applications is with AWS Lambda. You can run microservices, automate processes, and react to real-time events with a few lines of code—all without having to worry about infrastructure management. We have seen how to create AWS Lambda Function using AWS SDK and trigger the function using S3 Bucket. For further posts about cloud and devops, follow DevOps Horizon.