Using-AWS-Lambda-Function-URLs

Cloud-native applications have been trending for a number of years in the search for scalability, reliability, and wide availability. And, microservices are one of the more popular configurations to address these needs. AWS Lambda is one of the most popular microservice providers — it’s a serverless environment comprised of serverless applications known as functions.

Until recently, it took some additional configuration via the Gateway API to expose these functions. This lack of exposability has prevented AWS Lambda from being as useful as it could be, as it’s ideal for simple functions that don’t need additional requirements like authentication or caching. This is why the introduction of AWS Lambda Function URLs is such welcome news for developers working with serverless architecture.

Introducing AWS Lambda Functions URLs

AWS Lambda is one of the most popular serverless environments, especially among API developers and designers. It’s easy, intuitive, and painless to set up a REST API using AWS Lambda in just a few minutes.

But, until recently, AWS Lambda functions had to use an API gateway to access Lambda’s functions. This has been addressed with the introduction of AWS Lamdba Function URLs in April 2022, which is a cause for celebration in the AWS developer community.

AWS Lambda is often considered a Function-as-a-Service (FaaS), which means that code will be run when a URL is accessed. Before the introduction of AWS Lambda Function URLs, you had to invoke AWS functions through the API gateway endpoint. Now it’s even easier to integrate AWS Lambda functions into your API collections and automation workflows.

AWS Lambda Function URLs are endlessly customizable with very little effort. You simply have to create a Function URL and map it to any of Lambda’s functions. When that URL is invoked, the $LATEST version of the code will be retrieved and run automatically.

Lambda URLs follow this format:

https://<url-id>.lambda-url.<region>.on.aws

In their announcement, though, they recommend mapping a function URL to an alias. That way, you can create new versions, test integrations, and update as needed.

How To Setup AWS Lambda Function URLs

Next, we’ll show you how to create your own Function URL in AWS Lambda so you can try them out for yourself.

To start, you’ll need an AWS account if you don’t have one already.

Once your AWS account is set up, you can access Lambda via the AWS Management Console.

In the AWS Lambda console, find Create Function in the top-right corner.

First, name your function in the Basic functions section. We’re calling ours’ My-webhook-handler, but you can name your function whatever you like.

Next, you’ll see an option to choose the Runtime Environment. We’re going to use Node.js 14.x.

In the Advanced Settings tab at the bottom of the page, you will find the option to Create Function URL.

You’ll also see all of the other variables that you can customize for your function, including Auth Type. The AWS_IAM option lets you specify who’s able to access your API, which makes it a good option for internal APIs. Selecting AuthType None means that AWS Lambda won’t perform IAM authentication before invoking the function.

All of this is handled automatically when using the AWS Lambda console, though, so there is no need to worry about it for now.

Choose None for now. Specific functions may still require specific permissions in order to be accessed. These can be added manually using the AddPermissions API.

Finally, you can customize your Function URL using the CORS option. Selecting the Configure cross-origin resource sharing (CORS) at the very bottom makes your API consumable by external browsers and websites. It also dictates what custom headers can be created and what methods can be used to interact with them.

When you’re done, click the Create function button in the bottom-right corner.

You’ll be redirected to your brand-new function, where you’ll find a link to your brand-new function URL.

Clicking the Function URL returns the simple Hello World Hello from Lambda!

Tutorial: How To Setup A Lambda Function With a Function URL

To get a feel for how Function URLs function in action, we’ll show you how to create a basic Lambda Function and assign it to its own Function URL.

First, you need to give Lambda permission to access your AWS assets. To do this, start by going to the IAM Roles Manager. First, start by choosing AWS service from the top-left corner of the Trusted Entity Type section. Then choose Lambda from the Use case section and then click the Next button.

On the Add permissions page, select the AWSLambdaBasicExecutionRole option at the top of the list. Click Next again.

Finally, name your role. We’ve called ours lambda-function-URLs.

Now we’re ready to create a Function and Function URL.

To do this, create a file in your development directory using your editor of choice.

Input the following:

exports.handler = async (event) => {

const queryParam = event.queryStringParameters.customparamter
console.log(JSON.stringify(queryParam))

const response = {
    statusCode: 200,
    body: JSON.stringify('Hello from Lamba Function URL.'+ ' You have entered: ' + queryParam),
};
return response;
};

Then save it as index.js.

Next, you’re going to create a deployment package:

zip function.zip index.js

Now you need to deploy that package. You can upload deployment packages using Lambda console, AWS Command Line Interface (AWS CLI), or Amazon Simple Storage Service (Amazon S3) bucket.

We’ll use the Lambda Console. In the top-right corner of Lambda console’s code editor, select Upload from. Choose the function.zip deployment package you create. This will override the code that’s already there.

Now you can test your function. Here’s what that would like using curl:

curl https://bxvfmpr3trwrxxi4ift4b6z6640dvkyn.lambda-url.ap-northeast-1.on.aws/\?customparamter\='hello' -X POST -H "Content-type:application/json"

Remember to substitute your Function URL to invoke the function you’ve just created. When you submit, you should see a result something like this:

"Hello from Lamba Function URL. You have entered: hello"

With these few simple lines of code, you can see how you can use Lambda Function URLs to return a JSON string, input a custom parameter, and assign it to a variable. Even better, it can be done without ever opening a browser or editor (although the AWS Lambda console makes it exceptionally easy to work with and customize function URLs.)

Final Thoughts: AWS Function URLs

Developers and AWS enthusiasts have been clamoring for native function URLs for quite some time. Until recently, it was possible to get AWS Lambda out of the box, but it required tweaking the Gateway API. Now it’s as easy as pushing a couple of buttons.

From a developer’s standpoint, it also makes it easy to create and deploy functions from the command line or in your code itself. This can be a major blessing for creating testing environments.

Keep in mind that Function URLs are not intended to replace the Gateway API — instead, they’re ideal for creating microservices or similar serverless applications.