Handling % character in remote resource names

Hi,

Just wondering if there’s and easy way to handle resource names with % character in the name.

When trying to provision a google pubsub subscription “hello%world”. Terraform creates the resource fine, but then subsequent changes error out showing this error - `Error: Error when reading or editing Subscription: - : invalid URL escape “%wo”. I know it’s not ideal to have % in the name. But we have a number of production resources that have this naming convention and now it’s quite an effort to recreate them.

I have tried urlencode for the resource name, but the problem persists.

thanks.

Hi @vikas.saroha ,

Unfortunately, there are constraints about Terraform resource names as specified in the documentation in the last Note of the resource syntax paragraph
Here is an extract :

Note: Resource names must start with a letter or underscore, and may contain only letters, digits, underscores, and dashes.

The Terraform resource name can be different from its name in the managed backend. I don’t know much about google pubsub subscription. But if I take the example of an AWS EC2 instance : The VM name can be vm001 in the AWS console and in terraform I can have the following block

resource "aws_instance" "vm-001" {
  ...
  tags = {
    Name = "vm%001"
  }
}

Thanks for your reply @bassam.benhamouda. Sorry I should have clarified, the resource name I was referring to is for a remote resource on GCP.

resource "google_pubsub_subscription" "hello-world" {
  name = urlencode("hello%world")
}

The resource could be created with terraform apply but any subsequent changes to it aren’t possible as it throws this error - invalid URL escape “%wo”.

│ Error: Error when reading or editing Subscription: parse "https://pubsub.googleapis.com/v1/projects/abcd-144234/subscriptions/hello%world": invalid URL escape "%wo"

So the issue is with the attribute name of the resource google_pubsub_subscription.
The provider documentation do not state any limitation regarding the characters to use for the attribute :

name - (Required) Name of the subscription.

The Google API documentation do not state any limitation either :

Required. Name of the subscription. Format is projects/{project}/subscriptions/{sub}.

Looks like a bug to me (or may be a non documented limitation), you might want to open an issue in the google provider github

1 Like