gdix0n
gdix0n

Reputation: 264

Is it possible to remove a terraform resource from an index without rebuilding

I have existing terraform infrastructure that has been created by someone before me and they had the instance created using a count, we want to remove the count but terraform is wanting to rebuild the instance.

Is there a way I can remove the count without having to rebuild the instance?

I was wondering if something such as terraform state mv would be able to achieve this or if something else was possible.

Thanks in advance!

UPDATE: Example shown below

  # module.compute.aws_kms_alias.kms-alias will be created
  + resource "aws_kms_alias" "kms-alias" {
      + arn            = (known after apply)
      + id             = (known after apply)
      + name           = "alias/kms-eks"
      + name_prefix    = (known after apply)
      + target_key_arn = (known after apply)
      + target_key_id  = (known after apply)
    }

  # module.compute.aws_kms_alias.kms-alias[1] will be destroyed
  - resource "aws_kms_alias" "kms-alias" {
      - arn            = "arn:aws:kms:::alias/kms-eks" -> null
      - id             = "alias/kms-eks" -> null
      - name           = "alias/kms-eks" -> null
      - target_key_arn = "" -> null
      - target_key_id  = "" -> null
    }

Upvotes: 5

Views: 1900

Answers (2)

Greg Dubicki
Greg Dubicki

Reputation: 6950

Note that if you would have a resource created with an index being a string, you would need to use a syntax like this:

terraform state mv 'module.compute.aws_kms_alias.kms-alias["element_1"]' module.compute.aws_kms_alias.kms-alias

Upvotes: 0

Matthew Schuchard
Matthew Schuchard

Reputation: 28854

You can modify the state to rename the resource in the state to match what is in your config. Since the count meta-argument contains characters that are special, the resource in the command needs to be cast as a literal string in the shell interpreter:

terraform state mv 'module.compute.aws_kms_alias.kms-alias[1]' module.compute.aws_kms_alias.kms-alias

Upvotes: 5

Related Questions