Skip to content

AWS Lambda in Python

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.


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:

  1. Open the AWS Management Console and navigate to the AWS Lambda service.
  2. Click on the “Create function” button to create a new Lambda function.
  3. Choose “Author from scratch” as the function template.
  4. Give your function a name and choose “Python 3.x” as the runtime.
  5. 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.
  6. 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:

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:

  1. In the AWS Lambda console, click on the “Deploy” button to open the deployment wizard.
  2. 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.
  3. Choose the appropriate “Execution role” for your function. This role determines the permissions that your function has to access other AWS resources.
  4. 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:

  1. In the AWS Lambda console, click on the “Test” button to open the test configuration editor.
  2. 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 an age field.
  3. Click the “Create” button to create the test event.
  4. Click the “Test” button to run

Leave a Reply

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