Home » Lambda deployments from local to AWS using Serverless

Lambda deployments from local to AWS using Serverless

by admin

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 state of serverless apps are being run inside a stateless container by the cloud providers, which can be triggered on an event base like Cloudwatch, Apigateway, ALB, etc. on AWS.

What is a Stateless Function?

  • Functions that can be run inside an application server instead run 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 have the ease of testing 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 have the possibility to test the application without deploying the code to the AWS lambda.

Configuring Serverless on a local machine.

Local testing of the function.

  • Create a sample function which return the deployment status referred 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 mentioned above parameters to provision lambda under a private subnet.
  • Lets 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
  • Events section creates an API gateway and adds as an trigger to the function and outputs the final API gateway endpoint for testing.
  • Provide the rest API-Gateway ids in-case if any existing API gateway needs to be reused.
  • Once the parameters are in place perform a dry run on local and validate the function.
    • Below is the command used 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 create the resources in the background by creating a Cloudformation stack and provision the required resources mentioned in the serverless yaml.
  • In 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 file are being deployed from the above result. As it only deploys th files which have 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.

    • Inorder to delete any existing deployment on the same account below command can be used

Command: sls remove –function

  • Above command helps in removing the existing infra deployed by the serverless with help of CloudFormation from the AWS account same time Final CloudFormation will also get deleted.

Written by – Karanam Sreenivas