AWS Lambda
AWS Lambda is a serverless computing platform that you can use to execute your code to build on-demand, smaller applications. It is a compute service that runs your backend code without you being involved in the provisioning or managing of any servers in the background.
The Lambda service scales automatically based on your usage and it has inbuilt fault-tolerance and high availability, so you don't need to worry about the implementation of HA or DR (disaster recovery) with AWS Lambda. You are only responsible for managing your code, so you can focus on the business logic and get your work done.
Once you upload your code to Lambda, the services handles all the capacity, scaling, patching, and infrastructure to run your code and provides performance visibility by publishing real-time metrics and logs to Amazon CloudWatch. You select the amount of memory allocation for your function (between 128 MB and 3 GB). Based on the amount of memory allocation, CPU and network resources are allocated to your function. You could also say that AWS Lambda is a function in code that allows stateless execution to be triggered by events. This also means that you cannot log in to actual compute instances or customize any underlying hardware.
With Lambda, you only pay for the time that your code is running. Once execution is completed, the Lambda service goes into idle mode and you don't pay for any idle time. AWS Lambda follows a very fine-grained pricing model, where you are charged for compute time in 100 ms increments. It also comes with a Free Tier, with which you can use Lambda for free until you reach a certain cap on the number of requests. We will study AWS Lambda pricing in more detail in a later section.
AWS Lambda is a great tool for triggering code in the cloud that functions based upon events. However, we need to remember that AWS Lambda (in itself) is stateless, meaning that your code should run as you develop it in a stateless manner. However, if required, a database such as DynamoDB can be used. Over the years, AWS Lambda has become very popular for multiple serverless use cases, such as web applications, data processing, IoT devices, voice-based applications, and infrastructure management.
Supported Languages
Lambda is stateless and serverless. You should develop your code so that it runs in a stateless manner. If you want to use other third-party services or libraries, AWS allows you to zip up those folders and libraries and give them to Lambda in a ZIP file, which in turn enables other supportive languages that you would like to use.
AWS Lambda supports code written in the following six languages:
- Node.js (JavaScript)
- Python
- Java (Java 8 compatible)
- C# (.NET Core)
- Go
- PowerShell
Note
AWS Lambda could change the list of supported languages at any time. Check the AWS website for the latest information.
Exercise 1: Running Your First Lambda Function
In this exercise, we'll create a Lambda function, specify the memory and timeout settings, and execute the function. We will create a basic Lambda function to generate a random number between 1 and 10.
Here are the steps for completion:
- Open a browser and log in to the AWS console by going to this URL: https://aws.amazon.com/console/.
Figure 1.4 : The AWS console
- Click on Services at the top-left of the page. Either look for Lambda in the listed services or type
Lambda
in the search box, and click on the Lambda service in the search result:Figure 1.5 : AWS services
- Click on Create a function to create your first Lambda function on the AWS Lambda page:
Figure 1.6 : The Get started window
- On the Create function page, select Author from scratch:
Figure 1.7 : The Create function page
- In the Author from scratch window, fill in the following details:
Name: Enter
myFirstLambdaFunction
.Runtime: Choose Node.js 6.10. The Runtime window dropdown shows the list of languages that are supported by AWS Lambda, and you can author your Lambda function code in any of the listed options. For this exercise, we will author our code in Node.js.
Role: Choose Create new role from one or more template. In this section, you specify an IAM role.
Role name: Enter
lambda_basic_execution
.Policy templates: Select Simple Microservice permissions:
Figure 1.8 : The Author from scratch window
- Now, click on Create function. You should see the message shown in the following screenshot:
Figure 1.9 : Output showing Lambda function creation
So, you have created your first Lambda function, but we have yet to change its code and configuration based on our requirements. So, let's move on.
- Go to the Function code section:
Figure 1.10 : The Function code window
- Use the Edit code inline option to write a simple random number generator function.
- The following is the required code for our sample Lambda function. We have declared two variables:
minnum
andmaxnum
. Then, we are using therandom()
method of theMath
class to generate a random number. Finally, we call"callback(null, generatednumber)"
. If an error occurs, null will be returned to the caller; otherwise, the value of thegeneratednumber
variable will be passed as an output://TODO implement let minnum = 0; let maxnum = 10; let generatednumber = Math.floor(Math.random() * maxnum) + minnum callback(null, generatednumber);
- In the Basic settings window, write
myLambdaFunction_settings
in the Description field, select 128 MB in the Memory field, and have 3 sec in the Timeout field:Figure 1.11 : The Basic settings window
- That's it. Click on the Save button in the top-right corner of the screen. Congratulations! You have just created your first Lambda function:
Figure 1.12 : Output of the Lambda function created
- Now, to run and test your function, you need to create a test event. This allows you to set up event data to be passed to your function. Click on the dropdown next to Select a test event in the top-right corner of the screen and select Configure test event:
Figure 1.13 : Lambda function Test window
- When the popup appears, click on Create new test event and give it a name. Click on Create and the test event gets created:
Figure 1.14 : The Configure test event window
- Click on the Test button next to test events and you should see the following window upon successful execution of the event:
Figure 1.15 : The Test execution window
- Expand the Details tab and more details about the function execution appear, such as actual duration, billed duration, actual memory used, and configured memory:
Figure 1.16 : The Details tab
You don't need to manage any underlying infrastructure, such as EC2 instances or Auto Scaling groups. You only have to provide your code and let Lambda do the rest of the magic.
Activity 1: Creating a New Lambda Function that Finds the Square Root of the Average of Two Input Numbers
Create a new Lambda function that finds the square root of the average of two input numbers. For example, the two numbers provided are 10 and 40. Their average is 25 and the square root of 25 is 5, so your result should be 5. This is a basic Lambda function that can be written using simple math functions.
Here are the steps for completion:
- Follow the exercise that we just completed before this activity.
- Go to the AWS Lambda service and create a new function.
- Provide the function name, runtime, and role, as discussed in the previous exercise.
- Under the section on Function code, write the code to find the square root of the average of two input numbers. Once done, save your code.
- Create the test event and try to test the function by executing it.
- Execute the function.
Note
The solution for this activity can be found on page 152.
Limits of AWS Lambda
AWS Lambda imposes certain limits in terms of resource levels, according to your account level. Some notable limits imposed by AWS Lambda are as follows:
- Memory Allocation: You can allocate memory to your Lambda function with a minimum value of 128 MB and a maximum of 3,008 MB. Based on memory allocation, CPU and network resources are allocated to the Lambda function. So, if your Lambda function is resource-intensive, then you might like to allocate more memory to it. Needless to say, the cost of a Lambda function varies according to the amount of memory allocated to the function.
- Execution Time: Currently, the Lambda service caps the maximum execution time of your Lambda function at 15 minutes. If your function does not get completed by this time, it will be automatically be timed out.
- Concurrent Executions: The Lambda service allows up to 1000 total concurrent executions across all functions within a given region. Depending on your usage, you may want to set the concurrent execution limit for your functions, otherwise the overall costs may escalate very soon.
Note
If you want to learn more about other limits of Lambda functions, go to https://docs.aws.amazon.com/lambda/latest/dg/limits.html#limits-list.
AWS Lambda Pricing Overview
AWS Lambda is a serverless compute service and you only pay for what you use, not for any idle time. There is a Free Tier associated with Lambda pricing. We will discuss the Lambda Free Tier in the next section.
To understand the AWS billing model for Lambda, you first need to understand the concept of GB-s.
1 GB-s is 1 Gigabyte of memory used per second. So, if your code uses 5 GB in 2 minutes, and then 3 GB in 3 minutes, the accumulated memory usage would be 5*120 + 3*180 = 1140 GB seconds.
Note
The prices for the AWS services discussed in this section and in this book are current at the time of writing, as AWS prices may change at any time. For the latest prices, please check the AWS website.
Lambda pricing depends on the following two factors:
- Total Request Count: This is the total number of times the Lambda function has been invoked to start executing in response to an event notification or invoke call. As part of the Free Tier, the first 1 million requests per month are free. There is a charge of $0.20 for 1 million requests beyond the limits of the Free Tier.
- Total Execution Time: This is the time taken from the start of your Lambda function execution until it either returns a value or terminates, rounded up to the nearest 100 ms. The price for execution time varies with the amount of memory allocated to your function. If you want to understand how the cost of total execution time varies with the total amount of memory allocated to the Lambda function, go to https://aws.amazon.com/lambda/pricing:
Figure 1.17 : Lambda pricing
Lambda Free Tier
As part of the Lambda Free Tier, you can make 1 million free requests per month. You can have 400,000 GB-seconds of compute time per month. Since function duration costs vary with the allocated memory size, the memory size you choose for your Lambda functions determines how long they can run in the Free Tier.
Note
The Lambda Free Tier gets adjusted against monthly charges, and the Free Tier does not automatically expire at the end of your 12-month AWS Free Tier term, but is available to both existing and new AWS customers indefinitely.
Activity 2: Calculating the Total Lambda Cost
We have a Lambda function that has 512 MB of memory allocated to it and there were 20 million calls for that function in a month, with each function call lasting 1 second. Calculate the total Lambda cost.
Here's how we calculate the cost:
- Note the monthly compute price and compute time provided by the Free Tier.
- Calculate the total compute time in seconds.
- Calculate the total compute time in GB-s.
- Calculate the monthly billable compute in GB- s. Here's the formula:
Monthly billable compute (GB- s) = Total compute – Free Tier compute
- Calculate the monthly compute charges in dollars. Here's the formula:
Monthly compute charges = Monthly billable compute (GB-s) * Monthly compute price
- Calculate the monthly billable requests. Here's the formula:
Monthly billable requests = Total requests – Free Tier requests
- Calculate the monthly request charges. Here's the formula:
Monthly request charges = Monthly billable requests * Monthly request price
- Calculate the total cost. Here's the formula:
Monthly compute charge + Monthly request charges
Note
The solution for this activity can be found on page 153.
Additional Costs
While estimating Lambda costs, you must be aware of additional costs. You will incur costs as part of Lambda integration with other AWS services such as DynamoDB or S3. For example, if you are using the Lambda function to read data from an S3 bucket and write output data into DynamoDB tables, you will incur additional charges for read from S3 and writing provisioned throughput to DynamoDB. We will study more about S3 and DynamoDB in Chapter 2, Working with the AWS Serverless Platform.
In summary, it may not seem like running Lambda functions costs a lot of money, but millions of requests and multiple functions per month tend to escalate the overall cost.