infrastructure - Terraform terraform_remote_state Partial Configuration -
my team relies heavily on s3 remote state within terraform. use -backend-config feature of cli specify s3 configuration when initializing projects, our actual terraform code looks like:
terraform { backend "s3" {} } the above works great long s3 attributes specified on cli -backend-config.
we use similar strategy referencing these states elsewhere in our configurations. since parameters backend dynamic , specified on cli, looking same.
data "terraform_remote_state" "dns" { backend = "s3" config { key = "configurations/production/dns/terraform.tfstate" } } in above example, we've omitted required region , bucket parameters, of course causes plan/apply fail (with not valid region:).
is there method can specify region , bucket remote state references cli instead of hard-coding them?
the backend block rather special because gets processed in terraform's workflow, , doesn't have access normal terraform features such variables. that's why has own special mechanism configuring it.
the terraform_remote_state data source, on other hand, regular data source , normal interpolation strategy can used it. pass settings cli, example, use variables:
variable "dns_state_region" { } variable "dns_state_key" { } data "terraform_remote_state" "dns" { backend = "s3" config { region = "${var.dns_state_region}" key = "${var.dns_state_key}" } } you can pass these terraform plan command:
$ terraform plan \ -var="dns_state_region=us-west-1" \ -var="dns_state_key=configurations/production/dns/terraform.tfstate"
Comments
Post a Comment