2

All my terraform files start with:

terraform {
  required_version = ">= 0.11, < 0.12"
  backend "s3" {
    bucket  = "my-terraform-state"
    key     = "my-service/my-component/terraform.tfstate"
    region  = "eu-west-1"
    encrypt = "true"
  }
}

provider "aws" {
  region = "eu-west-1"
  version = ">= 1.8, < 1.9"
}

What I would like is something like

module "header" {
  source = "module-location"
  region = "eu-west-1"
  state_key = "my-service/my-component"
}

When I try something like this it fails with the message that the backend configuration cannot contain variables. I also read about the read-only state and noticed that it can contain variables.

0

1 Answer 1

1

You can have a singe file called terraform.tf:

terraform {
  required_version = ">= 0.11, < 0.12"
  backend "s3" {
    bucket  = "my-terraform-state"
    key     = "my-service/my-component/terraform.tfstate"
    region  = "eu-west-1"
    encrypt = "true"
  }
}

and a provider.tf:

provider "aws" {
  region = "eu-west-1"
  version = ">= 1.8, < 1.9"
}

Place these files in the root dir. Terraform will use them and you no longer need to have those headings at the top of each file.

You can not use variables / interpolation in terraform.tf, as this file is used very early by terraform.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. But wouldn't all components share the same state file, thus losing the advantage of storing the state per component?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.