DEV Community

Charles Scholle
Charles Scholle

Posted on

Function URLs and Keeping AWS Lambda Warm and Healthy

The 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 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 and make an update to the default event to call a /health endpoint.

Why call a /health endpoint?

Firstly, the health endpoint monitoring pattern 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, 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:

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
}
Enter fullscreen mode Exit fullscreen mode

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

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\":{}}"
}
Enter fullscreen mode Exit fullscreen mode

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.

Conclusion

By using this fork, 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 is a recent solution to the cold start issue as well, but appears to be limited to Java at the time of writing.

Top comments (0)