Reputation: 351
s3.tf
terraform {
backend "s3" {
bucket = "some-bucket"
key = "path/to/key"
region = "some-aws-region"
}}
How to pass the bucket and region values to this from a variables.tf file?
Upvotes: 27
Views: 27632
Reputation: 1652
hello here's a solution :
terraform {
backend "s3" {
}
}
pass the backend like that and then :
on the terraform init command :
terraform init \
-backend-config="bucket=${TFSTATE_BUCKET}" \
-backend-config="key=${TFSTATE_KEY}" \
-backend-config="region=${TFSTATE_REGION}"
you should use env to set TFSTATE_BUCKET TFSTATE_KEY and TFSTATE_REGION
here's a link of the docs : the Terraform docs on "Partial Configuration" of Backends
Upvotes: 53
Reputation: 5905
Montassar's answer is quite good, but I prefer file version:
dev.tfbackend
file
bucket="some-bucket"
region="some-aws-region"
main.tf
,
terraform {
backend "s3" {
key = "path/to/key"
}}
terraform init -backend-config=dev.tfbackend
Upvotes: 17
Reputation: 1
I see that you're trying to use have different environments and specify them on your S3 config. But I'd recommend you to use workspaces
.
To create a new workspace:
terraform state new <name>
Upvotes: 0
Reputation: 3225
I believe this is not currently possible as if you add a variable interpolation in that, you will get an error
terraform.backend: configuration cannot contain interpolations
Upvotes: 2