Edit Template

AWS Lambda Basics: Run Your First Function Fast

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

  1. Trigger: An event source (S3 upload, HTTP request, etc.)
  2. Lambda Function: The logic that handles the event
  3. Execution Environment: AWS creates a secure, isolated environment to run your code
  4. 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
  1. Go to the Services menu and click on Lambda under the Compute section.
  2. Click on the Create a function button.
    1. Choose Author from scratch
    2. Function name : Enter mylambdafunction
    3. 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
  1. Click “Test” on the Lambda page.
  2. Name the test event (e.g., test1) and leave the default JSON event.
  3. 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

  1. lambda_handler is the entry point AWS Lambda looks for.
  2. event contains the event data (e.g., S3 info, API request).
  3. 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:

  1. Go to the CloudWatch Console
  2. Click Logs → Log groups
  3. Find /aws/lambda/mylambdafunction

4. View logs for each invocation

Best Practices for AWS Lambda

  1. Keep functions short and focused:
    • One function = one responsibility.
  2. Use environment variables:
    • Don’t hardcode secrets or configs.
  3. Enable retries carefully:
    • Prevent duplicate processing.
  4. Monitor with CloudWatch:
    • Set alarms for errors or slow execution.
  5. Use layers for shared code:
    • Import common libraries (like boto3 or pandas) via Lambda Layers.
  6. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Recent Posts

Category

content created for you!

Company

About Us

FAQs

Contact Us

Terms & Conditions

Features

Copyright Notice

Mailing List

Social Media Links

Help Center

Products

Sitemap

New Releases

Best Sellers

Newsletter

Help

Copyright

Mailing List

© 2023 DevOps Horizon