AWS Lambda Function URLs with Serverless Framework
Tags: aws, lambda, serverlessframework, serverless
Published: 2022-04-20 13:02:35

Originally published at [Serverless](https://www.serverless.com/blog/aws-lambda-function-urls-with-serverless-framework) on April 6th, 2022

![](https://cdn-images-1.medium.com/max/3200/0*vbJH4vQ-o4__UExc.png)

AWS just released a brand new feature called “ [Lambda Function URLs](http://aws.amazon.com/blogs/aws/announcing-aws-lambda-function-urls-built-in-https-endpoints-for-single-function-microservices)”, and we are happy to announce that **Serverless Framework supports Lambda Function URLs immediately**.

A Lambda Function URL is a **simple solution to create HTTP endpoints with AWS Lambda**. Function URLs are ideal for getting started with AWS Lambda, or for single-function applications like webhooks or APIs built with web frameworks.

While not a complete replacement for API Gateway, which provides more features, a Function URL is a simpler alternative with no extra cost and a much larger timeout limit. We will explore the pros and cons in detail in this article.

Using Lambda Function URLs with Serverless Framework can be done via a single statement:
{% gist https://gist.github.com/richardgrantserverless/ff983a70ecc7f3d7d9f6cc56cd6dc2f5.js %}
To use that new feature, [upgrade Serverless Framework](https://www.serverless.com/framework/docs/getting-started#upgrade) (v3.12.0 or greater).

## Lambda URLs in details

Function URL is a new feature of AWS Lambda [that exposes a function as an HTTPS endpoint](http://aws.amazon.com/blogs/aws/announcing-aws-lambda-function-urls-built-in-https-endpoints-for-single-function-microservices).

To deploy a function with a URL, use the url property in serverless.yml:
{% gist https://gist.github.com/richardgrantserverless/e7b69c42bdf423d8525ee212d038508a.js %}
When running serverless deploy, the URL will be created and displayed in the terminal output. The URL will look like this:

[https://yti4le2qudflx43hh.lambda-url.eu-south-1.on.aws/](https://yti4le2qudflx43hh.lambda-url.eu-south-1.on.aws/)

That domain is unique to the Lambda function, and any URI path will invoke the function handler.

The request and response event payloads follow the same format as [API Gateway’s HTTP API “version 2” payload format](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html). That means it is possible to switch from a Lambda Function URL to API Gateway’s HTTP API without any code change.

Here is a simple NodeJS Lambda handler that responds to a Function URL:
{% gist https://gist.github.com/richardgrantserverless/5613068b2523c195a29e3c038a6a769b.js %}
## Endpoint configuration

The endpoint is public by default. It can also be protected by IAM authentication:
{% gist https://gist.github.com/richardgrantserverless/840172e4baf886d6b6c8ab6e5fc97250.js %}
CORS can also be configured on the Function URL:
{% gist https://gist.github.com/richardgrantserverless/bae60e29f61cd5e8d464404b5cd571a3.js %}
View the complete list of options for configuring CORS in the [HTTP API event documentation](https://www.serverless.com/framework/docs/providers/aws/events/http-api/#cors-setup).

Unlike API Gateway, Function URLs do not have a maximum execution time: the execution time is limited by the Lambda function’s timeout (up to 15 minutes).

Note that Function URLs do not support custom domains. The solution would be to use CloudFront with the Function URL as the origin.

## Function URLs vs. API Gateway

Lambda Function URLs let us create an HTTP endpoint for a Lambda function. Let’s address the obvious question: how is that different from API Gateway?

**Function URLs send all HTTP requests to one Lambda function.** This pairs well with backend frameworks like Express or Apollo (Node), Flask (Python), etc. Indeed, these frameworks can handle HTTP routing, authentication, middlewares, and more. Function URLs also work great for single-purpose endpoints like webhooks.

While simple, the downside of this approach is that the whole API is contained in a single Lambda function: this is the “ *Mono-Lambda*” pattern. The deployed package size could be big, debugging could be complex, and permissions are less granular than they could be. You can read more about that topic in [The What, Why, and When of Mono-Lambda vs Single Function APIs](https://dev.to/aws-builders/the-what-why-and-when-of-mono-lambda-vs-single-function-apis-5cig).

**API Gateway filters, processes, and routes requests to different Lambda functions.** This removes the need for using backend frameworks. API Gateway can take care of transforming requests and responses, authenticating clients with different mechanisms, supporting custom domains, managing API access via API keys, and much more.

Note that the exact list of API Gateway features varies between v1 (REST API) and v2 (HTTP API): read [Choosing between HTTP APIs and REST APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-vs-rest.html) to learn more.

While API Gateway provides more features, it also comes with added complexity and costs. Beyond the free tier, API Gateway v1 starts at $3.5/million requests and v2 at $1/million requests. Unlike API Gateway, Lambda Function URLs do not add any cost.

Regarding performance, one could expect Function URLs to add less latency than API Gateway. In practice, the difference is minimal. Here is the latency I measured with a quick test:

* Function URL adds 9 to 10ms to the HTTP response time

* API Gateway v2 adds 12 to 15ms to the HTTP response time

The latency mentioned above is added to the Lambda execution duration.

Let’s compare all this in detail:

![](https://cdn-images-1.medium.com/max/2932/1*DjlFG7F5R8klyZa6PtCO3g.png)

## Use cases

Based on the comparison with API Gateway, we can conclude that Lambda Function URLs could be great for scenarios like:

* APIs built with backend frameworks, like ExpressJS/Apollo/Flask…

* Simple webhooks, where API Gateway could be overkill.

* Server-side rendered websites, built with CloudFront + Lambda (skipping API Gateway).

* APIs with a response time longer than 29 seconds (API Gateway’s maximum timeout), for example to import or process data, or long-polling scenarios.

It’s also worth keeping in mind that Lambda Function events are compatible between Function URLs and API Gateway’s HTTP APIs. That means that Function URLs could be a great way to get started with HTTP on Lambda, and later upgrade to API Gateway to use its features.

Those are just ideas, we can’t wait to see what you build with it! Check out [the Serverless Framework documentation for Lambda Function URLs](https://www.serverless.com/framework/docs/providers/aws/guide/functions#lambda-function-urls) to get started.

*
Originally published at [https://www.serverless.com](https://www.serverless.com/blog/aws-lambda-function-urls-with-serverless-framework).*