runatlantis / atlantis

Terraform Pull Request Automation
https://www.runatlantis.io
Other
7.68k stars 1.05k forks source link

Apply error: "Saved plan does not match the given state" #2278

Closed alfredo-gil closed 2 years ago

alfredo-gil commented 2 years ago

Overview of the Issue

I'm trying to update atlantis to the latest version v0.19.3 but I'm getting the same error starting with the version v0.18.2. When I try to do atlantis apply the apply failed and I get this error:

 Error: Saved plan does not match the given state

 The given plan file can not be applied because it was created from a
 different state lineage.

So when the same workflow it works on version v0.18.1 but starts failing on v0.18.2 and the problem persist on the latest version v0.19.3

Reproduction Steps

Currently, I'm using a simple terraform code to create an s3 bucket.

terraform version: 1.1.9 providers:

This is the terraform provider block:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "~> 3.74"
    }
  }

  required_version = "1.1.9"
}

Environment details

We pass specific values through environment variables Atlantis server-side config file:

# config file
- name: ATLANTIS_HIDE_PREV_PLAN_COMMENTS
   value: 'true'
- name: ATLANTIS_ENABLE_DIFF_MARKDOWN_FORMAT
   value: 'true'
- name: ATLANTIS_LOG_LEVEL
   value: debug
- name: ATLANTIS_DATA_DIR
   value: /atlantis-data
- name: ATLANTIS_PORT
   value: '4141'

Repo atlantis.yaml file:

# config file
---
version: 3
automerge: true
projects:
- name: devbroker
  dir: ./
  workspace: devbroker
  autoplan:
    when_modified: ["**/*.tf", "*.tfvars", "configuration/**/*.yaml"]
    enabled: true

Our custom workflow is this:

        plan:
          steps:
          - env:
              name: TF_WORKSPACE
              command: echo "${WORKSPACE}"
          - init:
              extra_args: ["-backend-config", "./backends/${WORKSPACE}.conf"]
          - run: terraform${ATLANTIS_TERRAFORM_VERSION} workspace new "${WORKSPACE}" || true
          - plan
        apply:
          steps:
          - env:
              name: GITHUB_BASE_URL # for GitHub Enterprise
              command: echo "https://${ATLANTIS_GH_HOSTNAME}/api/v3/"
          - env:
              name: GITHUB_TOKEN
              command: echo "${ATLANTIS_GH_TOKEN}"
          - env:
              name: CONFIG_PATH
              value: /etc/atlantis/atlantis-org-applyer.yaml
          - apply

Additional Context

We saw that between version v0.18.1 and version v0.18.2 the post workflows had been introduced.

jccarte commented 2 years ago

I ran into this same issue. It seemed to work once I added TF_WORKSPACE to my apply step. I think this change caused the issue: https://github.com/runatlantis/atlantis/pull/2006

- env:
     name: TF_WORKSPACE
     command: echo "${WORKSPACE}"
alfredo-gil commented 2 years ago

Many many thanks @jccarte !!! It works like a charm!!

alfredo-gil commented 2 years ago

Many many thanks @jccarte !!! It works like a charm!!