What is Serverless?
- Serverless computing, serverless in short, is a deployment and execution model for cloud providers like AWS, Azure, GCP, etc. is responsible for running the applications locally and testing them, and keep ready for production deployment.
- Typically, serverless apps are being run inside a stateless container by the cloud providers, which can be triggered on an event base like CloudWatch, API gateway, ALB, etc., on AWS.
What is a Stateless Function?
- Functions that can be run inside an application server instead of running inside a stateless container are maintained as infrastructure by AWS.
- Prior to any execution, these functions can only be invoked on an AWS event basis, where the request will be processed on a synchronous basis by default with a 15 mins hard session timeout.
What is our use case?
- The current setup involves a sample application that needs to be deployed to AWS lambda. At the same time, the developer should easily test it locally before deployment.
- As the development team is working on the application, deployment and testing strategy are involved in all application development across all the maintained environments.
- At a high level, the development team should be able to test the application without deploying the code to the AWS lambda.
Configuring Serverless on a local machine.
1. Install NodeJs on Windows and Linux-based systems; refer to the below link.
2. Install serverless.
Local testing of the function.
- Create a sample function that returns the deployment status in the serverless file.
- Now create a serverless file to test the above function locally, which requires certain parameters with a file name as serverless.yml.
- Add the security group and VPC subnet details to the abovementioned parameters to provision lambda under a private subnet.
- Let’s add the lambda and API gateway details to the serverless file.
- Above package statement packages the whole function based on the wanted files for final deployment to the AWS.
- The events section creates an API gateway, adds a trigger to the function, and outputs the final API gateway endpoint for testing.
- Provide the rest API-Gateway ids if any existing API gateway needs to be reused.
- Once the parameters are in place, perform a dry run locally and validate the function.
- Below is the command to test the function developed locally from terminal/bash.
Command: serverless invoke local –function <function_name>
- Deploy the function from local to AWS account using below command. Command: serverless deploy or SLS deploy –function <function_name>
- Below is the reference for the first deployment of lambda to AWS account.
- Below is the reference to the deployment of new updates to lambda function.
- The deploy command creates the resources in the background by creating a Cloudformation stack and provisioning the required resources mentioned in the serverless yaml.
- In the above yaml file, we are provisioning AWS lambda, IAM Role for Lambda, and AWS API gateway as a trigger for the lambda.
- The serverless will let the developer know which files are being deployed from the above result. It only deploys th files with changes compared to the existing deployment in the AWS account.
- In case of any deployment is initiated without any changes, serverless will keep the information posted back stating no changes deployed.
- In order to delete any existing deployment on the same account below command can be used.
Command: sls remove –function
- The above command helps remove the existing infra deployed by the serverless with the help of CloudFormation from the AWS account same time. Final CloudFormation will also get deleted.
Written by – Karanam Sreenivas