Terraform destroy fails in CircleCI

Hi,

I am currently using CircleCI as my CI tool to build AWS infrastructure using Terraform

My flow is,

  1. Create an AWS instance using Terraform
  2. Install Docker and run Nginx image on it
  3. Destroy the infrastructure

My CircleCI config is as follows,

version: 2
jobs:
  terraform_apply:
    working_directory: ~/tmp
    docker:
            - image: hashicorp/terraform:light
            - image: ubuntu:16.04

    steps:
      - checkout
      - run:
          name: terraform apply
          command: |
            terraform init
            terraform apply -auto-approve
      - store_artifacts:
          path: terraform.tfstate

  terraform_destroy:
    working_directory: ~/tmp
    docker:
            - image: hashicorp/terraform:light
            - image: ubuntu:16.04
    steps:
      - checkout
      - run:
          name: terraform destroy
          command: |
            terraform init
            terraform destroy -auto-approve

workflows:
  version: 2
  terraform:
    jobs:
      - terraform_apply
      - click_here_to_delete:
          type: approval
          requires:
            - terraform_apply
      - terraform_destroy:
          requires:
            - click_here_to_delete

Here I am using 2 jobs, One for the creation and one for Deletion in CircleCI workflow.

My first job is running successfully but when I started second it start from scratch so I could not get previous terraform apply state hence terraform could not destroy my already created infrastructure.

I am looking for some solution where I can somehow save state file and copy it to next job where terraform can destroy my previous architecture

Hi,

You could consider saving the state file in a workspace, as it will then be retrievable from downstream jobs in your workflow.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.