| by Arround The Web | No comments

How to Create a Lambda Function With Python Runtime

“Lambda is a serverless computing service provided by AWS to run your code without managing the scaling and high availability of the servers. By the term serverless, we mean that you do not need to have a server and manage it to run your application. Lambda performs tasks on your behalf and manages everything by itself. So, it has a unique feature of auto-scaling and managing the sources and the services as per requirements. Remember that, in real-time, it is not possible to run an application without a server and its management, so it is the AWS that assigns a server and manages all these services by itself, which is not your headache.”

Following are some of the major tasks which AWS undertakes and performs on our behalf when you deploy your application on lambda.

  1. Servers and Operating system maintenance
  2. Management of memory, CPU, Network, and other resources
  3. Automatic Scaling
  4. High availability
  5. Monitoring fleet health
  6. Applying security methods
  7. Keeping track of lambda functions (Cloud watch)

Lambda function supports several languages, including Java, Go, PowerShell, Node.js, C#, Python, and Ruby. In order to use any other programming language, it also provides Runtime API service. In this tutorial, we will explain how we can create a lambda function with python runtime from scratch.

Billing of Lambda

After having been through a brief introduction of lambda service, the very first thing that comes to our mind is, if it is serverless computing, then how would AWS charge this service? Unlike the billing of EC2 servers, where AWS charges for the time the server is running, irrespective of the fact whether our code is running or not, AWS charges for Lambda only when the code is running. So, the Lambda service is charged only when our code is up, not from the time our Lambda function was created. So the duration of the bill for the Lambda function is calculated for the code execution time, which is rounded up to the nearest 1 ms*.

Creating a Lambda Function With Python Runtime

To create a lambda function with python runtime, we will create a function, and then we will configure it to use the python runtime. In this tutorial, the whole procedure of creation and configuration of the Lambda function will be discussed.

First of all, login to the AWS management console and then search for Lambda in the search bar and go for it

On the left sidebar, click on the functions button to get all the functions in the lambda console.

On the right side of the screen, click on the create function button to create the function.

After choosing the create function, we will be directed to the configuration page of the Lambda function. There we will find four ways of creating a Lambda function. For now, we will go for the “Author from scratch” option.

In the Basic Information menu, we will set a Function name of our choice for our function.

In the next step, we will choose a language that we want to use to write our function. In this tutorial, we will choose python 3.9 as our runtime.

Then, the following two options will appear for architecture.

  • X86_64
  • arm64

For this demo, we will choose x86_64 as the architecture that we want to use for the function code.

By default, AWS creates an IAM role that has permission to throw logs to CloudWatch. We may create our own role with the policies that we want to use for our Lambda function as per our requirements. For this tutorial, in the “Change default execution role” menu, we will go for “Create a new role with basic Lambda permissions.

Then, in the bottom right corner, we will click on create a function to create the function.

A new screen will appear with a success message and overview of the function as below. On this page, we may add triggers and destinations for our function. Besides it, other information about the function can also be seen, which includes Description, Last modification information, Function ARN, and URL to the function.

Testing of a Lambda Function

In the code source menu, we can see our hello world python code, as shown in the picture below.

Here we can test our code by simply clicking on the Test button.

It will prompt a configure test event screen, and here you can configure a test event by naming it, choosing the availability of the test event as private or shareable, and for the default hello-world template that we have chosen.

Then click on the save button to save the event.

It will take us back to the main menu of the function displaying the success message at the top. Here we will again click on the Test button, and our code will be executed.

As we have seen the execution results of our code in the new tab. Now we can overwrite and edit our code and test it in the same way.

Reading Logs of a Lambda Function

As we have given permission to our Lambda function to upload logs to CloudWatch. So, logs will be uploaded to cloud watch each time we execute our code.

In the left side panel of cloudwatch, we will click on logs. Here, we will be able to see two options in logs.

  • Log groups
  • Log insights

In log groups, we will be able to see logs for hello-world-function as /aws/lambda/hello-world-function.

In log insights, we are able to search and analyze our logs’ data. Here we can make queries to perform our work related to logs more efficiently.

Here in log insights, if we choose our log group and click on Run Query, we will get our desired results.

And, We will get our results with @timestamp and @message as follows

In the same way, we can create up to 1000 Lambda functions. We can write our python code to meet our requirements and execute it. Moreover, multiple tasks can also be automated using the Lambda function. We have mentioned some basic tasks below that are managed by the Lambda function through python code using the boto3, which is Python SDK for AWS.

  • To receive EC2 instance condition alerts
  • To schedule the start and stop of EC2 instances
  • To put an item into the AWS DynamoDB table


Lambda function is a serverless computing service provided by AWS in which AWS performs all the tasks on our behalf, from the allocation of a server to the maintenance, management, networking, and security of that server for our code. AWS charges for lambda only when the Lambda function is triggered, and it may be billed in milliseconds even. Hence, this Lambda-function service has revolutionized the field of cloud computing, making a number of cloud tasks easy to handle.

Share Button

Source: linuxhint.com

Leave a Reply