Skip to content

Instantly share code, notes, and snippets.

@jniesen
Last active June 19, 2017 12:10
Show Gist options
  • Save jniesen/29650a74df757651ed8e292be4ce621b to your computer and use it in GitHub Desktop.
Save jniesen/29650a74df757651ed8e292be4ce621b to your computer and use it in GitHub Desktop.
Terraform environments and state backends

Given the following Terraform:

terraform {
  version = "0.9.8"

  backend "consul" {
    address = "re.consul.aws-dev.manheim.com:8500"
    path    = "terraform/jniesen/tf-envs"
  }
}

output "env" {
  value = "${terraform.env}"
}

After running terraform init && terraform plan && terraform apply, a statefile is created at terraform/jniesen/tf-envs which looks like this:

{
    "version": 3,
    "terraform_version": "0.9.8",
    "serial": 1,
    "lineage": "c6ca7a3d-95a6-4f6d-90dc-f3cfc7a1c5ce",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {
                "env": {
                    "sensitive": false,
                    "type": "string",
                    "value": "nonprod"
                }
            },
            "resources": {},
            "depends_on": []
        }
    ]
}

Then add a new environment.

After running terraform env new nonprod && terraform plan && terraform apply, a statefile is created at terraform/jniesen/tf-envs-env:nonprod which looks like:

{
    "version": 3,
    "terraform_version": "0.9.8",
    "serial": 1,
    "lineage": "c6ca7a3d-95a6-4f6d-90dc-f3cfc7a1c5ce",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {
                "env": {
                    "sensitive": false,
                    "type": "string",
                    "value": "nonprod"
                }
            },
            "resources": {},
            "depends_on": []
        }
    ]
}

The cool thing to notice is that Terraform appended the -env:nonprod onto the end of the statefile path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment