Our root module structure is as follows:
PROJECT_ROOT
│
├── main.tf # everything else.
├── variables.tf # stores the structure of input variables
├── terraform.tfvars # the data of variables we want to load into our terraform project
├── providers.tf # defined required providers and their configuration
├── outputs.tf # stores our outputs
└── README.md # required for root modules
In terraform we can set two kind of variables:
- Enviroment Variables - those you would set in your bash terminal eg. AWS credentials
- Terraform Variables - those that you would normally set in your tfvars file
We can set Terraform Cloud variables to be sensitive so they are not shown visibliy in the UI.
We can use the -var
flag to set an input variable or override a variable in the tfvars file eg. terraform -var user_ud="my-user_id"
- The -var-file flag is used to pass Input Variable values into Terraform plan and apply commands using a file that contains the values.
This is the default file to load in terraform variables in blunk
- Terraform automatically loads any files ending in
.auto.tfvars
or.auto.tfvars.json
Terraform uses a specific order of precedence when determining the value of a variable. If the same variable is assigned multiple values, Terraform will use the value of highest precedence, overriding any other values. Below is the precedence order starting from the highest priority to the lowest.
- Environment variables (
TF_VAR_variable_name
) - The
terraform.tfvars
file - The
terraform.tfvars.json
file - Any
.auto.tfvars
or.auto.tfvars.json
files, processed in lexical order of their filenames. - Any
-var
and-var-file
options on the command line, in the order they are provided. - Variable defaults
If you lose your statefile, you most likley have to tear down all your cloud infrastructure manually.
You can use terraform import but it won't for all cloud resources. You need check the terraform providers documentation for which resources support import.
terraform import aws_s3_bucket.bucket bucket-name
Terraform Import AWS S3 Bucket Import
If someone goes and delete or modifies cloud resource manually through ClickOps.
If we run Terraform plan is with attempt to put our infrstraucture back into the expected state fixing Configuration Drift