What Are AWS Lambda Function URLs And Why You Don’t Need API Gateway
Deploy Lambda function URLs without building and maintaining rest APIs.
API Gateway is a powerful, highly scalable and fully managed (rest) API service that AWS offers to you at very low charge (1 million calls per month for free).
AWS Lambda function URLs allow you to define an endpoint for your function which you can call directly from your front-end using a simple fetch request.
This makes it a very useful feature. Furthermore, you can configure security with CORS and IAM permissions for access & security management.
While Lambda function URLs are very convenient, they don’t entirely eliminate the need for API Gateway.
API Gateway has many other advantages which Lambda function URLs don’t provide.
Some powerful and useful features of API Gateway include:
- API Lifecycle Management — robust tools for managing lifecycles of your API like staging, versioning and deploying of APIs.
- Throttling & Rate Limiting — a useful feature of API Gateway is that you can throttle and rate limit calls made to the API
- Caching — you can cache responses from your backend to reduce latency
- Custom Domain Names — allows you to expose endpoints under a custom domain name
While these are advantageous, sometimes you do not need the overhead of managing a large rest API and Lambda function URLs provide a highly convenient method of calling your functions in a serverless manner (API Gateway is also serverless by the way).
Creating A Lambda Function URL
On the AWS Lambda console, you can click on functions in the left sidebar. You’ll be directed to your functions list page, there you will see a Create Function orange button — click that.
Create your function, filling in the details, function name, runtime you want to use (i’m using node v20 — but that doesn’t make a difference).
Don’t forget to set the necessary permissions for your Lambda function:
Once you have set the permissions your function needs, you can go ahead and create the function.
This will redirect you to the function page.
As you can see above, the Function URL field has no value. Let’s add one now!
Scroll down a little and you will see a menu bar. Select Configuration.
Click on the Create function URL button. You’ll be redirected to the function URL creation page.
Here you can choose your authentication type — either IAM or none to let anyone access it. You also want to check the CORS option to configure CORS for your function.
Here you can set the values you want, just remember that the configuration you set here must match exactly the configuration you write in your fetch request in your code, otherwise you will have CORS errors, which can often be hard to debug.
I will leave the default on and proceed to create the function URL:
This will allow origin from anywhere and allows any method (GET, POST, etc)
Once you create the function URL, you will see the URL now visible in your function page:
Calling your Lambda function
Now you are ready to call your lambda function from your front end. Don’t forget to add code to at least return a “Hello World” or something to verify you get a response.
We will copy the newly generated function URL provided in the lambda function and add it in our fetch request on our front end code.
fetch("https://dfs54yrsfs4rhusdfg.lambda-url.us-east-1.on.aws/")
.then(data => {
console.log('Data received:', data.json());
// Do something with the received data
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
And there you have it, you should be seeing the output of your lambda function code in your console.
Conclusion
In summary, making use of Lambda function URLs is a powerful way to expose endpoints for our serverless lambda functions without using rest APIs or API Gateway. This convenience lets us not worry about managing APIs and allows us to call our functions directly using the generated endpoint URL. This streamlines the development and calling of server code and allows us to be more agile or used to quickly test and deploy code.
I hope you learned something of value and practical to make use of for your next project!
Click here to say hi 👋, collaborate with me or explore my services 🤝. Clap 👏 for this story if you liked it and follow me for more content.
Uriel