skip to Main Content

I’m setting up a pipeline that provisions resources in AWS. Each time I run the pipeline, I get get a module already exist error. I know the resources I want I already provisioned but my understanding of Terraform is that if it already exists it just skips it and provisions the rest that don’t already exist. How do I make it skip existing modules and not result into a pipeline build error.

2

Answers


  1. my understanding of Terraform is that if it already exists it just skips it and provisions

    Sadly your understanding is incorrect. TF does not check if something exists before it provisions resources. By TF design principles it is assumed that resources do not exist if they are to be managed by TF.

    How do I make it skip existing modules and not result into a pipeline build error.

    You have to do it manually. Pass some variables to your TF script for conditional creation of resources. TF has no capability to check for pre-existance of resources, unless you do it yourself.

    Login or Signup to reply.
  2. Terraform does not skip the resource if it already exists, it throws an error and quits execution.

    To deal with this kind of problem, the best alternative is to import the existing resource to your state file.

    In the end of each resource page from the official documentation you will find a "import" section, usually it goes like:

    terraform import terraform_state_id component_id
    
    Example:
    terraform import aws_instance.web i-12345678
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search