AWS Lambda Function with Function URL

In this blog, we'll learn how to execute an AWS Lambda function using a Function URL without using API Gateway. This step-by-step guide will help you understand the process, from setting up the Function URL to testing it with GET and POST requests. Let’s dive in!

What is a Function URL?

AWS Lambda provides a Function URL that allows direct execution of a Lambda function via a unique URL. Unlike API Gateway, this approach simplifies the process by enabling execution without an intermediary API layer.

Enable Function URL

For Existing Functions

  1. Open your Lambda function and go to the Configuration tab.
  2. Locate the Function URL section and click on Create Function URL.
  3. Set the authentication type to None.
  4. Save the settings. A URL will be generated for your function.

For New Functions

  1. While creating a new function, provide a name and scroll to Additional Configuration.
  2. Check the option to Enable Function URL.
  3. Select the authentication type as None and complete the creation process.

Testing the Function URL

Once you have the Function URL, you can test it by pasting it into your browser. Initially, you might encounter an Internal Server Error. This happens because the Lambda function expects input values that weren’t provided.

To resolve this, modify your Lambda function code to include default values:

def lambda_handler(event, context):
    number1 = event.get("number1", 10)
    number2 = event.get("number2", 20)
    return {
        "statusCode": 200,
        "body": f"The sum is {number1 + number2}"
    }

Deploy the changes and re-test the URL. This time, the function should return the output directly.

Using Function URL with HTTP Methods

The Function URL supports multiple HTTP methods, including GET and POST.

GET Method

  1. Pass values directly in the URL path, e.g., /number1/number2.
  2. These values will be available in the event object.

POST Method

  1. Use tools like Postman to send a POST request.
  2. Include input values in the request body as JSON.

Example JSON payload for POST:

{
  "number1": 15,
  "number2": 25
}

Reading Input in Lambda

Use the event object to access inputs:

  • Path Parameters: Available as event['path'].
  • Request Body: Parse the raw input into JSON.

Viewing Logs in CloudWatch

To debug or verify the input, view logs in CloudWatch:

  1. Go to the Monitoring tab of your Lambda function.
  2. Open the View CloudWatch Logs link.
  3. Check the latest log stream for event object details and other metadata.

Monitoring Metrics

AWS Lambda provides essential metrics to monitor the function’s performance:

  • Invocation Count: Number of function triggers.
  • Error Count: Number of errors encountered.
  • Duration: Time taken to execute.
  • Success Rate: Percentage of successful invocations.

These metrics help in understanding and optimizing the function's behavior.

Key Learnings

In this blog, we explored:

  1. What Function URL is and how it simplifies Lambda execution.
  2. How to enable and test Function URL for both GET and POST methods.
  3. Debugging and monitoring using CloudWatch Logs.

For more such tutorials and hands-on guides, visit Learning Ocean. Explore a wide range of content and enhance your learning journey today!