How To Deploy Lambda Functions With Serverless Framework ⚡️
Deploying multiple Lambda functions to AWS in your application
The serverless framework is one of my favorite IaC (infrastructure as code) frameworks for building serverless services in AWS.
It is my go-to method for creating Lambda functions, DynamoDB tables, API Gateway APIs and other serverless services.
The Serverless Framework makes it really easy to work with serverless and build fast, reusable cloud systems.
In this article, we’ll look at building out a Lambda function using Serverless Framework and deploying it to AWS the right way.
Installing Serverless Framework
The first step here is to install the serverless framework CLI.
In your terminal run this command:
npm install -g serverlessOnce installed, you can confirm the installation by running:
serverless -- versionIf you see a message saying “Installed Serverless Framework…”, you’re good to go.
Next you’ll need to configure AWS so that it can deploy infrastructure using an IAM role.
Before doing this, go into your AWS account and create an IAM role with admin permissions so you can create any service you need through the CLI.
Once created run this command:
aws configure(if you don’t have the AWS CLI installed before, you can do so by running “brew install awscli”).
You’ll get prompted for your IAM user’s access key ID and secret access key. Enter them and follow the instructions.
Once your AWS user is configured, you can start using the serverless framework to create infrastructure.
Creating the YAML File
The heart of the serverless framework is the serverless.yml file.
This file defines your service, provider (AWS in this case) and all the resources you want to provision (Lambda, DynamoDB, S3, etc).
Here’s an example of creating a Lambda function that will fetch users from a database (i.e. a DynamoDB table):
service: my-app-fetch-users
frameworkVersion: "3"
provider:
name: aws
runtime: nodejs20.x
region: us-east-1
functions:
my-app-fetch-users:
handler: index.handler
memorySize: 1024
timeout: 10
events:
- http:
path: v1/users
method: getWhat this does:
Creates a service called my-app-fetch-users
Configures AWS Lambda to use Node JS v20.x runtime in us-east-1 region
Defines a Lambda function named index that will be triggered via an API Gateway HTTP API endpoint, in the path /v1/users (GET).
Now let’s write the actual Lambda function code to fetch the users from a DynamoDB table.
Writing The Lambda Function
Here we’ll assume the DynamoDB table is already created (we’ll focus on creating Lambda functions in this article).
By default, Serverless framework looks for a index.mjs file in your project.
So you’ll want to setup your backend project like this:
The room folder is the backend. The inside of it you have a folder called functions. Then inside the functions folder, all of your functions can reside.
The folder my-app-fetch-users is inside this folder. It contains the index.mjs file (the actual lambda function code) and the serverless.yml file (which is used to deploy the function to AWS).
Let’s write this code into the index.mjs file:
import { DynamoDBClient, QueryCommand } from "@aws-sdk/client-dynamodb";
const ddbClient = new DynamoDBClient({ region: "us-east-1" });
const TableName = "users";
export const handler = async (event) => {
try {
const command = new QueryCommand({
TableName,
KeyConditionExpression: "pk = :pk",
ExpressionAttributeValues: {
":pk": { S: "app-users"}
},
});
const response = await ddbClient.send(command);
const users = response.Items;
return {
statusCode: 200,
body: JSON.stringify(users),
};
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify({ error: err.message }),
};
}
};This function runs a simple query operation to a DynamoDB table called “users” and fetches user items inside the “app-users” partition.
Now when we invoke the API which we will create below, it will trigger this Lambda function which will fetch the necessary data from DynamoDB.
Deploying To AWS Lambda
Now that we’ve setup our serverless configuration and written the function, it’s time to deploy.
From within the backend folder, run this command in the CLI:
serverless deploy Or you can use the shorthand:
sls deployWith this command, the CLI will package your code, create a CloudFormation stack and deploy the resources (Lambda function, API Gateway, etc) to AWS.
Once the deployment is completed, Serverless Framework will print out the endpoint of your function like:
endpoints:
GET - https://ry4jft.execute-api.us-east-1.amazonaws.com/dev/my-app-fetch-usersNow you can test out the endpoint by running it in your browser or fetch call:
curl https://ry4jft.execute-api.us-east-1.amazonaws.com/dev/my-app-fetch-users👋 My name is Uriel Bitton and I’m committed to helping you master Serverless, Cloud Computing, and AWS.
🚀 If you want to learn how to build serverless, scalable, and resilient applications, you can also follow me on Linkedin for valuable daily posts.
Thanks for reading and see you in the next one!


