title: Terraform apply command fails when runs twice in Gitlab pipeline and resources were already created
body: <p>I'm quite new with Terraform and I'm trying to replicate in my terraform configuration the stack I have already built for production (basically: Api gateway - Lambda - DynamoDB).</p>
  
  <p>If I run <code>terraform init</code>, <code>terraform plan</code> and then <code>terraform apply</code> from my local host, everything is created as I want.</p>
  
  <p>The problem arises when it comes to my Gitlab CI/CD pipeline, as Terraform complains about the existing resources (the first time runs properly, the second time complains and throws an error).</p>
  
  <p>My Terraform steps in my <code>.gitlab-ci.yml</code> file:</p>
  
  <pre><code>plan:
    stage: plan
    image:
      name: hashicorp/terraform:light
      entrypoint:
      - '/usr/bin/env'
      - 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
    script:
      - cd terraform
      - rm -rf .terraform
      - terraform --version
      - terraform init
      - terraform plan
  
  deploy:
    stage: deploy
    image:
      name: hashicorp/terraform:light
      entrypoint:
      - '/usr/bin/env'
      - 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
    script:
     - cd terraform
     - terraform init
     - terraform apply -auto-approve
    dependencies:
      - plan
    when: manual
  </code></pre>
  
  <p>I see in my pipeline console the following error:
  <a href="https://i.stack.imgur.com/Iw9cB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Iw9cB.png" alt="enter image description here"></a></p>
  
  <p>So after some Googling I saw that maybe the <code>terraform import</code> command could help.</p>
  
  <p>Then added this <code>import</code> command to my .gitlab-ci.yml: </p>
  
  <pre><code> script:
   - cd terraform
   - terraform init
   - terraform import aws_dynamodb_table.demo-dynamodb-table demo-dynamodb-table
   - terraform apply -auto-approve
  </code></pre>
  
  <p>And the error in the Gitlab console was:
  <a href="https://i.stack.imgur.com/vUTf5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vUTf5.png" alt="enter image description here"></a></p>
  
  <p>In the meantime I tried also this last change locally, and the error was:
  <a href="https://i.stack.imgur.com/DSx6p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DSx6p.png" alt="enter image description here"></a></p>
  
  <p>So to summarize: I would need to know how to use Terraform in the right way to be able to run the <code>apply</code> command in my Gitlab CI/CD pipeline without conflicts with the resource that was created in the previous run of this same pipeline.</p>