How to Deploy Lambda From Local to AWS Using Serverless?

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.

Installation.

1. Install NodeJs on Windows and Linux-based systems; refer to the below link.

https://positiwise.com/blog/how-to-install-npm-and-node-js-on-mac-and-windows/

 

Install NodeJs on the windows and linux

2. Install serverless.

https://www.serverless.com/framework/docs/providers/fn/guide/installation

Install serverless

Local testing of the function.

  • Create a sample function that returns the deployment status in the serverless file.
Create a sample function
  • Now create a serverless file to test the above function locally, which requires certain parameters with a file name as serverless.yml.
create a serverless file to test the function locally
  • 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.
add the lambda and api gateway
  • 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>
command used to test the function developed locally
  • 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.
deployment of lambda to AWS account
  • Below is the reference to the deployment of new updates to lambda function.
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.
Deployment initiated
  • In order to delete any existing deployment on the same account below command can be used.

Command: sls remove –function

command used to test the function developed locally from terminal
  • 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

Share

Search Post

Recent Posts

How to communicate with customers on multiple communication channels
July 29, 2024
The role of cloud consulting in enhancing customer experience
July 22, 2024
Exploring Different Cloud Computing Models: Public, Private, and Hybrid
July 16, 2024

Categories

Tags

Subscribe to the
latest insights

Subscribe to the latest insights

Related Posts

How to communicate with customers on multiple communication channels

The role of cloud consulting in enhancing customer experience

Exploring Different Cloud Computing Models: Public, Private, and Hybrid

Get in Touch!

Are you prepared to excel in the digital transformation of healthcare with Rapyder? Let’s connect and embark on this journey together.

I accept T&C and Privacy