Skip to content

Instantly share code, notes, and snippets.

@leetrout
Created April 17, 2020 04:55
Show Gist options
  • Save leetrout/51e605000fa1f59d1f6902023d685c8c to your computer and use it in GitHub Desktop.
Save leetrout/51e605000fa1f59d1f6902023d685c8c to your computer and use it in GitHub Desktop.
Terraform Force Push Examples

main.tf

provider "aws" {
  version = "~> 2.0"
  region  = "us-east-2"
}

terraform {
  backend "s3" {
    bucket = "hc-lee-force-push-test"
    key    = "tfstate"
    region = "us-east-2"
  }
}

resource "random_integer" "rando" {
  min     = 1
  max     = 50000
}

Example force push (S3)

tf state pull > state

cat state
{
  "version": 4,
  "terraform_version": "0.12.24",
  "serial": 0,
  "lineage": "1aa2c42c-763f-c016-1cd9-7f6167380282",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "random_integer",
      "name": "rando",
      "provider": "provider[\"registry.terraform.io/hashicorp/random\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "id": "41836",
            "keepers": null,
            "max": 50000,
            "min": 1,
            "result": 41836,
            "seed": null
          },
          "private": "bnVsbA=="
        }
      ]
    }
  ]
}

Edit lineage and save and compare with diff:

tf state pull | diff - state
5c5
<   "lineage": "1aa2c42c-763f-c016-1cd9-7f6167380282",
---
>   "lineage": "1aa2c42c-763f-c016-1cd9-7f6167380283",

Try to push:

tf state push state
Failed to write state: cannot import state with lineage "1aa2c42c-763f-c016-1cd9-7f6167380283" over unrelated state with lineage "1aa2c42c-763f-c016-1cd9-7f6167380282"

Force push and compare again (no output from push):

tf state push -force state
tf state pull | diff - state
4c4
<   "serial": 1,
---
>   "serial": 0,

Try to push again:

tf state push state
Failed to write state: cannot import state with serial 0 over newer state with serial 1
tf state push -force state
tf state pull | diff - state
4c4
<   "serial": 1,
---
>   "serial": 0,

main.tf

terraform {
  backend "pg" {
    conn_str = "postgres://hashicorp@localhost/terraform_backend?sslmode=disable"
  }
}

resource "random_integer" "rando" {
  min     = 1
  max     = 50000
}

Example force push (Postgres)

tf state pull > state

cat state
{
  "version": 4,
  "terraform_version": "0.12.24",
  "serial": 1,
  "lineage": "e695d9ff-7da6-a474-bd5c-3ace97a545b4",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "random_integer",
      "name": "rando",
      "provider": "provider[\"registry.terraform.io/hashicorp/random\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "id": "29092",
            "keepers": null,
            "max": 50000,
            "min": 1,
            "result": 29092,
            "seed": null
          },
          "private": "bnVsbA=="
        }
      ]
    }
  ]
}

Edit lineage and save and compare with diff:

tf state pull | diff - state
5c5
<   "lineage": "e695d9ff-7da6-a474-bd5c-3ace97a545b4",
---
>   "lineage": "e695d9ff-7da6-a474-bd5c-3ace97a545b5",

Try to push:

tf state push state
Failed to write state: cannot import state with lineage "e695d9ff-7da6-a474-bd5c-3ace97a545b5" over unrelated state with lineage "e695d9ff-7da6-a474-bd5c-3ace97a545b4"

Force push and compare again (no output from push):

tf state push -force state
tf state pull | diff - state
4c4
<   "serial": 2,
---
>   "serial": 1,

Try to push again:

tf state push state
Failed to write state: cannot import state with serial 1 over newer state with serial 2

But works with force:

tf state push -force state

tf state pull | diff - state
4c4
<   "serial": 2,
---
>   "serial": 1,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment