AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. You can use AWS Lambda to run your code in response to events such as changes to data in an Amazon S3 bucket, a new row added to a DynamoDB table, or a scheduled event. In this tutorial, we will show you how to write an AWS Lambda function in Python.
Prerequisites
Before we get started, make sure you have the following prerequisites:
- An AWS account
- AWS CLI installed and configured on your local machine
- Basic knowledge of Python
Creating an AWS Lambda Function
To create an AWS Lambda function, follow these steps:
- Open the AWS Management Console and navigate to the AWS Lambda service.
- Click on the “Create function” button to create a new Lambda function.
- Choose “Author from scratch” as the function template.
- Give your function a name and choose “Python 3.x” as the runtime.
- Under “Permissions,” choose “Create a new role with basic Lambda permissions” to automatically create an IAM role that allows the Lambda function to write logs to Amazon CloudWatch Logs and access other AWS services.
- Click the “Create function” button to create your new Lambda function.
Writing the Python Code
Now that you have created a new Lambda function, it’s time to write the Python code that will be executed by the function. In the AWS Lambda console, click on the “Function code” tab to access the code editor.
Here’s a simple Python function that returns a greeting message:
1 2 3 4 5 6 7 8 | import json def lambda_handler(event, context): name = event['name'] age = event['age'] message = f"Hello, {name}! You are {age} years old." return { 'statusCode': 200, 'body': json.dumps(message) } |
The lambda_handler
function takes two arguments: event
and context
. The event
argument is a dictionary that contains input data for the function. In this example, the input data is expected to have a name
and an age
field. The context
argument provides information about the execution environment of the function.
The function creates a greeting message by formatting the name
and age
values, and returns a JSON object with a statusCode
of 200 and a body
that contains the message.
Deploying the Function
Now that you have written your Python code, it’s time to deploy the function to AWS Lambda. Here’s how:
- In the AWS Lambda console, click on the “Deploy” button to open the deployment wizard.
- Choose the deployment package that you want to upload. For Python functions, the deployment package should be a ZIP file that contains your Python code and any necessary dependencies.
- Choose the appropriate “Execution role” for your function. This role determines the permissions that your function has to access other AWS resources.
- Click the “Create” button to deploy your function to AWS Lambda.
Testing the Function
To test your AWS Lambda function, you can use the built-in test functionality in the AWS Lambda console. Here’s how:
- In the AWS Lambda console, click on the “Test” button to open the test configuration editor.
- Give your test event a name and add the necessary input data for your function. In this example, the input data should be a JSON object with a
name
and anage
field. - Click the “Create” button to create the test event.
- Click the “Test” button to run