visit
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.
In this article, we will discuss:
npm
(Node Package Manager) are installed on your local machine. You can download them from
"Users."
(Image 1)
Click on "Add users"
(Image 2).
username
and press the next
button."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). Click Next.
"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).
“Third-party-service“
and accept Alternative 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.
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.
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;
};
{
"name": "hackernoon",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}
npm install -g serverless
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
.
serverless deploy
Deploying hackernoon-test to stage dev (us-east-1)
✔ Service deployed to stack hackernoon-test-dev (58s)
endpoint: GET - //37c01fcgfj.execute-api.us-east-1.amazonaws.com/dev/hello
functions:
hello: hackernoon-test-dev-hello (531 B)