Hello, everyone!
In this diary, we will build our first serverless page, which says the hello.
We will using these components of AWS below:
- Lambda
- API Gateway
We are going to write our business code as a function in AWS Lambda, and assign an URL to run and show the response of the function via the API Gateway component.
Create an AWS Lambda function
- Go to AWS Lambda dashboard page: https://console.aws.amazon.com/lambda/;
- Click the “Create function” button near the northeast corner;
- Choose the “Author from scratch” option;
- Enter the function name “hello”;
- Choose the runtime “Node.js 14.x”;
- Leave the rest things default;
- Click the “Create function” button near the southeast corner.
After, it will redirect you to the function page automatically.
Write the code
In the function page, you can see Code source panel with default folder “hello” and file “index.js”. Let’s double click on “index.js” to open it in the editor.
Now, paste the code below:
exports.handler = async (event) => {
console.log(event)
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/plain'
},
body: 'Hello!',
}
return response
}