Using Terraform, I am trying to apply a lifecycle rule for a specific Google Storage bucket prefix. According to Google Cloud documentation (https://cloud.google.com/storage/docs/lifecycle#matchesprefix-suffix), this is possible using matches_prefix = "path"
However, when I run Terraform plan and apply, this matches_prefix
is being ignore and the lifecycle rule is being applied to the whole bucket instead.
This is my current code:
module "my-buckets" {
source = "terraform-google-modules/cloud-storage/google"
version = "3.2"
project_id = var.gcp_project_id
location = "US"
names = ["operational-bucket"]
prefix = var.env_stage #variable is defined. In this case, the value is dev.
versioning = {
operational-bucket = true
}
randomize_suffix = false
lifecycle_rules = [
{
action = {
type = "Delete"
}
condition = {
matches_prefix = "var/logs"
age = 400
}
}
]
}
And this is the result of running terraform plan
(having matches_prefix
empty):
resource "google_storage_bucket" "buckets" {
id = "dev-operational-bucket"
name = "dev-operational-bucket"
# (11 unchanged attributes hidden)
+ lifecycle_rule {
+ action {
+ type = "Delete"
}
+ condition {
+ age = 400
+ matches_prefix = []
+ matches_storage_class = []
+ matches_suffix = []
+ with_state = (known after apply)
}
}
# (1 unchanged block hidden)
}
Anyone already had a similar problem?
The specific debugging information for this would normally be in the module documentation or the variable declaration type specification, but those both omit the helpful information in this module. However, we can also find some helpful information in the release notes that this capability to specify matches_prefix
was added in version 4.0.0. Ordinarily the variable type specification would cause your invalid argument to throw a runtime error, but it is not specified in this module, and so it is merely ignored during Terraform execution.
That root cause explanation aside: you need to upgrade the module from 3.2.0 to 4.0.0. This is a backwards incompatible release, and therefore you may need additional modifications to your module declaration before you can begin using the matches_prefix
argument in the lifecycle_rules
. You can also attempt to switch from using this module to your own module, as the module only contains one config file, and you are only managing one bucket, and so the cost/benefit may be worthwhile.