Terraform Advanced — Terragrunt Remote State Config
Terragrunt auto-generates backend configuration per directory — no more copy-pasting S3 keys.
The Magic: path_relative_to_include()
# Root terragrunt.hcl
remote_state {
backend = "s3"
config = {
bucket = "mycompany-terraform-state"
key = "${path_relative_to_include()}/terraform.tfstate"
region = "us-east-1"
}
}
This single line generates unique keys for every stack:
environments/dev/vpc/ → dev/vpc/terraform.tfstate
environments/dev/eks/ → dev/eks/terraform.tfstate
environments/prod/vpc/ → prod/vpc/terraform.tfstate
environments/prod/rds/ → prod/rds/terraform.tfstate
Auto-Create Bucket and Table
remote_state {
backend = "s3"
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
config = {
bucket = "mycompany-terraform-state"
key = "${path_relative_to_include()}/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
# Terragrunt will create the bucket and table if they don't exist
s3_bucket_tags = {
ManagedBy = "terragrunt"
}
dynamodb_table_tags = {
ManagedBy = "terragrunt"
}
}
}
💡 Production Tip: Terragrunt creates the S3 bucket and DynamoDB table automatically on first run — no bootstrap step needed.
What’s Next?
Manage cross-stack dependencies.