Deploy AWS Lambda functions with Serverless Framework and Node.js
Deploy test lambda - Edition #29
Hey, I'm Marco and welcome to my newsletter!
As a software engineer, I created this newsletter to share my first-hand knowledge of the development world. Each topic we will explore will provide valuable insights, with the goal of inspiring and helping all of you on your journey.
In this episode, you will learn how to deploy Lambda functions so that they can be invoked via an HTTP call using the AWS API Gateway.
You can download all the code shown directly from my Github repository: https://github.com/marcomoauro/hello-world-lambda
👋 Introduction
Serverless technology is very interesting because it allows you to focus on your product goals without worrying about managing servers. This makes the development process much easier and more enjoyable. I discovered the benefits of serverless computing thanks to a former colleague who introduced me to it.
I will guide you through the process of creating and deploying your first AWS Lambda function.
You will learn how to set up the function so it can be invoked through an HTTP call using AWS API Gateway. This will be a great starting point for understanding how serverless applications work and how you can leverage them to build scalable and efficient solutions.
👨💻 Let's implement together
You can download all the code shown directly from my Github repository: https://github.com/marcomoauro/hello-world-lambda
You will need the setup I showed here:
Initialize project
let's create a serverless project, to do so we can open the terminal and run the command:
sls // or "serveless"
we choose:
AWS / Node.js / Simple Function
and then:
Skip Adding An App
choose a project name, once done enter the project folder and initialize an NPM project with:
npm init -y
within the project you will see that there are two files:
serverless.yml
handler.js
serverless.yml
serverless.yml is a configuration file used in serverless applications.
It defines the functions, events, and resources for your serverless service. With this file, you can specify how your functions should be deployed, what triggers them, and what permissions they need. It simplifies managing and deploying serverless functions.
This is an example:
org: implementing
service: hello-world-lambda
provider:
name: aws
runtime: nodejs20.x
region: eu-west-2
functions:
hello:
handler: handler.hello
the sections we find are:
provider
In this part you can configure informations including the cloud provider you want to use and other information depending on the one you choose.
Having chosen AWS, we can choose the Node runtime that will be used in Lambda and the region where we wish to deploy the services.
Even though cloud providers offer many options, Serverless v4 has chosen to focus exclusively on AWS. If you want to use Google Cloud, Azure or other providers, you need other solutions. You can find more information here.
functions
This section defines the functions of the application; a Lambda will be created for each when it is deployed.
Each function has a name and specifies the handler file, the events that trigger it (such as HTTP requests) and other settings such as memory and timeout.
In our case we have created a function with the name hello, if triggered the JS hello function in the handler.js file will be executed.
functions:
hello:
handler: handler.hello
A Lambda function can be triggered in many ways:
By an HTTP endpoint created with API Gateway.
By writing to or a TTL expiration in DynamoDB.
By reading from an SQS queue.
By scheduled events, like every minute.
By listening to an SNS topic.
And much more!
Deploy
Deployment is done using the command:
sls deploy
This command creates an AWS CloudFormation stack that includes all the resources like Lambda functions, queues, buckets, API Gateway, and CloudWatch logs, along with their configurations. The Serverless framework automates this process, so you don't have to create and configure everything manually. It also allows you to version your infrastructure as code, making it easier to manage and update.
We deploy the application:
Browsing the Cloudformation service, we can see that the new stack hello-world-lambda-dev has been created:
you can see that all the services the stack needs to function have been created such as: Lambda, Log, IAM Role, Bucket S3, BucketPolicy S3.
The -dev part in the stack name hello-world-lambda-dev was added by the framework and is called stage.
It allows you to create multiple environments for your application such as staging, test, prod. You can specify the stage using --stage option during deploy, an example:
sls deploy --stage prod
we can test the lambda by invoking it via the serverless cli with the command:
sls invoke --function hello
Create public endpoint
One way to trigger a Lambda function is by calling an endpoint. I will show you how easy it is to create this using the AWS API Gateway service.
we can modify the serverless.yml file in this way:
org: implementing
service: hello-world-lambda
provider:
name: aws
runtime: nodejs20.x
region: eu-west-2
functions:
hello:
handler: handler.hello
events:
- httpApi:
method: GET
path: /hello
The events defines the events that will invoke your functions, such as HTTP requests, changes in a DynamoDB table, messages in an SQS queue, scheduled tasks, or SNS notifications. This section helps you configure how and when your functions should run.
In our case we have added a public endpoint that will invoke our Lambda if called with the HTTP method GET and path /hello.
we launch the deployment to update the stack:
sls deploy
We find the new public endpoint directly in the output of the command.
Test it from this link: https://rqp1zzyk66.execute-api.eu-west-2.amazonaws.com/hello
By browsing the Lambda service and choosing the function hello-world-lambda-dev-hello we can see from the UI that the endpoint generated with API Gateway has been successfully added as a trigger to the function.
🌟 Top lectures of the week
How I write 1000s tests with little effort
I am not a fan of heroism in the engineering industry
Communicate like a Senior: Phrases used by the best leaders
Normalization is not enough anymore.
And that’s it for today! If you are finding this newsletter valuable, consider doing any of these:
🍻 Read with your friends — Implementing lives thanks to word of mouth. Share the article with someone who would like it.
📣 Provide your feedback — We welcome your thoughts! Please share your opinions or suggestions for improving the newsletter, your input helps us adapt the content to your tastes.
💬 Chat with me — If you have any doubts or curiosity, please write to me, I will be happy to answer you!
I wish you a great day! ☀️
Marco
A hands-on example, well explained Marco!