Menu Close

How to Create an AWS Lambda Function for Python

How to create an aws lambda function for python

In this article, we will see Step by step guide to create an AWS lambda function for Python. This tutorial will just focus on creating a simple aws lambda function with the help of Python runtime so that you can have an understanding of the AWS lambda function. In later articles, we will explore some advanced concepts of the AWS lambda function with the help of examples.

Before creating a lambda function in Python, let’s understand some important concepts about the AWS lambda function by definition.

What is AWS Lambda?

AWS lambda is a serverless computer service that runs your code in response to events and it manages all the underlying resources for you.

AWS lambda runs your code on high-availability compute infrastructure and performs all the administration of computer resources including server and operating system maintenance, capacity provisioning, automatic scaling, and logging.

With AWS Lambda, You have to supply your code to one programming language that AWS Lambda support.

Lambda Runtimes:

AWS lambda supports multiple programming languages and these languages are called runtimes in AWS lambda. You can use runtimes that are provided by the AWS lambda itself or your can build your own runtimes also.

You can below picture which are runtimes supported by AWS lambda.

Create an AWS Lambda Function for Python

What is AWS Lambda Function?

The lambda function is the principal resource of the AWS lambda service. You will organize your code into a function and aws will run your function only when it needs and scale automatically. You will be charged when your code is consumed. There is no charge when you code it not running. You can configure your function using AWS Management Console, Lambda API, etc.

Let’s see how can we create an AWS lambda function.

Prerequisites

If you do not have an AWS account, Please follow the below-given links to create an AWS account, An IAM User, and assign Lambda access to that IAM user.

Steps to Create an AWS Lambda Function for Python

Now, I am assuming you have created AWS free AWS free account, an IAM user, and also assigned lambda access to that IAM user. Now follow the following steps in order to create the AWS lambda function for Python runtime.

  • Login into the AWS management console with an IAM user. The following credentials are required to login into the AWS management console with an IAM user.
    • Account ID
    • Username
    • Password
  • Search for lambda in the search box and click on Lambda from the search result.
Create an AWS Lambda Function for Python
  • Click on Create function.
Create an AWS Lambda Function for Python
  • Choose Author from scratch because we want to create an AWS lambda function from scratch.
    1. Specify the AWS Lambda function name, In my case it is mytestingfunction.
    2. Select Runtime from the dropdown list. I have selected Python 3.9. You can select as per your requirement.
    3. Leave Architecture ( x86_64 ) by default.
    4. As you can see here, I am not configuring Advance Settings. Will explore this in later tutorials.
    5. Click on Create function.
Create an AWS Lambda Function for Python
  • After a few seconds, Your AWS lambda function for Python will be created and ready to use.
  • All your lambda Functions will be displayed in the Functions section.
Create an AWS Lambda Function for Python

Now it’s time to test the lambda function, is it created perfectly or not?

Testing AWS Lambda Function

Follow these steps to test the AWS lambda function.

  • To check the lambda function, just click on the lambda function which you have created.
  • By default AWS Lambda for Python gives the following code snippet.
import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

But for testing purposes, I am going to change it with the following code snippet.

import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': event
    }
Note:- It is just a sample code provided by the AWS lambda itself but in real life application,you will write your business logic according to your project requirement.

After changing something in the code, don’t forget to click on the Deploy button because without deploying the code your changes will not be reflected. In fact, a message something like “Changes not deployed” will be showing. As you can see below.

Create an AWS Lambda Function for Python

After successfully deploying the code, You will get a notification message something like below.

Create an AWS Lambda Function for Python
  • Now we are ready to test. Click on Configure test event under the Test dropdown.
Create an AWS Lambda Function for Python
  • Provide the following test information.
    • Choose Create new event option.
    • Provide test event name. In my case event name is “my-first-aws-lambda-event“, It might be different in your case.
    • Provide separate values in each key.
    • And click on Save.
Create an AWS Lambda Function for Python
  • To test the AWS lambda function with the event, select an event from the Test dropdown and click on Test.
Create an AWS Lambda Function for Python
  • It will take some time to execute the function and after that, you will see your response in a separate tab like below.
Create a Python Lambda Function for Python

As you can see in the above picture, The AWS lambda function has been successfully executed along with the event and 200 status code.

So, this is how you can create your AWS lambda function for Python programming.

Summary

I hope the process of creating the AWS lambda function for Python was easy and understandable. Remember, Always use the IAM user to create the AWS lambda function in fact you can use root
users also to create the lambda function but you have to remember one thing IAM users must have permission to access aws lambda. If you are working in an organization, Then it will be a high possibility, your reporting person will provide you IAM user who has permission to access aws lambda.

If you like this article, please share and keep visiting for further AWS tutorials.

Happy Learning….

AWS Lambda Overview 2024

Related Posts