Skip to content

Latest commit

 

History

History
81 lines (49 loc) · 3.16 KB

File metadata and controls

81 lines (49 loc) · 3.16 KB

Terraform Beginner Bootcamp 2023 - Week 1

Root Module Structure

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

Standard Module Structure

Terraform and Input Variables

Terraform Cloud Variables

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.

Loading Terraform Input Variables

Terraform Input Variables

var flag

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"

var-file flag

  • The -var-file flag is used to pass Input Variable values into Terraform plan and apply commands using a file that contains the values.

terraform.tvfars

This is the default file to load in terraform variables in blunk

auto.tfvars

  • Terraform automatically loads any files ending in .auto.tfvars or .auto.tfvars.json

order of terraform variables

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.

  1. Environment variables (TF_VAR_variable_name)
  2. The terraform.tfvars file
  3. The terraform.tfvars.json file
  4. Any .auto.tfvars or .auto.tfvars.json files, processed in lexical order of their filenames.
  5. Any -var and -var-file options on the command line, in the order they are provided.
  6. Variable defaults

Dealing With Configuration Drift

What happens if we lose our state file?

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.

Fix Missing Resources with Terraform Import

terraform import aws_s3_bucket.bucket bucket-name

Terraform Import AWS S3 Bucket Import

Fix Manual Configuration

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