-
Book Overview & Buying
-
Table Of Contents
Serverless Architectures with AWS
By :
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.
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:
AWS Lambda could change the list of supported languages at any time. Check the AWS website for the latest information.
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:

Lambda in the search box, and click on the Lambda service in the search result:


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:


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.

minnum and maxnum. Then, we are using the random() method of the Math 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 the generatednumber 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); myLambdaFunction_settings in the Description field, select 128 MB in the Memory field, and have 3 sec in the Timeout field:




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.
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:
The solution for this activity can be found on page 152.
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:
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 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.
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:
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.
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.
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:
Monthly billable compute (GB- s) = Total compute – Free Tier compute
Monthly compute charges = Monthly billable compute (GB-s) * Monthly compute price
Monthly billable requests = Total requests – Free Tier requests
Monthly request charges = Monthly billable requests * Monthly request price
Monthly compute charge + Monthly request charges
The solution for this activity can be found on page 153.
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.
Change the font size
Change margin width
Change background colour