What is AWS Lambda?

AWS Lambda services
AWS Lambda

Introduction to AWS Lambda Service

Amazon Web Services (AWS) is one of the world’s most prevalent cloud platforms, providing over 200 full-featured services to data centers around the planet.

AWS Lambda is a serverless technology that allows you to run code without the need to administer servers. The code can be written in the AWS Management console, downloaded as a ZIP file, or as a container. The Lambda code can be executed after you manually run it, or automatically after some event. 

Lamba Triggers:

The trigger for running AWS Lambda can be:

  • lifecycle events, such as with Amazon Simple Storage Service;
  • events like S3 bucket data change, DynamoDB update;
  • response to input HTTP requests, for example, using Lambda with API Gateway;
  • scheduled Lambda function AWS using Amazon EventBridge (CloudWatch);
  • trigger an event from an Amazon SQS queue. (can be used to process orders in online stores;
  • and etc.

Benefits of Amazon Lambda:

  1. Everything is recorded in logs that you can review at any time.
  2. Lambda languages ​​are almost the complete modern technology stack. At the time of writing, the following languages ​​were supported (.NET 6 (C#/PowerShell), .NET Core 3.1 (C#/PowerShell), Go 1.x; Java 11 (Corretto), Java 8 on Amazon Linux; Node.js 16 .x, 12.x, 14.x; Python 3.9, 3.8, 3.7; Ruby 2).
  3. Payment for the use of only resources for the duration of the function.
  4. Easy to set up. Prepare the necessary script, create a function, and select a trigger to run.

Payment for Using Lambda AWS Serverless Functions.

You are only billed for the resources you use. The cost is calculated depending on the number of feature requests and their duration (the time your code is used). The Lambda code usage time is calculated from the start of the code execution to the return of a value or termination of work for another reason, rounded to a multiple of 1 ms. The price depends on the amount of RAM allocated to the Amazon Lambda function.

In AWSLambda functions you can choose the amount of memory it will use. By this choice, CPU power and other resources are also assigned. So the memory can be selected from 128 MB to 10,240 MB in 1 MB increments. At 1769MB, the function is equivalent to one vCPU (one vCPU-second of credits per second).

The free tier includes 1 million free requests per month and 400,00 GB of computing seconds per month, which can be used on both x86 and Arm processors. There is also a multilevel pricing model (Savings Plans). Which provides for a reduction in payment for using the service, subject to the mandatory use of a constant amount of Lambda computing resources (measured in USD / hour) over one or three years. Savings are achieved through duration and Provisioned Concurrency.

If the Amazon Lambda function will use other AWS resources, then you need to create an IAM Role that the function will use. There is a separate Lambda@Edge service. It works as an add-on to the Amazon CloudFront service. As a result, you can run your code in AWS locations closer to your users. Which improves system performance and reduces latency and allows you to make your applications globally distributed. The cost of use is calculated for the number of requests and the resources used. The AWS website has pricing examples.

For example, if the Lambda@Edge function is executed 10 million times a month and each execution takes 10 ms, the cost would be:

Total Charges = Computing fees  + Request fees  = $0.63 + $6.00 = $6.63 per month.

How Do You Get Started Using Lambda in AWS?

If you do not have an AWS account, you can create one and use certain products and services for free within certain limits for 12 months. This allows you to test applications for free to understand the technology and choose the solution that is best for you.

When registering, be sure to provide your credit card details. You need to keep track of the resources used so as not to exceed the values ​​provided by the free tier. You can do this by periodically reviewing the AWS Billing Dashboard.

You can also control costs using AWS Budget. Namely:

  1.  Monitor certain AWS services.
  2.  Control your expenses so that they do not exceed a certain threshold. If the amount exceeds the threshold, then you can set up notifications through the AWS SNS service to receive SMS to the phone, and E-mail notifications, using the AWS Chatbot service (communication service) send a message to Slack. You can also set up the automatic shutdown of instances (servers), etc.
  3. Control of the percentage of resource use.  If there is a fixed charge, for example for reserved resources, you can find out the usage percentage of those resources.

Before getting started with AWS services, I recommend setting up AWS Budget to control your spending. In a few clicks, you can set up an e-mail alert when spending exceeds the threshold you set.

AWS Lambda Example

I will provide an example of how to quickly deploy a simple web page in the AWS console. The page expands within two minutes. First, we go to the AWS console, look for the Lambda AWS service, and select the region we need. 

AWS Lambda example

The region should be chosen taking into account where most of your customers are located.I chose the region Europe(Frankfurt) eu-central-1. Click Create Function.

AWS Lambda example 2

In “Advanced settings” check “Enable function URL”. If in “Auth type” select “NONE”, then all users, when they click on your URL, can run your Lambda function. Enabling Tags and adding it to an AWS resource allows you to quickly allocate resources that belong to a specific group later. Next, click “Create function”.

AWS Lambda example 3

Here in the “Lambda_function” tab paste your code (in our case, in Python 3.9).

Lambda Function

AWS Lambda function example

Short description of the Python code:

In source IP, we write the IP address from where the request came from .Checking rawQueryString – passing additional parameters in the request. If there is an additional parameter called Name, then the response will be “Hello <Name parameter value> Your public IP address is …….. “If there are no additional parameters, then the response will be “Hello from Lambda! Your public IP address is ……”. To write the code to our function, you need to click “Deploy”. Also, the code can be uploaded in a .zip file by clicking “Upload from”. There is a file upload size limit. For files larger than 10 MB, consider uploading using Amazon S3. Now when you click on the “Function URL” link https://<URL-ID>.lambda-url.eu-central-1.on.aws We will go to our website, which will indicate your public IP address.

Also, the code can be uploaded in a .zip file by clicking “Upload from”. There is a limit on the size of the uploaded file. For files larger than 10 MB, consider uploading using Amazon S3. 

Now when you click on the “Function URL” link https://<URL-ID>.lambda-url.eu-central-1.on.aws/We will go to our website, which will indicate your public IP address.

Amazon Lambda

If we pass an additional parameter with URL name=Robert

https:// <URL-ID>.lambda-url.eu-central-1.on.aws/?name=Robert

We get it in a web browser

Amazon Lambda 2

We can always change the settings in the Function URL and change the “Auth type”. For example, by selecting “AWS_IAM”. Only authenticated IAM users and roles can make requests to your function URL

Amazon Lambda example

 The deployment of your site is very fast. Within 2 minutes you get your simple website.

If you want to use Lambda to work with AWS services to get information about created AWS resources and manage them (create, delete, stop, start).

Then you need to grant additional permissions to the AWS Lambda function.

 Conclusion

In this article, I briefly described what AWS Lambda is and gave an example of its use.The AWS Lambda function is a very powerful tool for working with AWS services. With it, you can better configure your systemIf you will be using AWS, be sure to try using AWS Lambda functions.