Sonia Manoubi
crunchcrunch

crunchcrunch

Lambda function URLs: deploy and test with CDK

Photo by Remotar Jobs on Unsplash

Lambda function URLs: deploy and test with CDK

Sonia Manoubi's photo
Sonia Manoubi
·May 26, 2022·

3 min read

Table of contents

  • Definition
  • Pricing
  • Difference with API Gateway
  • Create and deploy the lambda with CDK
  • On the console
  • Test our lambda
  • Conclusion

In this article, we are going to discover a new feature from AWS Lambda : function URLs.

Definition

A function URL is a dedicated HTTP(S) endpoint for your Lambda function.
The endpoints have the following format:

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

Pricing

Function URLs are included in Lambda’s request and duration pricing.

Difference with API Gateway

Function URLs are best for use cases where you must implement a single-function microservice with a public endpoint that doesn’t require the advanced functionality of API Gateway, such as request validation, throttling, custom authorizers, custom domain names, usage plans, or caching.
For example, when you are implementing webhook handlers, form validators, static html file...
It is also the simplest way to invoke your Lambda functions during development without leaving the Lambda console or integrating additional services.

Create and deploy the lambda with CDK

Let's create a new project

mkdir lambda-url
cd lambda url
cdk init

lib/lambda-url-stack.ts

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

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

    // The code that defines your stack goes here

    const myFunction= new lambda.Function(this, 'LambdaHandler', {
      runtime: lambda.Runtime.NODEJS_14_X,    // execution environment
      code: lambda.Code.fromAsset('lambda'),  // code loaded from "lambda" directory
      handler: 'index.handler'                // file is "index", function is "handler"
    });

    const fnUrl = myFunction.addFunctionUrl({  // your function url
      authType: lambda.FunctionUrlAuthType.NONE,
    });

    new CfnOutput(this, 'TheUrl', {
      value: fnUrl.url,
    });
  }
}

At the root of your project, (next to lib and bin), create a lambda folder and inside it, an index.js file

# at the root of your project
mkdir lambda
cd lambda
touch index.js

lambda/index.js (let's render some html ! )

exports.handler = async function(event) {
    console.log("request:", JSON.stringify(event, undefined, 2));
    const body = '<html><head><title>HTML from Lambda URL</title></head>' + 
    '<body><h1>Hello you ! This is served by a Lambda function URL</h1></body></html>'
    return {
      statusCode: 200,
      headers: { "Content-Type": "text/html" },
      body
    };
  };

Everything is ready ! Now let's deploy

❯ cdk deploy

✨  Synthesis time: 14.84s

LambdaUrlStack: deploying...
[0%] start: Publishing XXXXXXXXXXXXXXXXX:current_account-current_region
[0%] start: Publishing XXXXXXXXXXXXXXXXX:current_account-current_region
[50%] success: Published XXXXXXXXXXXXXXXXX:current_account-current_region
[100%] success: Published :XXXXXXXXXXXXXXXXXcurrent_account-current_region
LambdaUrlStack: creating CloudFormation changeset...

 ✅  LambdaUrlStack

✨  Deployment time: 45.96s

Outputs:
LambdaUrlStack.TheUrl = https://XXXXXXXXXXXXXXXXX.lambda-url.us-east-1.on.aws/
Stack ARN: XXXXXXXXX:stack/LambdaUrlStack/XXXXXXXX

✨  Total time: 60.79s

You can find the code on this repository

On the console

Go to Lambda , you should see the one you just created.

Click on it, and you should see your function URL (see capture)

Capture d’écran 2022-05-26 à 14.45.18.png

Test our lambda

Copy the function URL and paste in your browser, hit enter and you should see this :

Capture d’écran 2022-05-26 à 14.51.18.png

Conclusion

Congrats ! You know now how to use Function URLs for Lambda.

To go further : AWS Documentation

See you in the next one :)

Did you find this article valuable?

Support Sonia Manoubi by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this