Is this acceptable for extending backend config?

I’ve run into the need to either use custom env vars in my backend key or terraform vars that are passed in. I’ve gotten the env vars to work by doing this:

When running this:
CLIENT=“mike” KEY="/my/next/key" VALUE=“ABC123456” terraspace build parameter-store

with config/terraform/backend.rb llke this:

def client
    ENV['CLIENT'] unless ENV['CLIENT'].blank?
end

def key
    ENV['KEY'] unless ENV['KEY'].blank?
end

backend("s3",
    bucket:         "terraform-state-:ACCOUNT-:REGION-:ENV",
    key:            ":PROJECT/:TYPE_DIR/:APP/:ROLE/:MOD_NAME/:ENV/:EXTRA/:REGION/#{client}/#{key}/terraform.tfstate",
    region:         ":REGION",
    encrypt:        true,
    dynamodb_table: "terraform_locks"
)

That results in this:

{
    "terraform": {
        "backend": {
            "s3": {
                "bucket": "terraform-state-733734425040-us-east-1-dev",
                "key": "main/stacks/parameter-store/dev/us-east-1/mike/my/next/key/terraform.tfstate",
                "region": "us-east-1",
                "encrypt": true,
                "dynamodb_table": "terraform_locks"
            }
        }
    }
}

This appears to work but I’m also wondering if I can use terraform vars passed in like so?

terraspace up parameter-store -var key="/my/test/key" -var value="ABC123"

So questions:

  1. Is using the stack specific vars possible in the backend config
  2. If not is setting up the custom env vars like I have an acceptable and safe approach?

I’m also hoping this will allow me to use or not use any of the custom env vars with any given stack. For example:

KEY="/my/next/key" VALUE="ABC123456" terraspace build parameter-store
terraspace up parameter-store -var key="/my/test/key" -var value="ABC123"

or

CLIENT="mike" terraspace build ec2-instance
terraspace up ec2-instance -var client="mike"

Thanks!