Terraform importing a resoure at an index

Viewed 854

My terraform is trying to create the following s3 bucket resources.

  # module.storage.module.s3_bucket[14].aws_s3_bucket.this[0] will be created
  + resource "aws_s3_bucket" "this" {
      + acceleration_status         = (known after apply)
      + acl                         = "private"
      + arn                         = (known after apply)
      + bucket                      = "assets-bucket"
      + bucket_domain_name          = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = false
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)

      + server_side_encryption_configuration {
          + rule {
              + apply_server_side_encryption_by_default {
                  + sse_algorithm = "AES256"
                }
            }
        }

      + versioning {
          + enabled    = (known after apply)
          + mfa_delete = (known after apply)
        }
    }

  # module.storage.module.s3_bucket[14].aws_s3_bucket_public_access_block.this[0] will be created
  + resource "aws_s3_bucket_public_access_block" "this" {
      + block_public_acls       = false
      + block_public_policy     = false
      + bucket                  = (known after apply)
      + id                      = (known after apply)
      + ignore_public_acls      = false
      + restrict_public_buckets = false
    }

I'm trying to import the bucket into my state file. However, I can't seem to manage to import that data. Presumably, it doesn't like the index. Though, docs I'm looking at shows this should work.

terraform import  module.storage.module.s3_bucket[14].aws_s3_bucket.this  assets-bucket --state=./terraform.tfstate
zsh: no matches found: module.storage.module.s3_bucket[14].aws_s3_bucket.this

# this attempt also fails
terraform import  module.storage.module.s3_bucket[14].aws_s3_bucket.this[0] assets-bucket --state=./terraform.tfstate
zsh: no matches found: module.storage.module.s3_bucket[14].aws_s3_bucket.this[0]
1 Answers

Modern shells treat square brackets as a part of the shell command rather than an input to another script/application. That's why you're getting an error from zsh rather than Terraform.

zsh: no matches found: module.storage.module.s3_bucket[14].aws_s3_bucket.this

If you put single quotes around the value it would treat that as an argument of import instead of a command to ZSH.

terraform import  'module.storage.module.s3_bucket[14].aws_s3_bucket.this'  assets-bucket --state=./terraform.tfstate

If you're curious what it does when you don't put the ' around it, run set -x enable debug mode where it will print what ZSH doing with all the arguments.

Use set +x to turn it off.

Related