AWS Lambda Functions to Create a Simple REST API Endpoint
You can use AWS Lambda for serverless hosting of API endpoints without needing a full server. Let's set up a simple REST API.
Lately, I've found a great option for creating simple APIs or running functions without the hassle of deploying and managing a server. AWS Lambda offers a convenient solution by allowing functions to run in a pre-configured environment, eliminating the need for server management. This service supports various programming languages, including C#, Python, and JavaScript, enabling easy code deployment triggered by user-defined events.
AWS Lambda also simplifies setting up API endpoints through a user-friendly dashboard. In this guide, I'll show you how to establish a basic AWS Lambda REST API Endpoint, including necessary configurations to access the provided URL.
Be aware of the costs associated with using Lambda, which are based on function runtime. For infrequent use, charges are minimal, but extensive use can lead to significant costs. Fortunately, AWS provides tools to monitor usage and set alerts to manage expenses effectively.
Now, let’s get started…
Prerequisites
Before diving into AWS Lambda, ensure you have an AWS account and have installed API testing software like Postman. If not, follow these steps to get started:
Create a new Lambda function
To get started, let’s login to your AWS account and in the search bar enter lambda then select the Lambda option under services with the distinctive Lambda logo
If this is your first time creating a Lambda function then it will take you to the landing page for Lambda with a few examples and explanations. If you have a function already created it will take you to the dashboard list of the functions available.
For this example we’re on the initial landing page so we’ll click on Create a function button.
The create functions wizard is quite easy to follow and there aren’t a huge amount of fields to populate to get started.
There’s even an option to select Use a blueprint which lets you choose Lambda functions with simple pre-built boiler code templates to speed up your initial build, but for this simple demo I’m going to start one from scratch.
Make sure you fill out a Function name and select the Runtime, and with the runtime you can select what you’ll do your coding in which gives you a few options to choose:
Node.js
.NET
Python
Ruby
And select the Architecture, for this I’m selecting the default and using Node.js to use JavaScript.
One thing that I want to point out is depending on how you want to use your Lambda function, you’ll probably want to update the Advanced settings.
In my situation, I want to quickly just use the Enable function URL so I don’t have to set any other API integrations and just use the URL it generates, your situation might be different, but I’m just wanting to create a quick and easy API function.
Another thing is I want this endpoint to be accessible to any public access, here again you may want to restrict it to authenticated users, but you can also set specific authentication within the function itself later on too.
I’m also wanting to configure cross-origin resource sharing(CORS) which is super handy if you’re trying to access the function api from a different domain that browsers can restrict.
Once you've completed your choice of configuration, click on Create function.
And there we go, we've created a base template for a Lambda Function and also generated a Function URL that we can use to access this function.
This is the URL you can use to access it as the API Endpoint! Super easy!
And with this, if you load the Function URL in your browser it should load the words “Hello from Lambda!”.
Create function code
Now we have the initial Lambda Function template generated, we can update the code so it can generate something that we can actually use.
In this example, I’m just doing something simple that will receive a POST request with a body and then use that url to get data and return the response to the original request.
This doesn’t do much… however, I’ve used this before to get over that annoying CORS allowed origins error you can get in your browser. You know, when you build a site and try to retrieve data in your site from another domain but it comes back with an error because of Cross-Origins-Resource-Sharing error, well, the function isn’t running in a browser, so it can access the data and then you can set CORS access from your own site to this function and circumvent that issue on your site! 😉
You can do so much with the code here, add environment variables, create multiple folders, run testing, upload files, etc. But we’re just going to edit the existing content for now in the main file, I may do another post on more detailed functions down the track when I have more time.
If you scroll down on the main Dashboard you can see the Code section is highlighted and the window by default is opened to the index.mjs file.
You can run tests on your code and Deploy changes, at the moment, the deploy button is shaded because we haven’t made any changes to what is live at the moment.
Now I’m going to replace everything within the index.mjs file with the below code:
import https from "https";
export const handler = async (event) => {
const request = JSON.parse(event.body);
const options = {
hostname: request.hostname,
path: request.path,
};
return new Promise((resolve, reject) => {
https
.get(options, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
reject(new Error("statusCode=" + res.statusCode));
}
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
resolve({
statusCode: 200,
headers: {
"Content-Type": "application/json",
},
body: data,
});
});
})
.on("error", (err) => {
reject(err);
});
});
};
This pretty much gets the body from the request that’s being sent to it, gets the hostname and path field values from the request body and loads them up in a new https GET request and then if the response is successful it sends that data back to the original request (the person who sent the original request), otherwise it will trigger an error if it can’t get it or fails.
Breakdown:
Me sending request with host to lambda → lambda sending that request to the host requested → lambda gets response → lambda send it back to me
Now i’ve made changes it shows that i can Deploy and it also lists that there are changes not deployed
Click on the Deploy button and that will make the code go live and accessible to the Function URL we saw earlier.
Now technically, we should be able to hit this endpoint with a payload and it’ll return whatever is on the other end based on the hostname and we can do that with Postman!
Test endpoint with Postman
If you haven’t setup and installed Postman, I’d suggest you download it now and set it up, it’s super handy for any API testing and will help out loads during your development and testing.
If you don’t want to use Postman, you can use a free online API service like ReqBin or Postbin, or a number of online free options, the request is pretty much the same, you can even use your command line with a curl command.
*NOTE: Just keep in mind, if you’re using a free online API testing service, I’d recommend NOT putting any confidential data in there to test.
Anyway, back to Postman…
Open up postman and let’s create a new request by clicking on the “+” button
This will create a new request option and default it to GET request, we need to add Function URL , change it to a POST request and then select the Body and add the raw payload data (i’m just using my profile site which will just return the HTML page).
Payload example:
{
"hostname": "",
"path": ""
}
And now, if you click on Send, this will hit the api endpoint and return the result.
This is great! You now have the Lambda API Endpoint working, receiving a payload, running a function and returning a response!
Now, there is another problem though, if you are using the api endpoint in your own website, the domain doesn’t match your site, so it’ll through a CORS error.
To resolve this we need to add some Configuration settings to allow your website in the browser to talk with the function and not show any CORS errors, and that is the next section.
Fix CORS errors with Lambda Functions
The endpoint is now accessible and the functions should be working while returning a response, however, if you try and access the endpoint within your own site it’s most likely going to be throwing a CORS error and not loading any data.
Let’s go through how to fix that.
Just note, I’m going to adjust this at the highest level to get it working fast and quickly, it’s probably best to apply the correct fields in each section instead of the wildcard “*” in production environments to make sure it’s secure and has the correct permissions.
Coolio, so first thing, let’s select the Configuration tab and on the left menu we’ll bring up the Function URL.
This will display the CORS and URL configuration, but we can see that they are missing some permissions, particularly the Expose headers, Allow header and Allow methods.
So let’s click on the Edit button.
From our previous configurations settings when we created the function, most of this should be fine, but what we want to do it edit the below:
Allow Origin
*
Expose headers
*
Allow headers
*
Allow methods
POST
This is not best practice as I’m allowing all which is what the asterisk represents, so in real environment it’s best to set the ones that are required. But I’m just doing this quickly to get it working.
Then click on the Save button at the bottom.
Now if you add the url and payload to your web application, it should allow access and return with a response and have NO CORS errors coming back!
YAY!
Monitoring
In the Monitoring tab, which is in the same section as the Code and Configuration tab, you can view a range of analytics and statics in regards to your functions.
This includes, errors, requests, timeframe, charged time usage, etc.
It’s super handy to keep track of how the Lambda function is running.
Summary
Lambda Functions are extremely handy and helpful to run quick bits of code and create a simple API Endpoint to run basic tasks. For complex and large running functions, it may not be the most suitable solution, however if it’s just quick little compute methods this can be a lifesaver with its simple setup and monitoring.
AWS Lambda Functions can also be linked to most of the services within the AWS ecosystem and triggered when other events are fired.
There are so many more features and functionality that Lambda functions can assist with and for developers, not having to maintain a server is a huge win. I’ll probably go into more details in a later post on some other Lambda integrations, but for now, I just wanted to highlight how simple it is to get an API endpoint setup and use with your own website.
Until next time peeps, take care and happy dev-ing! 😁