How to Deploy Lambda From Local to AWS Using Serverless?

Deploy Lambda From Local to AWS Using Serverless
May 26, 2022

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.

Install NodeJs on the windows and linux

2. Install serverless.

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

Cloud Computing Insights and Resources

cloud computing

Elevate Cloud Cost Optimization with Graviton and Spot Instance Integration

Cloud Cost Optimization refers to analyzing and implementing strategies to cut unnecessary expenses and boost organizational efficiency. It includes identifying […]

data warehouse migration

Accelerate and Simplify Your Data Warehouse Migration with AWS & Rapyder 

Data warehouse migration is a critical process that many organizations undergo to modernize their data infrastructure, improve performance, and enable […]

Cloud Consulting

6 Reasons to Collaborate with a Cloud Consulting Firm in 2024

The technology landscape keeps evolving, without a break, and the shift towards cloud solutions is undeniable. Companies are increasingly embracing […]