A quick tour of AWS Lambda
What is it?
AWS Lambda: This is a computational service that allows you to run code for almost any type of application or server service — all without the need for administration. AWS Lambda performs all the administration for you, including server and operating system maintenance, resource allocation and automatic scaling, code monitoring and logging. All you have to do is provide your code in one of the languages that AWS Lambda supports.
Why use it?
- Money. You only pay for the time when the service is running.
- Speed. By itself, lambda rises and works very quickly.
- Convenience. Lambda has many capabilities to integrate with AWS services.
- Performance. In parallel, it can be performed, depending on the region with a maximum of 1000 to 3000 copies. And if you wish, this limit can be raised by writing in support.
This approach has its drawbacks, you can not control the operating system on which the code is running, you can not control the CPU, memory and resources. AWS does all this.
All you can do is choose a language from the AWS Lambda supported.
What can?
Below is a brief list of the main features of AWS Lambda. Next, we consider everything in order.')

1. Triggers
Triggers are the “pathogens” of lambda. In some ways, lambda can be compared to PHP, in the sense that for us it
is executed and dies . Next, we consider in detail the mechanism of work. While you need to understand that lambda, this is one function that is performed on request from triggers.
Below is a list of all possible triggers:API Gateway
AWS IoT
Alexa Skills Kit
Alexa Smart Home
Application load balancer
Cloudfront
CloudWatch Events
CloudWatch Logs
Codecommit
Cognito Sync Trigger
DynamoDB
Kinesis
S3
SNS
SQS
For each of them, you will need to configure the unique parameters that are available for these triggers. You can also configure multiple triggers for one lambda. It depends on the type of trigger whether lambda will be executed synchronously or asynchronously.
Notice: Please note that the lambda can be made to run, and using the AWS CLI, AWS SDK in manual mode, passing all the necessary parameters. Including whether it will be performed synchronously or not.
Let's take an example:
1. API Gateway - allows you to pull the lambda on the http request and requires you to return the result to the user. Such an operation cannot be performed asynchronously, since requires an answer. For synchronous operations, some functions are not available.
2. SQS - for example, if our lambda processes messages from SQS, there is no need to return the result anywhere and it can be executed asynchronously. With asynchronous execution, several new features appear, for example, we can configure re-execution in case of an error, or send such requests further to the dead
SQS queue.
2. Permissions to AWS Services
These are AWS services to which lambda has access by default. What does it mean? In the function that you will write, you can always connect the AWS SDK and without keys or any authorization parameters you can use the available services. All available services you define in
IAM Role that you use for this lambda.
Each language in use has its own SDK that can communicate with the main AWS services.
Notice: for each lambda you configure the IAM Role on behalf of which lambda will run
You can set up a virtual network for your lambda, for example, to
connect securely to
RDS .
4. Online Editor
AWS Lambda also provides the ability to edit the code of your function directly from the interface in your browser.

5. Logging
All requests for lambda are logged in
CloudWatch , data on runtime and memory is also recorded there, this data can be very helpful in setting limits. Also in the code it is possible to log your own data (for example, in Node.js via console.log).
Additionally, you can always see statistics on the use of lambda on the tab
Monitoring6. Environment Variables
You have the ability to transfer environment variables to the code safely, which allows you to configure important parts of the system without delivering the code. It is possible to
encrypt environment variables through keys.Notice: Please note that there is a list of predefined environment variables.
7. Code
Now the most interesting part, in itself lambda consists of several parts.1. Layers - the bottom layer. It is not mandatory, but if you need to add some libraries to use the lambda, then you need to put them separately from the main code, so you greatly save on the amount of the main code and the speed of the function itself.
The layers in AWS Lambda are somewhat similar to the
layers in the docker , in the sense that they are permanents in relation to the function, and they need to be changed separately. Also they can be reused in other lambdas.
2. Function Environment - the code must necessarily contain a function that will be directly executed each time the lambda is started
(Handler) . About her below. And in front of her is her entourage, which we ask. The fact is that resource management occurs in such a way that this environment is stored separately from the function for some time after its completion. And at the next start, it resumes, without wasting time and resources on initialization. Thus, everything that is possible must be initialized to the function itself, for example, configuration, connection of libraries, etc.
3. Handler - directly by itself the executable code, depending on the language, is determined in different ways. For example, take Node.Js. In order for your code to execute, you need:
- js file - 1pcs
- exports.yourFunction = () => {// Your code} - 1pcs
By default, index.js runs and looks for a function in it called “handler”. Then executes it. Your function may be asynchronous in code. This does not affect the synchronous execution of lambda.
Below is a sample code, I will try to describe what is happening there:
8. Versioning
The service supports convenient versioning. In short, we can issue a version of each downloaded copy. And add aliases that point to some version. How it works? Look at the diagram below.

And so, now let's see what needs to be done to get what we see in the diagram.
First condition
- Create the first version of our lambda. Together with the first version, we create a pointer to the "$ LATEST" version. It always indicates the latest version added.
- Add alias "Dev" . Here we choose where to bind, we have several options - create a pointer to a specific version number, in our case "1" , bind the pointer to "$ LATEST" , or bind to another alias. In this case, we attach to the $ LATEST pointer , and now our “Dev” alias will always point to the last branch, so we can always test our application with the latest version of lambda on our environment. And if suddenly we need to check how it works on the old version, we just need to switch the alias in the trigger or change the link to the version of the alias without touching our application
- Add a Stage alias and attach it to the first version of our lambda
- Add the “Prod” alias and repeat what we did for the “Stage”
Notice: here is how to work with aliases in practice. The practical part of the lambda will be in the next article along with SQS
And so, now we have received something incomprehensible, in fact 3 alias refer to one version, it is not clear. But nothing, all in orderSecond state
- Create a second version of our lambda (It is possible to add a second output "Hello world"). Here I want to note that at this moment "$ LATEST" will immediately look at the second version. And since “Dev” is tied to "$ LATEST" , it will also look at the second version.
- Next, we want our “Stage” to look at the second version. Now it is tied to version "1" . Here we need to manually change the version that “Stage ” points to.
- We are happy. We got what we see as the second state on the graph. That is, our “Prod” looks at the first version, and “Dev” and “Stage” at the second.
Third state
Now, to get the third state, we just need to add a couple more lines to our code and there will be a third version of our lambda. And
"Dev" will look at her now.
We summarize
And so, what do we have?
Small Fast Relatively cheap. Autoscaled Versioned Function
In this article we covered only the theoretical part, familiarity with this service. As with
SQS in the last article. In the next article we will look at how these two services interact with each other and in practical form we will learn how to configure and use them.