Let's leverage Stacks' global Terraform code feature to simplify Terraform state management.
# stacks/backend.tf
terraform {
backend "{{ var.backend_type }}" {
{% for key, value in var.backend_args.items() -%}
{{ key }} = "{{ value }}"
{% endfor -%}
key = "{{ var.stacks_path }}/terraform.tfstate" # e.g. "stacks/vpc/layers/production/terraform.tfstate"
}
}
# stacks/backend.tfvars
backend_type = "s3"
backend-args = {
region = "eu-south-2"
bucket = "my-terraform-state"
dynamodb_table = "my-terraform-state"
role_arn = "arn:aws:iam::0123456789:role/Terraform"
}