3

I'm Getting an error creating a secret because it was created manually.

Error: error creating Secrets Manager Secret: ResourceExistsException: The operation failed because the secret <SECRET> already exists.

Is there any way to tell Terraform to skip creating a resource if it already exists ?

1
  • No, there is not. The idea of a tool like Terraform is to manage your complete infrastructure with it, not to cherry-pick things that you sometimes do and sometimes don't want to manage. You can use the terraform import functionality to import existing infrastructure into your state. The documentation for each resource type will show you how to import it.
    – Oldskool
    Nov 15, 2022 at 1:03

3 Answers 3

2

after terraform 0.12 we have ways to control it using lifecycle we can simply skip resource by setting up lifecycle

resource "aws_instance" "web" {
    ami = "foo"
    lifecycle {
        ignore_changes = ["ami"]
    }
}

Here is the link of bug https://github.com/hashicorp/terraform/pull/2525

1

No there is no way to tell Terraform to skip creating a resource if it already exists.

Is there a reason why you want to skip resource creation? Why not import the existing resource into Terraform state file so it doesn't try to create the resource that already exists?

You could provide more background information for this.

1
  • Can you provide a simple example? The docs do not seem to help much... Dec 13, 2023 at 8:02
0

One hack we can do is for every named resource you plan to create, perhaps do a check for the resource using data block and then use count in the resource block in order to create or skip.

With this it will increase the execution time but recurring already exists errors could be mitigated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.