AstroNomad
AstroNomad

Reputation: 117

How to resolve Terraform "already exists." error for multiple resources while terraform apply?

Terraform fails on terraform apply, because of failure on "already exists" error. I think this happened, because I manually deleted the tfstate and ddb md5 entries. Which created the whacky state of Terraform.

Now when I do init, plan and apply, I am getting quite a few errors as follows example:

Error: error creating SSM parameter: ParameterAlreadyExists: The parameter already exists. To overwrite this value, set the overwrite option in the request to true.
......

Error: error creating SSM parameter: ParameterAlreadyExists: The parameter already exists. To overwrite this value, set the overwrite option in the request to true.


Error: Error creating DB Parameter Group: DBParameterGroupAlreadyExists: Parameter group abc already exists

I have taken a look into the import option, but it's too messy.

Is there an easy or cleaner approach on tacking this? Thank you, any advice will be helpful.

Upvotes: 6

Views: 18450

Answers (1)

Leo
Leo

Reputation: 538

The short answer is, it depends.

Each resource has it own functionalities, some allow you to overwrite existing resources and some don't.

For example, for ssm parameters, you can add a "overwrite" flag to the resource.

resource "aws_ssm_parameter" "foo" {
  name  = "foo"
  type  = "String"
  value = "bar"

  overwrite = true
}

Official reference: ssm_parameter

Now, a good way to avoid the issue of loosing tfstate is to store it in S3 in a bucket that has version control.

Upvotes: 5

Related Questions