Background Overlay
Serverless Computing

Deploying a Lambda Function Using the Serverless Framework

Serverless Lambda Deployment

Serverless computing has revolutionized how developers build and deploy applications. With the Serverless Framework, deploying AWS Lambda functions becomes a breeze, abstracting away infrastructure management and letting you focus on code. In this post, we’ll walk through deploying a Lambda function using the Serverless Framework, complete with code examples.

Introduction

AWS Lambda allows you to run code without provisioning servers, but managing its infrastructure manually can be tedious. The Serverless Framework simplifies this by providing a CLI and configuration model to define and deploy Lambda functions, API Gateway triggers, and more. This article covers the end-to-end process of setting up and deploying a Lambda function, perfect for beginners and seasoned developers alike.


What is the Serverless Framework?

The Serverless Framework is an open-source tool that streamlines the development and deployment of serverless applications. It supports AWS Lambda and other providers, automating tasks like packaging code, creating IAM roles, and setting up API Gateway. Key features include:

  • Simplified configuration using YAML.
  • Support for multiple runtimes (Node.js, Python, etc.).
  • Plugins for local testing and advanced workflows.

Step-by-Step: Deploying a Lambda Function

1. Prerequisites

Before starting, ensure you have:

  • Node.js (14.x or later) and npm installed.
  • An AWS account with IAM permissions for Lambda, API Gateway, and CloudFormation.
  • AWS CLI configured with credentials (aws configure).
  • Serverless Framework installed globally:
    npm install -g serverless

2. Set Up the Project

Create a new project and initialize a Serverless service:

mkdir my-lambda-app
cd my-lambda-app
serverless create --template aws-nodejs

This generates serverless.yml and handler.js.

3. Configure serverless.yml

Edit serverless.yml to define your Lambda function and API Gateway trigger:

service: my-lambda-app

frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs18.x
  region: us-east-1
  memorySize: 128

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /hello
          method: get

4. Write the Lambda Function

Update handler.js with your function logic:

'use strict';

module.exports.hello = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Hello from Lambda!',
      input: event,
    }),
  };
};

5. Deploy the Function

Deploy to AWS with:

serverless deploy

This packages your code, creates a CloudFormation stack, and provisions the Lambda function and API Gateway. The CLI outputs the API endpoint.

6. Test the Function

Test the API endpoint using curl or a browser:

curl https://[id].execute-api.us-east-1.amazonaws.com/hello

This link can be found from your CLI or AWS console

Expected output:

{"message":"Hello from Lambda!","input":{...}}

Best Practices

  • Use environment variables in serverless.yml for sensitive data.
  • Set least-privilege IAM roles for your Lambda function.
  • Use the serverless-offline plugin for local testing.
  • Monitor logs in AWS CloudWatch or the Serverless Dashboard.

Remove Lambda

To remove the whole stack from AWS, run this code. It will remove Lambda, gateway, and other things related to what was deployed before from AWS

serverless remove

Conclusion

The Serverless Framework makes deploying AWS Lambda functions straightforward and efficient. By following the steps outlined, you can set up, deploy, and test a Lambda function with minimal effort.

For more serverless adventures, explore integrating other AWS services or scaling your application with multiple functions.

Happy coding!


If you found this post helpful, share it on social media. For more on serverless and web development, follow my Medium, Stack Overflow, or check out my GitHub repositories.