Deploying a Serverless Function with AWS Lambda
Mariam Adedeji
Posted on September 29, 2024
In this tutorial, you will learn how to create a simple AWS Lambda function using JavaScript to handle HTTP requests.
First off, what is Serveless?
Serverless computing allows you to run code without provisioning or managing servers. It basically allows you to focus on coding and forget about infrastructure. AWS Lambda is one of the most popular services for running serverless functions.
Let’s start by highlighting all the steps needed and then explaining them in detail below:
- Set up AWS Lambda function
- Write a JavaScript function
- Set Up an API Gateway Trigger
- Test Serverless Function
- (Optional) Monitor and Scale
Prerequisites
- AWS Account – You’ll need an AWS account to access the AWS Lambda service. If you don’t have one, sign up here.
- A Basic Understanding of JavaScript – We'll be writing a simple JS function.
- Some Coffee ☕ – To keep things fun, of course!
Step 1 — Set Up AWS Lambda
1) Login to AWS Console:
Go to the AWS Management Console and sign in.
2) Navigate to Lambda:
- In the search bar, type "Lambda" and select Lambda.
- Click Create function.
3) Create a New Lambda Function:
- Select Author from scratch.
- For the Function name, enter
myServerlessFunction
. - For the Runtime, choose
Node.js 20.x
(for JavaScript support). - Leave other options as default.
- Click Create Function.
You should now have a basic Lambda function ready to be customized!
Step 2 - Write Your JavaScript Function
By default, AWS Lambda creates a template function. Let’s modify it to respond to HTTP requests.
1) Edit the Code:
In the Code tab, you’ll see a file named index.mjs
. Replace the code with the following:
export const handler = async (event) => {
const name = event.queryStringParameters?.name || 'World';
const response = {
statusCode: 200,
body: JSON.stringify({
message: `Hello, ${name}! Welcome to Serverless.`,
}),
};
return response;
};
What this does:
- Event object: This contains request data. Here, we are reading query parameters (e.g., ?name=John).
- Response: It returns a 200 status with a JSON body containing a greeting message.
2) Deploy the Function: Click Deploy to save and publish your function changes.
Step 3 - Set Up an API Gateway Trigger
To make your Lambda function accessible over HTTP, we’ll use AWS API Gateway.
1) Add API Gateway Trigger:
- Scroll to the Function 0verview section and click Add trigger.
- From the dropdown, select API Gateway.
- Choose Create a new API.
- For API Type, select HTTP API.
- For Security, choose Open.
- Leave the defaults for Additional settings and click Add.
2) Get the API URL: After adding the trigger, you will see the API Gateway URL. Copy this URL, you will use it to call your function.
Step 4 - Test Your Serverless Function
With everything set up, let’s test your function.
1) Open a Web Browser or Postman: Use the API Gateway URL you copied earlier. Test the function by appending the name query parameter to the URL:
https://your-api-url?name=John
2) Result: If everything is working, you should get a response like:
If you omit the name query parameter, you should get the default:
Step 5 - (Optional) Monitor and Scale
AWS Lambda automatically scales based on the number of requests. You don’t need to worry about managing infrastructure, but you can monitor your function’s performance.
Go to the Monitoring tab: Inside your Lambda function dashboard, you’ll see metrics such as invocation count, duration, errors, etc.
Yay! That’s it! You can extend this function to do practically anything, like querying databases, processing files, or integrating with other AWS services.
If you’ve found this article helpful, please leave a like or a comment. If you have any questions or constructive feedback, please let me know in the comment section.
Posted on September 29, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.