Function URLs and Keeping AWS Lambda Warm and Healthy
Tags: aws, lambda, terraform
Published: 2023-01-13 22:20:42

The [terraform-aws-lambda-warmer](https://github.com/Nuagic/terraform-aws-lambda-warmer) repository is a useful tool for keeping your AWS Lambda functions warm and reducing cold start latency. Recently, I discovered that Lambda functions now allow for [function URLs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html) that can host a server over HTTPS without the need for additional tooling such as AWS Gateway, etc. These are perfect for behind the scenes APIs where you do not necessarily need a human friendly custom URL. With this in mind, I decided to [fork the repository](https://github.com/chaz8080/terraform-aws-lambda-warmer) and make an update to the default event to call a `/health` endpoint.

## Why call a `/health` endpoint?

Firstly, the [health endpoint monitoring pattern](https://learn.microsoft.com/en-us/azure/architecture/patterns/health-endpoint-monitoring) is a widely employed technique, so if you move away from Lambda, it will not be wasted effort. Monitoring the health of your Lambda functions is crucial for ensuring that they're running smoothly and that any issues are caught quickly. By calling the health endpoint at regular intervals, you can check that your functions are running as expected and take action if they are not. This can help to prevent downtime and ensure that your users are getting the best possible experience.

Additionally, calling a health endpoint also helps to keep the Lambda warm, reducing latency. Cold starts can be a major performance bottleneck, and by keeping your functions warm, you can ensure that they're ready to go when they're needed. Furthermore, emitting a CloudWatch event on failure can aid in monitoring, as it will trigger an alert and allow you to take action.

## How to use the fork

To use my [fork of the repository](https://github.com/chaz8080/terraform-aws-lambda-warmer), you'll need to set up the health endpoint and configure the warmer. Assuming that you are hosting your server on a Lambda function and deploying your function with terraform, here is an example to add the warmer:

```terraform
resource "aws_lambda_function" "my_lambda_function" {
  # Configuration for your Lambda function
}

module "my_lambda_function_warmer" {
  source        = "https://github.com/chaz8080/terraform-aws-lambda-warmer"
  function_name = aws_lambda_function.my_lambda_function_warmer.function_name
  function_arn  = aws_lambda_function.my_lambda_function_warmer.arn
}
```

If your health endpoint's path is something other than `/health`, then you can override the `input` variable, such as:

```terraform
resource "aws_lambda_function" "my_lambda_function" {
  # Configuration for your Lambda function
}

module "my_lambda_function_warmer" {
  source        = "https://github.com/chaz8080/terraform-aws-lambda-warmer"
  function_name = aws_lambda_function.my_lambda_function_warmer.function_name
  function_arn  = aws_lambda_function.my_lambda_function_warmer.arn
  input         = "{\"body\":null,\"httpMethod\":\"GET\",\"path\":\"/api/v2/health\",\"requestContext\":{}}"
}
```

This will set up the resources to invoke your Lambda function every 5 minutes by default. See the other variables that can be overridden [here](https://github.com/chaz8080/terraform-aws-lambda-warmer/blob/main/variables.tf).

## Conclusion

By using [this fork](https://github.com/chaz8080/terraform-aws-lambda-warmer), you can keep your Lambda functions warm that are utilizing function URLs. I hope you find it useful and encourage you to try it out and provide feedback.

It should be noted that [SnapStart](https://techcrunch.com/2022/11/28/aws-makes-lambda-cold-start-latency-a-thing-of-the-past-with-snapstart) is a recent solution to the cold start issue as well, but appears to be limited to Java at the time of writing.