How to Create Lambda function URLs
Tags: aws, api, lambda, serverless
Published: 2022-04-10 14:52:06

AWS recently introduced AWS [Lambda Function URLs](https://aws.amazon.com/blogs/aws/announcing-aws-lambda-function-urls-built-in-https-endpoints-for-single-function-microservices/). Built-in HTTPS Endpoints for Single-Function Microservices. It helps users to configure an HTTPS endpoint for a Lambda function without using any other AWS services such as AWS API Gateway or Application Load Balancer.

First of all, go through Architecture Diagram.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zukex1tdy8wsrtx9wl2p.png)

## Create IAM role for Lambda function

In AWS management console search and go inside IAM service. Next create IAM Role for lambda function. Use Trusted entity type as **AWS service** and Use case as **Lambda**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqnh7oawb2cuas9pouur.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wllhrmbswsu1f08rw4rd.png)

Use permissions as **AWSLambdaBasicExecutionRole** AWS managed policy.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjz3nm24ldrpw83soj9b.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89q1xet3dbvbmv9vyev2.png)

Type role name as **function-url-role**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wez62l11kzgz3cvtswze.png)

Finally, click **Create role** button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8dug3zv6jmtwcaqkpcqj.png)

## Create Lambda function

* In AWS management console search and go inside Lambda service. 
  Next click Create function button. Select Author from scratch 
  and given function name as **function-url-demo**.
* Select runtime as Python 3.9.
* Expand Execution role and select Use an existing role. after 
  that select previos step created Role name as         
  **function-url-role**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8j2jd57573l80cnaync.png)
* After that expand Advanced settings and select **Enable Function 
  URL** and Auth type as **NONE**.
* Finally click Create function button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7b1ue9s8l7r9i4d0py99.png)

## Test the function

* Add following code part for **Code source** section 
  lambda_function.py file.

lambda_function.py
```
import json
def lambda_handler(event, context):
    body = "Hello Lambda Function URL"
    statusCode = 200
    return {
        "statusCode": statusCode,
        "body": json.dumps(body),
        "headers": {
            "Content-Type": "application/json"
        }
    }
```
Click Deploy button to deploy code.

* After that we can Test the function.
  Go to Test section and add test event. Give the event name as 
  **Test1** and select hello-world template. Click save button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hf4awekfxqtb12hbth0u.png)
  Next click test button. you can see output as following.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9443vwufgg06t6bi30ia.png)

## Test the function URL endpoints

You can use curl or Postman.
* You can get function URL in Function overview section or go to 
  configurations section and copy Function URL.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t09rqfhekdf1qpzi7cdo.png)

* You can use curl command. paste it your terminal, You can see 
  response.
`curl -X GET '{{Your Function URL}}' -H 'Content-Type: application/json'`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pi975hhlrkui8oehwabb.png)

* In postman you use GET method & paste Function URL.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y9hdyf74ilkcai7ct0te.png)

By deleting AWS resources no longer using, you can prevent unnecessary charges to your AWS account. Open the Functions page in the Lambda console and select the function and go action section. After that click the Delete button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vy2lugih1i0us33q8lgt.png)

Thanks for reading the Article.

References - https://aws.amazon.com/blogs/aws/announcing-aws-lambda-function-urls-built-in-https-endpoints-for-single-function-microservices/