Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native Variable Support in S3 Backend Configuration #36563

Closed
aymene01 opened this issue Feb 21, 2025 · 0 comments
Closed

Native Variable Support in S3 Backend Configuration #36563

aymene01 opened this issue Feb 21, 2025 · 0 comments
Labels
enhancement new new issue not yet triaged

Comments

@aymene01
Copy link

aymene01 commented Feb 21, 2025

Terraform Version

v1.10.5

Use Cases

When managing multiple environments and components in a large infrastructure, we often need to reuse the same backend configuration pattern with different values (environment, product, component names). Currently, this requires either:

  • Duplicating backend configurations
  • Using separate backend.hcl files
  • Creating complex workarounds with templates

Current Limitations

The backend block does not support variables or locals:

terraform {
 backend "s3" {
   bucket         = local.backend.bucket # Not allowed
   key            = var.state_key        # Not allowed
   region         = var.aws_region       # Not allowed
   # ...
 }
}

Attempted Solutions

# modules/remote-state/outputs.tf
output "config" {
  description = "Remote state configuration"
  value = {
    bucket         = "acme-${var.environment}-tfstates"
    key            = "${var.product}/${var.component}.tfstate"
    region         = var.region
    encrypt        = true
    dynamodb_table = "acme-${var.environment}-terraform-state-lock"
    assume_role = {
      role_arn = var.environment == "prod" ? 
        "arn:aws:iam::987654321098:role/TerraformProd" : 
        "arn:aws:iam::123456789012:role/TerraformNonProd"
    }
  }
}

# main.tf
terraform {
  backend "s3" {}
}

module "remote_state" {
  source = "../remote-state"
  
  environment = var.environment  # dev/staging/prod
  product     = "payment-service"
  component   = "transactions"
  region      = "eu-west-1"
}

data "terraform_remote_state" "state" {
  backend = "s3"
  config  = module.remote_state.config
}

Proposal

Allow variable and local usage in backend configurations, similar to other Terraform blocks:

terraform {
 backend "s3" {
   bucket          = "${var.environment}-tfstates"
   key             = "${var.product}/${var.component}.tfstate"
   region          = var.aws_region
   encrypt         = true
   dynamodb_table  = "${var.environment}-terraform-state-lock"
   assume_role {
     role_arn      = var.backend_role_arn
   }
 }
}

No response

@aymene01 aymene01 added enhancement new new issue not yet triaged labels Feb 21, 2025
@aymene01 aymene01 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

1 participant