Reputation: 19
I have been following this tutorial to run global services with Google Cloud Run using Terraform.
That is enabled by using a data source to retrieve all Cloud Run regions as a list and deploying in all of them;
data "google_cloud_run_locations" "default" { }
Then, deploying the Cloud Run service using for_each
construct in HCL:
for_each = toset(data.google_cloud_run_locations.default.locations)
I want to achieve something similar be able to add specific / limited regions as opposed to deploying in all regions. For example, to a list I declare in terraform.tfvars
.
I suppose there are slight modifications to be made in case that is possible.
More information:
As per the official docs, I can specify a location where I want to run my service.
This link shows how to configure cloud run to deploy to all available regions.
What I want to do is to deploy to more than one region (but not all) with Terraform, e.g.
["us-west1", "us-central1", "us-east1"]
Is it possible or would I need to change the data source that retrieves all Cloud Run regions?
Upvotes: 0
Views: 207
Reputation: 28739
The data for google_cloud_run_locations
does not allow filtering because the API endpoint only supports returning all possible locations. Therefore, we need to do the filtering in Terraform DSL. There is no intrinsic function that is equivalent to a filter
, select
, etc. from other languages. Therefore, we need a for
lambda expression here.
All possible locations are stored in the attribute data.google_cloud_run_locations.default.locations
, so we would filter on that list with a regular expression. Given the example in the question of limiting to the list ["us-west1", "us-central1", "us-east1"]
:
for_each = toset([for location in data.google_cloud_run_locations.default.locations : location if can(regex("us-(?:west|central|east)1", location))])
The conditional selects only the locations which match the regular expression because the can
function returns a boolean
type for a successful or unsuccessful match. The regular expression can be easily modified for a different subset of locations if necessary.
Upvotes: 2