avatarUriel Bitton

Summary

AWS Lambda Function URLs offer a direct method to invoke Lambda functions without the need for API Gateway, providing a simple and serverless approach to front-end integration with backend functions.

Abstract

AWS Lambda Function URLs enable developers to deploy serverless functions with direct endpoint access, bypassing the complexity of managing REST APIs through API Gateway. This feature simplifies the process of calling Lambda functions from the front-end using a simple fetch request. While AWS Lambda Function URLs offer convenience and security through CORS and IAM permissions, they do not entirely replace API Gateway, which provides additional capabilities such as API lifecycle management, throttling, caching, and custom domain names. The article guides readers through the process of creating a Lambda Function URL, configuring authentication and CORS, and demonstrates how to call the function from front-end code. The author emphasizes the practicality of this approach for streamlining development and testing, making it a valuable tool for developers working with serverless architectures.

Opinions

  • The author suggests that API Gateway, despite its comprehensive features, may introduce unnecessary complexity for simple use cases where Lambda Function URLs suffice.
  • Lambda Function URLs are praised for their ease of use and the ability to reduce development overhead by eliminating the need for API Gateway management.
  • The author highlights the importance of proper CORS configuration to avoid difficult-to-debug errors, implying that this is a common pitfall for developers.
  • While acknowledging the powerful features of API Gateway, the author implies that these features may not always be necessary, and Lambda Function URLs can be a more straightforward solution for certain scenarios.
  • The author encourages readers to explore and potentially adopt Lambda Function URLs for their projects, suggesting confidence in the feature's utility and effectiveness.

What Are AWS Lambda Function URLs And Why You Don’t Need API Gateway

Deploy Lambda function URLs without building and maintaining rest APIs.

Photo by Thom Milkovic on Unsplash

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.

Author Image

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:

Author Image

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.

Author Image

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.

Author Image

Click on the Create function URL button. You’ll be redirected to the function URL creation page.

Author Image

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:

Author Image

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:

Author Image

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

AWS
AWS Lambda
Lambda Function
Api Gateway
Serverless
Recommended from ReadMedium