Search code examples
google-cloud-platformrate-limitinggoogle-cloud-load-balancergoogle-cloud-armor

Add x-rate-limit response headers to the load balancer


I configured a Cloud Armor rate limiting rule and i would like to attach the following custom response headers to my global external HTTP(S) load balancer backend.

x-ratelimit-limit: The maximum number of requests available in the current time frame.
x-ratelimit-remaining: The number of remaining requests in the current time frame.
x-ratelimit-reset: A UNIX timestamp of the expected time when the rate limit will reset.

In the custom headers documentation no rate limit variable seems to exist. There is however a notice about Cloud Armor Response Headers further down:

You can configure Google Cloud Armor to insert a custom header and value.

I found the --request-headers-to-add rule setting but i need some way to access the current Cloud Armor rate limit values.

Is my use case not possible or can anyone point me in the right direction?


Solution

  • You can create a Cloud Function to retrieve Cloud Armor rate limit values to do this:

    • Create a code with Cloud Function which retrieves the Cloud Armor rate limit values and sets the custom response headers. Use Google Cloud Client Libraries for this and deploy the code into your project.
    • Now configure your global external HTTP(S) load balancer to route requests through the Cloud Function. Create a backend service which points to the Cloud Function URL and then add backend service to the load balancer backend configuration. Please check Cloud Load Balancing official documentation.