Dynamic S3 Bucket Path in Terragrunt Using Command Line Variable

104 Views Asked by At

I'm managing infrastructure with Terragrunt, and I'd like to dynamically change the S3 bucket path using a variable passed from the command line when running Terragrunt.

I'm using Terragrunt to manage my infrastructure and want to make the backend configuration more flexible. Currently, my backend configuration in terragrunt.hcl looks like this:

remote_state {
  backend = "s3"
  config = {
    bucket  = "bucket-name"
    key     = "${local.cluster_name}/${path_relative_to_include()}/terraform.tfstate"
    region  = "us-east-1"
    encrypt = true
  }
  generate = {
    path      = "backend.tf"
    if_exists = "overwrite_terragrunt"
  }
}

I would like to be able to pass a variable from the command line that can dynamically change the bucket and key in the config block. For example, when I run Terragrunt, I want to specify the bucket and path using something like:

terragrunt run-all apply -var "bucket=my-custom-bucket" -var "key=my-custom-path/${path_relative_to_include()}/terraform.tfstate"

I've looked into Terragrunt's documentation and backend configuration, but it seems that it doesn't support variable expressions in the config block.

When I run Terragrunt, I expect it to use the values I've specified for bucket and key from the command line, overriding the values in the terragrunt.hcl file.

Actual Outcome: Currently, the backend configuration uses the fixed values specified in terragrunt.hcl.

Additional Information:

Terragrunt version: v0.52.3

Terraform version: v1.6.1

Operating System: ubuntu 22

How can I dynamically change the bucket and key in Terragrunt's backend configuration using variables passed from the command line OR any other approacg to achieve the result as demonstrated above?

0

There are 0 best solutions below