Published on

How to create a Lambda URL Function using the AWS CDK

Authors
  • avatar
    Name
    Andreas Caldewei
    Twitter
How to create a Lambda URL Function using the AWS CDK (2022)

In this blog post we will learn on how to create a Lambda URL Function using the AWS CDK.

Overview

What is a Lambda URL Function?

A Lambda URL Function can be invoked by a URL. Lambda automatically generates a unique URL endpoint for you that never changes.

How does it work?

When creating a Lambda Function you can specify that the lambda should be a Lambda URL Function.

What can you do with it?

It is possible to create HTTP Endpoints for Lambdas without the need of an AWS Api Gateway.

Example

This example gives an overview on how to implement a Lambda URL Function in a CDK project.

Let´s write the code for a simple Lambda Function.

// /lib/cdk-lambda-urls-stack.ts

import { Stack, StackProps } from 'aws-cdk-lib'
import { Construct } from 'constructs'
import * as lambda from 'aws-cdk-lib/aws-lambda'
import * as cdk from 'aws-cdk-lib'
import * as path from 'path'

export class CdkLambdaUrlsStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props)

    // 👇 Create lambda function
    const lambdaFunction = new lambda.Function(this, 'lambda-function', {
      runtime: lambda.Runtime.NODEJS_14_X,
      handler: 'index.main',
      code: lambda.Code.fromAsset(path.join(__dirname, '/../lambda')),
    })
  }
}

In the code snippet above, a Lambda Function is instantiated and the location for the handler we specified. Next, let`s add the code for the handler.

// /lambda/index.js

async function main(event) {
  return {
    body: JSON.stringify({ message: 'Hello there 👋🏻' }),
    statusCode: 200,
  }
}

module.exports = { main }

The request and response event formats follow the same schema as the Amazon API Gateway payload format version 2.0. Further documentation can be found here Invoking Lambda Function URLs.

Let´s add the URL endpoint to lambda.

// /lib/cdk-lambda-urls-stack.ts

import { Stack, StackProps, CfnOutput } from 'aws-cdk-lib';
import { Construct } from "constructs";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as cdk from "aws-cdk-lib";
import * as path from "path";

export class CdkLambdaUrlsStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    ...

    // 👇 Setup lambda url
    const lambdaUrl = lambdaFunction.addFunctionUrl({
      authType: lambda.FunctionUrlAuthType.NONE,
    });

  }
}

To create a lambda URL endpoint we are calling the addFunctionUrl on the Lambda Function. Here we have to pass in an authType, which decides what permissions we need to call the lambda. For now, let's take FunctionUrlAuthType.NONE to make the lambda publicly available, but there are more options on how to configure Lambda Function URLs authorization.

To get the URL of the Lambda Function after the deployment we are adding one line.

// /lib/cdk-lambda-urls-stack.ts

import { Stack, StackProps, CfnOutput } from 'aws-cdk-lib';
import { Construct } from "constructs";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as cdk from "aws-cdk-lib";
import * as path from "path";

export class CdkLambdaUrlsStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    ...

    // 👇 print lambda url after deployment
    new CfnOutput(this, 'FunctionUrl ', { value: lambdaUrl.url });

  }
}

The CfnOutput class will print out the URL after we successfully deployed it. Let´s deploy.

cdk deploy

After the deployment, we should see the URL in the console.

✨  Deployment time: 63.55s

Outputs:
CdkLambdaUrlsStack.FunctionUrl = https://ueu4apq4m7f3f5wjpvc4f34547q0irijy.lambda-url.us-east-1.on.aws/
Stack ARN:
arn:aws:cloudformation:us-east-1:474737036145:stack/CdkLambdaUrlsStack/f64d0860-e421-11ec-9ce0-0e76f43425b7

Let´s see if the endpoint works by visiting the URL in the browser.

{ "message": "Hello there 👋🏻" }

The Lambda Function is now working and can be called without using an API Gateway. Don´t forget to destroy the stack again.

cdk destroy