AWS Lambda Function URLs: Here is How You can Set it Up

AWS Lambda Function URLs: Here is How You can Set it Up

AWS has officially released Lambda Function URLs on April 6th, 2022. That means, now you can call your lambda functions directly from your browser or favorite tool like postman. Read more about the official release here.

In this post, you’ll get to know about lambda function URLs in details. On top of that, you’ll see how to configure it.

To be honest, AWS Lambda has literarily brought a revolution in the serverless computing world. If your are new to lambda, here is a quick overview of it-

AWS Lambda lets you run code without managing servers. That means, as a developer, all you need to worry about is your application logic and rest all is taken care by AWS. Now, isn’t that cool ? Well, of course it is.

You can also checkout my previous post on How to Run AWS Lambda Function Within 5 Minutes to see how it works.

However, If you have worked with lambda, you will agree that, we have always asked these question-

  • How to create AWS lambda Rest API without API Gateway
  • or How to call AWS lambda function from browser
  • or How to call AWS lambda function from postman etc.

Not anymore, because with the release of Lambda Function URLs, all the above question has an easy breezy solution.

Ready to explore?

Alright !!!

Let’s go…

World Before AWS Lambda Function URLs Came into Existence?

Previously, while working with AWS Lambda for creating microservices, you were bound to use API Gateway or Application Load Balancer to expose a publicly available endpoint.

But if you think about it, there are times when you don’t need any fancy functionality of API gateway or ALB. All you need is, a way to call your lambda from anywhere.

AWS now gives you that flexibility by using lambda function URLS. Therefore, if you need a publicly available endpoint of lambda, just say yes to it and it’s easier than ever now. 🙂

How Does AWS Lambda Function URLs Help?

AWS lambda function URLs provides you with a built in https endpoint that you can call from your favorite browser or tools like postman. On top of that you can configure CORS(Cross Origin Resource Sharing) settings as well.

That means you no more need to use an API Gateway or an Application Load Balancer just to create a publicly accessible endpoint.

You can setup AWS Lambda function URLs using AWS management console or API. Once configured, lambda generates a unique endpoint for you. And it looks something like below-

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

url-id part will be uniquely generated by lambda for you. Once you setup, you can use it to call your function from anywhere.

How to Setup AWS Lambda Function URLS?

As we saw that we can set this up from console and API(which means SAM as well). You can configure it while creating a new lambda function or on an existing one as well.

Configure Lambda Function URLs on New Lambda Function

While creating a new lambda function you can find and configure function URLs in Advance Settings section. Click on Advance Settings to expand it as shown below-

Click on Enable function URL checkbox and you’ll get to select Auth type and also enable CORS if you wish.

For Auth type, you can choose AWS_IAM or NONE depending on how you want to control access to the URL. For now I want a publicly accessible endpoint, therefore, I am going with NONE.

When choosing NONE auth type, a resource based policy is attached to your function to allow the lambda to be invoked from anywhere. The policy looks like below-

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "StatementId": "FunctionURLAllowPublicAccess",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "lambda:InvokeFunctionUrl",
      "Resource": "arn:aws:lambda:ap-south-1:123456789012:function:Test",
      "Condition": {
        "StringEquals": {
          "lambda:FunctionUrlAuthType": "NONE"
        }
      }
    }
  ]
}

Once done, click on create function. After successful creation, you will get to see the generated function URL in the function overview as shown below.

Configure Lambda Function URLs on Existing Lambda Function

If you want to enable function URL for an existing function, you can do so using Configurations tab.

Configuration -> Function URL -> Create function URL

Once you click on Create function URL, you are redirected to this screen and rest of the process stays the same as above.

Configure Lambda Function URLs using SAM Template

One of the automated and convenient way to work with lambda is using SAM. Here is a step step tutorial on how to create a node.js lambda using SAM(Serverless Application Model)

Here is how you can configure your lambda with function urls-

      FunctionUrlConfig:
        AuthType: NONE
        Cors:
            AllowOrigins:
                - "https://cloudkatha.com"

If we put it as part of our lambda function, it looks like below-

  TestFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: demo/
      Handler: index.handler
      Runtime: nodejs14.x
      AutoPublishAlias: live
      FunctionUrlConfig:
        AuthType: NONE
        Cors:
            AllowOrigins:
                - "https://cloudkatha.com"

Once you deploy your SAM template, your lambda URL is live and you can start using it to call from your favourite tool.

Conclusion

In this post, we saw how were creating publicly available endpoint of lambda before lambda function urls came into picture.

We learnt about lambda function urls in details, how it can help us and how to set it up using console and SAM template.

Hope this article gave you a bit of insight. Please feel free to let me know in the comment section if you have any question.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

Suggested Read:

Leave a Reply

Your email address will not be published. Required fields are marked *