Implementation of a Lambda function and creation of an SQS with Serverless
As many of today’s computing vendors continue to base their businesses on providing Infrastructure-as-a-Service (IaaS), there is one clear goal being pursued by industry leaders: to make computing transparent.
With this premise, Serverless is used to refer to a computing model in which the provider of the computing layer allows us to run the so-called “functions” code fragments for a certain period, without having to deal with the basic infrastructure provided to deliver the service. In this model, it is the provider’s responsibility to provide the resources transparently, expand them automatically if demand increases, and release them when they are not in use.
To understand how Serverless works and why it had become so popular, first we must remember when AWS launched a way to create infrastructure from a code file, this is known today as Cloud Formation, and this came to facilitate the entire process of creating resources for our application. But not everything is so good, to build infrastructure from code, we must pass the access policies and roles to communicate resources.
Here is where the Serverless framework appears, which came to solve this problem of access policies. When we create infrastructure from Serverless, we do not need to pass the access policies to the resources in the code file.
¿But what do we achieve with this?
With this, we get “cleaner” and more readable code, where only the characteristics of the infrastructure we need are specified. And is that Serverless, at the moment of the creation of the resources, is in charge internally of generating the policies and the permissions of our resources.
In this article, we will discuss the implementation of a lambda function using the Serverless framework, as well as the creation of an SQS from the deployment.
The first thing we must do is to go to our AWS console and obtain the credentials of our account. Let’s see an explanatory image:
After that we copy the credentials:
In the console we type the following command to edit the AWS configuration:
On Mac:
nano ~/.aws/credentials
On Windows:
C:/Users/USERNAME/.aws/credentials
If you do not already have your account credentials configured, you will need to assign them from the CLI with the following command:
aws configure
Substitute your own AWS credentials values for the values your_access_key_id and your_secret_access_key.
After this we will proceed to create the test project folder and install serverless. To install it, we type the following command in the console:
npm i serverless
To create the serverless.yml file and the handler.js file that will contain our lambda function, we type the following command in the console:
Serverless create –-template aws-nodejs –-name serverlessTest
Our project should look like this:
For this test example, we will not edit the handler.js file since we will only wait for the lambda function to return a message.
In the serverless.yml file, we remove all the comments and we should be left with something like this, which is what we are going to work on:
To create the SQS automatically when the lambda is displayed we add the following event to the function:
And we assign the necessary resources for the SQS. For this, we add the following instruction:
Our serverless.yml file should look like this:
Finally, we create the deployment scripts inside our package.json file
Our package.json file should look like this:
To deploy the infrastructure we are going to create, we just need to type the following command:
npm run deploy
It is important to clarify that from the serverless.yml file we can deploy all the infrastructure we need for AWS with just one command, there we can define the resources to create EC2, SQS, SNS instances, among others.
The invoke:hello command allows us to run our function in the local environment, the deploy command allows us to deploy our function in AWS, and the remove command will remove all the resources that were created in AWS.
If we type in the console npm run invoke:hello we will see that our function is executed and we will have the lambda message:
We’ll also get the S3 bucket that Serverless generates to contain the deployment data, a lambda function, and an SQS.
You can find more information and templates of a serverless.yml file at the following link:
https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml
In conclusion, serverless is growing and, as you have seen, the benefits it gives us are very important. We stop worrying about server provisioning and infrastructure management and the focus on application development is excellent, which is good for the business as teams are more involved in delivering products that create value for customers quickly.