back_image
blog-image

AWS Lambda Function: How to Create It and Latest Pricing

Author Image By Editorial Team

Last Updated: May 15, 2023

5 minutes

00:00:00
Reading time: 0s

AWS Lambda is one of the hundreds of services provided by Amazon Web Services. Lambda provides flexible and serverless computing. AWS Lambda falls under the category of Function-as-a-service (FaaS). It is event-driven computing that a user can achieve without worrying about computing infrastructure. Since it’s an integral part of Amazon Web Services, all things like system integration and maintenance will be taken care of.  You just need to focus on the programming and the Lambda will handle all the aftermath. Using Lambda, the user can perform any kind of task including but not limited to computing tasks, serving a webpage, and running other AWS resources like S3 buckets.

What is Serverless?

We are going to define serverless in terms of how AWS explains it. Serverless doesn’t mean your application is running without a server, it’s there but the end users don’t need to worry about server management. 

For example, while setting up Lambda, you might use one of the common architectures i.e. LAMP stack. In this stack, you will get the following:

  1. Linux OS
  2. Apache HTTP server
  3. MySQL Relational Database Management
  4. PHP programming language

Apart from that there are other aspects of computing you need to consider.

  1. Database service
  2. HTTP gateway service
  3. Computing service

You see how many things you may have to worry about, but you don’t need to if you are using serverless architecture.

Here are the salient features of AWS Lambda serverless computing.

  • No server or infrastructure to manage.
  • Scalability
  • Pay only for what you use (Only pay for resources when they are in use or not idle): For example, if your company has an internal website and you only use it from 9 to 5 and only on weekdays then you won’t be paying for extra hours and days.
  • High availability and reliability because of the 93 availability zones spread throughout vast geographical areas.

AWS Lambda Function: How Does It Work?

  1. When a Lambda function is invoked (By Amazon CLI, an API, or an event in an Amazon S3 bucket) a resizeImage function is put into effect.
  2. When a request comes to the AWS cloud interface targeting the resizeImage Lambda function, AWS Lambda creates a container with code that you have written in the resizeImage Lambda function. And this newly built container handles the incoming request.
  3. One container can handle multiple requests, but when the number of requests reaches a certain threshold, the resizeImage Lambda function creates new containers. And these new containers handle further incoming requests.
  4. If the number of requests decreases the AWS Lambda will automatically kill the excess containers. (Number of containers is directly proportional to the number of incoming requests.)
How AWS Lambda function works

Apart from the coding part that you need to provide everything else will be handled by the AWS infrastructure.

As long as the resizeImage Lambda function and container are running, you will get charged. It means when there are no incoming requests, AWS Lambda will kill all the containers it builds and you will be charged nothing during that time.

How to Create an AWS Lambda Function using Lambda Web Console?

  • Go to aws.amazon.com
  • Sign in to the console
  • Go to the Services<Compute<Lambda from the top left corner or simply type “Lambda” in the search bar.
Navigate to AWS Lambda
  • Click on the create a function button for creating a new Lambda function. 
Create a Lambda Function

Author from Scratch

Create your own code with the help of the Hello World example.

  • Select the Author from scratch option from the top.
AWS Lambda - Author from scratch
  • Give a suitable name to the function. In the Runtime dropdown choose the programming language in which you want to write your function. (Note: AWS Console editor only supports Node.js, Python, and Ruby.)
Runtime
  • In the Change default execution role section, you need to select the permission for your function. You can choose the create a new role with basic Lambda permissions option if you’re creating a function for the first time.
Lambda Function Execution roles
  • Advanced settings comprised options like code signing for adding an extra security layer and VPC (Virtual Private Cloud).
  • Next, click on the create function button at the bottom right corner of the screen.
Advanced settings

Use a Blueprint

Templates for pre-built Lambda functions. You can select one of them as a basic structure and then modify them as per your requirements. Here, you will get code for built-in AWS integrations and some other commonly used functions.

This particular example shows you how to create a Lambda function as well as a trigger for Amazon S3 Bucket using a pre-built blueprint.

  • Click on the Use a blueprint option on the top.
AWS Lambda - Use a Blueprint
  • First, we have to select a blueprint.
  • Type s3 in the search bar. As a result, you will get two options.
    • For the node.js function choose s3-get-object.
    • For the python function choose s3-get-object-python.
Amazon s3 blueprint
  • Select any one of them and click on configure.
Configure Amazon s3 blueprint
  • In the Basic information section, fill the following details.
    • Give a suitable name in the Function name field.
    • In the Execution role option, select the Create a new role from AWS policy templates option. You’ll need to go to the IAM Console for creating any custom role.
    • Next, enter a name for your new role.
AWS S3 Execution role
  • In the S3 Trigger section, choose the S3 bucket. You have to make sure that both the S3 bucket and the function are in the same region.
  • In the Lambda function code, you will be able to see preconfigured code based on your input and the selected blueprint.
  • Click on the Create Function button.
Lambda Function code

Recommended: Create first AWS EC2 Instance within 5 minutes using free account

Latest AWS Lambda Pricing

The pricing model of serverless AWS computing is to pay only for what you use. It means if the Lambda function is not in use then you will be charged nothing.

AWS Lambda calculates the overall usage based on two metrics i.e. GB-seconds and the number of requests.

GB-seconds is the RAM allocated by you to the function multiplied by the total number of seconds your function ran to execute the code. And the number of requests is simply the total number of times your function is called.

X86 ArchitectureDurationRequest
First 6 Billion GB-seconds/month$0.0000166667 for every GB-second$0.20 per 1M request
Next 9 Billion GB-seconds/month$0.000015 for every GB-second$0.20 per 1M request
Over 15 Billion GB-seconds/month$0.0000133334 for every GB-second$0.20 per 1M request

The cost of duration is calculated based on the memory (RAM) you allocated to your function. AWS provides RAM starting from 128 MB up to 10,240 MB. Have a look at the below table to get an idea of pricing.

RAM (in MB)Price per ms
128$0.0000000021
512$0.0000000083
1024$0.0000000167
1536$0.0000000250
2048$0.0000000333
10240$0.0000001667

Conclusion

Hope you have learned a great deal about serverless computing and how the AWS Lambda function fits in this equation. AWS serverless is among the top-tier in the industry because of some obvious reasons. On top of that, you get Lambda@Edge feature that allows you to run the code closest to the user’s application. This minimizes the latency and improves performance as well. If you found this article interesting, then we suggest you look at AWS Route 53 blog. There we’ve explained it thoroughly and how it can benefit your computing infrastructure.

Categories: Technology

0 Comments

Leave a Reply

Avatar placeholder