Welcome to this comprehensive guide, where we will cover deploying a Serverless Node.js application on Amazon Web Services (AWS), one of the most popular cloud service providers globally. This tutorial is designed for beginners in software development, so we will take things slow and explain concepts in detail.
Before we dive in, it's important to understand what we're working with.
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, which lets developers create scalable network applications. AWS, on the other hand, offers a suite of cloud services that allow you to host and scale your applications. One of these services is AWS Lambda, a serverless computing service that runs your code in response to events.
In this article, we will discuss:
- Prerequisites
- Setting up your AWS account
- Creating a simple Node.js application
- Installing and configuring the Serverless Framework
- Deploying your application on AWS using the Serverless Framework
Prerequisites
- A basic understanding of JavaScript and Node.js.
- Node.js and
npm
(Node Package Manager) are installed on your local machine. You can download them fromhere . - An AWS account. If you don't have one, you can create it from
here .
Setting up your AWS account
After setting up your AWS account, you need to create a new user in AWS Identity and Access Management (IAM):
- Login to your AWS Management Console.
- Navigate to IAM, and then click on
"Users."
(Image 1)
-
Click on
"Add users"
(Image 2).
- Enter a
username
and press thenext
button. - Set permissions by attaching existing policies directly. For simplicity, use
"AdministratorAccess"
In a production environment, it's advised to limit the permissions, but for this guide, we will give admin access to our account (Image 3). ClickNext.
- Review the details and click on
"Create user."
After the user is created, you should click on the name of our account and go to“Security credentials“
and click on“Create access key“
(Image 4).
- Select
“Third-party-service“
and acceptAlternative recommended
options. Setup description and press“Create access key“
. You'll be shown an access key ID and a secret access key. Make sure to store them safely, as you won't be able to view the secret access key again.
Creating a Simple Node.js Application
How to create a simple app, I have described in my previous articles How to Deploy a Serverless Node.js Application on Azure and Deploying a Serverless Node.js Application on Google Cloud.
Firstly, it's important to note that AWS Lambda functions do not support listening on HTTP ports like a regular Node.js application might. Instead, AWS Lambda functions are event-driven, meaning they respond to events like HTTP requests through the API Gateway.
Secondly, AWS Lambda functions should export a handler function that takes an event object, context object, and a callback as parameters. This function is what AWS Lambda will call when your function is invoked.
Therefore, your original code should be modified to fit this model. Here's the modified app.js
code:
exports.handler = async (event, context) => {
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/plain',
},
body: 'Hello World\n',
};
return response;
};
A package.js will be:
{
"name": "hackernoon",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Save all these files to your project folder and you are ready to go.
Installing and Configuring the Serverless Framework
The Serverless Framework helps you develop and deploy AWS Lambda functions. It's a CLI tool that needs to be installed globally.
-
Install the Serverless Framework:
npm install -g serverless
-
Setup your AWS credentials with the Serverless Framework:
serverless config credentials --provider aws --key <your_key> --secret <your_secret>
Replace <your_key>
and <your_secret>
with the access key ID and secret access key you got when creating the IAM user.
-
In your project directory, create a new file named
serverless.yml
.
-
Open
serverless.yml
and add the following configuration:service: hackernoon-test provider: name: aws runtime: nodejs18.x functions: hello: handler: app.handler events: - http: path: hello method: get
This configuration defines a new service named hackernoon-test
. It specifies that we're using AWS as our provider and Node.js 18.x as our runtime. It also defines a function named hello
that will execute our handler
function in app.js
.
Deploying your Application on AWS
Now that we've set up our application and the Serverless Framework, we can deploy our application:
-
Deploy the service:
serverless deploy
-
Open your browser and navigate to your fresh link:
Deploying hackernoon-test to stage dev (us-east-1) ✔ Service deployed to stack hackernoon-test-dev (58s) endpoint: GET - https://37c01fcgfj.execute-api.us-east-1.amazonaws.com/dev/hello functions: hello: hackernoon-test-dev-hello (531 B)
And there you have it! You've just deployed your first Serverless Node.js application on AWS. As you can see, the Serverless Framework simplifies the process, letting you focus on writing your application.
Conclusion
In this guide, we've covered the basics of deploying a Serverless Node.js application on AWS. We've only scratched the surface of what's possible with AWS and the Serverless Framework, and there's much more to explore. Remember, the best way to learn is by doing, so don't be afraid to experiment and build your own applications.
Happy coding!