AWS Lambda Function URLs

Cagdas Ozbey
TysonWorks
Published in
4 min readFeb 22, 2023
AWS Lambda

AWS Lambda is a Serverless computing service offered by Amazon Web Services (AWS) that allows developers to run code without provisioning or managing servers. In this tutorial, we will explore AWS Lambda Function URLs, which are the endpoints that allow you to invoke your Lambda functions.

AWS Lambda Function URLs are unique HTTP endpoints that you can create using AWS Console, SDK or any other IaC tool. These URLs are used to trigger your Lambda function, and they can be integrated with a variety of workloads. Function URLs are dual stack-enabled, supporting IPv4 and IPv6. After you configure a function URL for your function, you can invoke your function through its HTTP(S) endpoint via a web browser, curl, Postman, or any HTTP client.

Once you create a function URL, its URL endpoint never changes. Function URL endpoints have the following format:

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

Authorization

The AuthType parameter determines how Lambda authorizes requests to your function URL. Depending on the configuration you choose, resource-based policies can enable other AWS accounts to invoke the Lambda function. To achieve more accurate access control, you can even combine two options.

When configuring your function URL, you need to specify one of the following AuthType options:

AWS_IAM : This option uses IAM to authenticate and authorize requests based on the IAM principal’s identity policy and the function’s resource-based policy. You should choose this option if you want only authenticated users and roles to invoke your function via the function URL.

NONE : This option does not perform any authentication before invoking your function. However, your function’s resource-based policy is always in effect, and you must grant public access to your function URL before it can receive requests. Choose this option only when you want to allow public, unauthenticated access to your function URL.

Now that we’ve covered the basics, let’s create a simple Lambda function to get our hands on Function URLs. I will use Cloud Development Kit(CDK) to provision necessary resources.

If you don’t have the CDK installed, please refer to this documentation.

//index.ts
import * as cdk from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import { config } from "dotenv";
import { Construct } from "constructs";
config();

class LambdaFunctionURLStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

// Defining a lambda function
const lambdaFunction = new lambda.Function(this, "function-url", {
functionName: "function-url",
runtime: lambda.Runtime.NODEJS_18_X,
code: new lambda.AssetCode("src"),
handler: "lambdaHandler.handler",
timeout: cdk.Duration.seconds(60)
});

// cors options, allowing all origins
const cors = {
allowedOrigins: ['*'],
};

//adding function url to the lambda function, and specifying no-auth
const functionURL = lambdaFunction.addFunctionUrl({authType: lambda.FunctionUrlAuthType.NONE, cors});

//Output for the function URL
new cdk.CfnOutput(this, "URLS", {
value: functionURL.url
});

}
}

const app = new cdk.App();
new LambdaFunctionURLStack(app, "LambdaFunctionURLStack", {
env: {
account: process.env.AWS_ACCOUNT_ID,
region: process.env.AWS_REGION
}
});
// src/lambdaHander.mjs
export async function handler(event, context, callback) {
try {
console.log("Event:", event);
return callback(null, "Hello, function URL");
} catch(err) {
console.error("Error",err);
return callback("Error")
}
}

This is a very simple stack, we are defining a Lambda function, and adding source code as a CDK asset. For the last step, we are creating a function URL for the Lambda function.

Let’s deploy this stack and see the results.

If you haven’t done already, bootstrap your CDK environment via CDK CLI.

cdk bootstrap

We can initiate the deployment.

cdk deploy

After it’s done, you will see the output URL. Let’s use the curl command to test our new endpoint.

curl --location --request GET 'https://xxxxxxxxxxxxxx.lambda-url.us-west-2.on.aws/' \
--header 'Content-Type: application/json'

Head over to the CloudWatch Console and check the logs for the event type. Once you are done with the stack don’t forget to remove the resources used in this tutorial.

cdk destroy

In this tutorial, we have explored AWS Lambda Function URLs, which are the endpoints that allow you to invoke your Lambda functions. Function URLs can be an alternative if you don’t plan to use API Gateway or Application Load Balancers.

Are you ready to enhance your AWS Cloud journey? Head over to our website and book a free consultation call

--

--