Problem
When using shell environments like PowerShell or Bash, you may encounter character escaping issues with commands such as terraform import or terraform state show. This is common when targeting a resource created with a for_each argument, because the resource address contains double quotes that are interpreted by the shell.
For example, a resource address like random_uuid.main["one"] will cause an error because the shell attempts to process the special characters ([ and ") instead of passing them literally to Terraform.
Prerequisites
- Terraform CLI installed.
- A shell environment such as PowerShell or Bash.
Procedure
Consider a resource block that uses for_each to create multiple instances.
resource "random_uuid" "main" {
for_each = toset(["one", "two"])
}This configuration creates two resources with the addresses random_uuid.main["one"] and random_uuid.main["two"].
To correctly target one of these resources with a Terraform command, you must enclose the entire resource address in single quotes ('). This ensures the shell treats the address as a single, literal string and prevents it from interpreting the double quotes and brackets.
Execute the terraform import command with the resource address enclosed in single quotes.
$ terraform import 'random_uuid.main["one"]' aabbccdd-eeff-0011-2233-445566778899
This same quoting technique applies to other commands that require a resource address, such as terraform state show.
$ terraform state show 'random_uuid.main["one"]'