4

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 ?

DorAmar
  • 43
  • 1
  • 1
  • 3
  • 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 '22 at 01:03

3 Answers3

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.

hazmei
  • 106
  • 2
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.