Backend S3

Here’s an example with the s3 backend.

config/terraform/backend.tf:

terraform {
  backend "s3" {
    bucket = "<%= expansion('terraform-state-:ACCOUNT-:REGION-:ENV') %>"
    key = "<%= expansion(':REGION/:ENV/:BUILD_DIR/terraform.tfstate') %>"
    region = "<%= expansion(':REGION') %>"
    encrypt = true
    dynamodb_table = "terraform_locks"
  }
}

Notice the variable notation. Terraspace expands it out, substituting the values. The starter backend.tf accounts for REGION, ENV, etc. Here’s an expanded example:

terraform
  terraform {
    backend "s3" {
    bucket = "terraform-state-111111111111-us-west-2-dev"
    key = "us-west-2/dev/stacks/demo/terraform.tfstate"
    region = "us-west-2"
    encrypt = true
    dynamodb_table = "terraform_locks"
  }
}

You can fully control the state file path by adjusting this. The string substitution also makes it clear what the state path looks like.


Why Is Env in Bucket Name?

By default, the bucket name has the ENV at the end. This is done so we can easily see which environment the bucket stores Terraform statefiles for. This quickly helps with debugging. If you prefer not to have the ENV at the end of the bucket name, remove it after generating the project with terraspace new project.

More tools: