Create Your First AWS Lambda Function | AWS Tutorial for Beginners

3 min read 6 hours ago
Published on Dec 12, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, you'll learn how to create your first AWS Lambda function using the AWS Management Console. AWS Lambda is a serverless computing service that allows you to run code in response to events without managing servers. This guide will walk you through the process of creating a Lambda function in Python 3.9, triggered by a file upload to an S3 bucket, and will also cover how to delete the resources once you're done.

Step 1: Understand AWS Lambda and Its Benefits

  • What is serverless computing?

    • Serverless computing allows developers to build and run applications without managing server infrastructure. You focus on writing code, while AWS handles server management.
  • Benefits of using AWS Lambda:

    • Cost-effective: You pay only for the compute time you consume.
    • Scalability: Automatically scales your application by running code in response to events.
    • Simplified management: No need to provision or maintain servers.

Step 2: Create an S3 Bucket

  • Log in to the AWS Management Console.
  • Navigate to the S3 service.
  • Click on "Create bucket."
  • Configure the bucket settings:
    • Bucket name: Choose a unique name.
    • Region: Select the nearest region.
  • Click "Create bucket" to finalize the setup.

Step 3: Create an AWS Lambda Function

  • Go to the AWS Lambda service in the Management Console.
  • Click on "Create function."
  • Choose "Author from scratch."
  • Configure the function settings:
    • Function name: Enter a unique name for your function.
    • Runtime: Select Python 3.9.
  • Click "Create function" to create the Lambda function.

Step 4: Write and Deploy the Lambda Code

  • In the Lambda function console, scroll down to the code editor.
  • Copy and paste the following example code to process S3 file uploads:
import json
import boto3

def lambda_handler(event, context):
    # Process the file upload
    s3 = boto3.client('s3')
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']
    
    print(f'Received file {key} from bucket {bucket}')
    return {
        'statusCode': 200,
        'body': json.dumps('File processed successfully')
    }
  • Click on "Deploy" to save and deploy the Lambda function.

Step 5: Create a Trigger for the Lambda Function

  • In the Lambda function console, select "Add trigger."
  • Choose "S3" from the list of services.
  • Configure the trigger settings:
    • Bucket: Select the S3 bucket you created.
    • Event type: Choose "PUT" to trigger the function on file uploads.
  • Click "Add" to establish the trigger.

Step 6: Manage Events and Permissions

  • Ensure that your Lambda function has the necessary permissions to access S3:
    • Navigate to the IAM role associated with your Lambda function.
    • Attach the policy AmazonS3ReadOnlyAccess to allow reading from the S3 bucket.

Step 7: Test the Lambda Function

  • Upload a test file to your S3 bucket.
  • Monitor the execution of your Lambda function by going to the CloudWatch service:
    • Check CloudWatch Logs to see the output from your Lambda function and confirm it's working as expected.

Step 8: Delete Resources

  • To clean up resources, you can delete the Lambda function and S3 bucket:
    • Go to the Lambda console, select your function, and click "Delete."
    • Navigate to the S3 console, select your bucket, and delete it.

Conclusion

Congratulations! You've successfully created an AWS Lambda function that processes file uploads from an S3 bucket. This tutorial covered the basics of serverless computing, setting up an S3 bucket, creating a Lambda function, writing and deploying code, setting triggers, and cleaning up resources. For further exploration, consider looking into additional AWS services or diving deeper into AWS Lambda features.