Expose your Lambda with Function URLs

Expose your Lambda with Function URLs

Clock Icon2022.04.07

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Cloud-native application aims for scalability, resiliency, performance and high availability, all these principles are fulfilled to a great extent by microservices architecture.

AWS Lambda is a pioneer in a microservices architecture. A typical serverless application is composed of a number of lambda functions. In order to expose these functions, we need to map them to the application load balancer or the eminent API Gateway. Exposing Lambda functions requires configuration and additional steps.

But what if we want to implement a single function microservice that is simple and doesn't require advanced functionalities like authentication or caching. All we want is a single lambda function that can be used by public users. In such a case configuring API Gateway or ALB is troublesome.

Then how do we expose lambda? AWS LAMBDA, now supports Function URL's

Do check out the Developer and Solutions Architect POV in the end for Lambda Function Url's.

What is Lambda Function URLs?

In simple words, it allows us to add an HTTPS endpoint or expose your lambda function without having to configure or operate on API Gateway or ALB.

When you create a function URL, Lambda automatically generates a unique URL endpoint for you. Function URL endpoints have the following format:

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

Note:- url-id is based on the account-id and other factors, it might be possible for someone to determine your account-id.

Features of Lambda Function URLs?

  • Allows configuring CORS (optional)

  • Associate with functions ALIAS or $LATEST version.

  • Supported by Console, SDKs, CloudFormation, SAM and CDK.

How to create Lambda Function URLs?

  • AWS Lambda team has provided such great flexibility that function URLs can be created either when you are creating a new function or when you want to add function URLs to an existing function.

For new function

  • In the console, create a function, add a name and check on advanced settings.
  • Click on enable function URL.
  • There are two options choose auth type:-

    • AWS_IAM:-only iam users can make requests to function URL.
    • NONE:- allows anyone to make a request on function URL. if you want some sort of authorization, you need some custom logic inside your function.

    before adding URL

    advanced settings

    function url created

  • There is also an option to enable CORS; the Default policy allows all origins, for more granular control, you need to edit it after you have created the function.

For existing function

  • CLick on configuration -> function URL and edit.

settings

function url while creating

AFTER ADDING URL

Lambda Code and Function URL demo

  • Lamba code
    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;
    };
    
    
    • curl request
curl https://b5vemor3wrwrxmi4ift4b6z6640dvkyn.lambda-url.ap-northeast-1.on.aws/\?customparamter\='hello' -X POST -H "Content-type:application/json"

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

Developer / Solutions Architect POV

  • If a website/browser will work with our API, we will need to include a few CORS headers that declare which origins, methods, and custom headers are allowed. This can be easily updated and edited in console or IaC templates.

  • Function URLs allow multiple URLs for the same function by mapping them to $LATEST or specific alias. This allows pushing code in production in a tested and efficient manner using deployment strategies.

  • In terms of cost, function URLs are included in duration and request pricing.

  • Function URLs should not be seen as alternatives to API Gateway. Function URLs should be used:-

    • when we want to use single-function architecture for our microservice.
    • When we don't want to use API Gateway features like custom authorizers, caching, custom domains, usage plans, built-in WAF support.

This is great functionality for use-cases when we need to research or try lambda. It definitely simplifies development by reducing the complexities and simplifying serverless architectures, especially in single-function lambda function.

Till then, Happy Learning!

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.