Skip to content

Latest commit

 

History

History
30 lines (25 loc) · 764 Bytes

3.2.1. Terraform state backend configuration.md

File metadata and controls

30 lines (25 loc) · 764 Bytes

Terraform state backend configuration

Let's leverage Stacks' global Terraform code feature to simplify Terraform state management.

1. Define state backend template

# 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"
  }
}

2. Define backend_type and backend_args

# 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"
}