Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to declare variables for s3 backend in terraform?

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?

like image 784
Keerthi hegde Avatar asked Jul 23 '20 07:07

Keerthi hegde


People also ask

How do I use S3 and DynamoDB in TerraForm?

Provide the S3 bucket name and DynamoDB table name to Terraform within the S3 backend configuration using the bucket and dynamodb_table arguments respectively, and configure a suitable workspace_key_prefix to contain the states of the various workspaces that will subsequently be created for this configuration.

Should terraform state objects in S3 have more precise access constraints?

In many cases it is desirable to apply more precise access constraints to the Terraform state objects in S3, so that for example only trusted administrators are allowed to modify the production state, or to control reading of a state that contains sensitive information.

How to dynamically configure the backend of TerraForm?

If you want to dynamically configure the backend you need to write a wrapper script or provide a backend-config via the arguments terraform.io/docs/language/settings/backends/… terragrunt is a nice project that's essentially a wrapper to terraform configurations.

Why do I need to run terraform init?

What this section of code does is it tells Terraform that we want to use an S3 backend instead of our local system to manage our state file. The rest of the code block simply references some of the different resources that we created earlier. Once again, we are required to run terraform init because we’re changing the management of the state file.


2 Answers

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

like image 112
Montassar Bouajina Avatar answered Nov 15 '22 06:11

Montassar Bouajina


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

like image 42
user2599522 Avatar answered Nov 15 '22 07:11

user2599522