intotecho
intotecho

Reputation: 5684

What is the syntax for specifying labels for a cloud storage bucket with terraform?

I can create a Google Cloud Storage Bucket using the Terraform resource google_storage_bucket.

But I am unable to find an example with the labels argument, or find a syntax that works.

The following passes terraform plan:

resource "google_storage_bucket" "report_staging" {
  name                        = "some-unique-name"
 
  labels = {
    key = "env" 
    value = "dev"
  }
}

but gives a 400 error on terraform apply

╷
│ Error: googleapi: Error 400: Invalid argument., invalid
│
│   with module.ndpe_project.google_storage_bucket.report_staging,
│   on ....main.tf line 130, in resource "google_storage_bucket" "report_staging":
│  130: resource "google_storage_bucket" "report_staging" {

I believe the above syntax would actually create two keys called 'key' and 'value'. This will also pass the plan stage.:

  labels = {
    environment = local.env 
    created_by  = "me"
  }
}

Upvotes: 2

Views: 3154

Answers (1)

John Hanley
John Hanley

Reputation: 81444

Use this syntax:

labels = {
  created_by = "me"
  deleted_by = "me"
}

Key/value pairs can be separated by either a comma or a line break.

The keys in a map must be strings; they can be left unquoted if they are a valid identifier, but must be quoted otherwise. You can use a non-literal string expression as a key by wrapping it in parentheses.

Maps/Objects

Upvotes: 5

Related Questions