Terraform        1

Command        1

Files        1

Commands        2

Target resources: to deploy/destroy specific resources in the existing stack        2

Terraform Variables        2

Varibale passing Flow with modules and multi env        2

Type constructor        3

1. List:        3

2. Tuple        3

3. Set        3

4. Map        3

5. Object        3

Modules        4

Locks        5

What:        5

How:        6

Locks in the cloud        6

Problem Scenarios and the Force Unlock Command        6

Backend configuration        6

Working with states        6

Managing workspace        6

Workspace commands        6

Troubleshoot        6

Callouts        6

1. Install Terraform steps        6

2.  Create a clean project and file        7

4. Commands        7

5. Reference resources        7

6. Folders and files        7

7. Outputs        8

8. Variable        8

Edge Scenarios Questions        8

Drawbacks        10

Terraform with Kubernetes        10

Terraform

Command

Files

  1. .terraform:
  1. install all the plugins to run code
  2. is a local cache where Terraform retains some files required for subsequent operations against this configuration
  1. Terraform.tfstate: 
  1. represents all of the states for terraform. Keep track of everything we create(that way if we modify something; ex: modify tag, it needs to be able to check what is the current state and configurations)
  2. Resource we create in a state file
  1. Terraform.lock.hcl
  1. lock file for various items that Terraform caches in the . terraform subdirectory of your working directory
  2. Terraform automatically creates or updates the dependency lock file each time you run the terraform init command.
  3. You should include this file in your version control repository so that you can discuss potential changes to your external dependencies via code review

Commands

Target resources: to deploy/destroy specific resources in the existing stack  

Terraform Variables

Load variables to terraform by below files

  1. terraform.tfvars - Auto load variables from this file

Load variable in below order

  1. Environment variable
  2. terraform.tfvars file; if present
  3. terraform.tfvars.json file;  if present
  4. Any *auto.tfvars or *auto.tfvars.json
  5. -var and -var-file
Varibale passing Flow with modules and multi env

Flow:

Dev.tfvars → root module → child module

 

  1. Workspace Selection
  2. Load Variable Definitions: Next, Terraform loads the variable definitions from the variables.tf (or similar) files in the root module's directory and in the child module directories if they exist.
  3. Load Variable Values: Terraform then loads the values for these variables from an environment-specific .tfvars file you specify when running terraform apply or terraform plan. You specify it with the -var-file flag.
  4. Pass Variables to Root Module: Terraform uses these variable values within your root module where the variables are referenced. For example, when you call a child module
  5. Pass Variables to Child Module: Finally, within the child module, referenced variables are substituted with these values in the corresponding resources

Type constructor
  1. List:
  1. Tuple
  1. Set
  1. Map

{
 
"vm1" = "Standard_D2s_v3"
 
"vm2" = "Standard_E2s_v3"
 
"vm3" = "Standard_F2s_v2"
}

  1. Object

object({
 hostname = string
 vm_size = string
 region = string
})

Constructor

Description

Works with Count

Works with For_each

Example Use Case

List

An ordered collection of items all of the same type.

Yes

Yes, but not recommended due to unpredictable ordering.

Define a number of similar resources based on length of list.

Type

An ordered collection of items, each item could potentially be a different type.

Yes

Yes, but not recommended due to unpredictable ordering.

To instantiate a set number of resources with definite ordering.

Set

An unordered collection of unique items, all of the same type. Order is not guaranteed.

No

Yes

Define a number of unique resources without worrying about order.

Map

A collection of key-value pairs.

No

Yes

Define resources where each has some unique configuration tied to the key.

object

A single complex structured collection with named keys.

Not directly, unless contained in a list or set

Not directly, unless contained in a list or set

Not directly, unless contained in a list or set

Not directly, unless contained in a list or set

Define a complex object like VM configuration parameters.

Modules

Locks

What:
How:
Locks in the cloud
Problem Scenarios and the Force Unlock Command

Backend configuration

Working with states

State Commands

Managing workspace

Workspace commands

Troubleshoot

  1. Terraform version
  2. Export TF_LOG_CORE=TRACE
  3. Export  TF_LOG_PROVIDER=TRACE
  4. EXPORT TF_LOG_PATH=logs.txt
  5. Terraform refresh

Callouts

1. Install Terraform steps
  1.  install homebrew - package manager tool for mac os. used to install all sorts of softwares

  1. install terraform
2.  Create a clean project and file
4. Commands  
5. Reference resources
6. Folders and files
7. Outputs
8. Variable

Edge Scenarios Questions

  1. We have resources in main.tf file, someone updated/added resources in the resource group/CloudFormation; we want these resources in the cloud but not in the main.tf
  1. Terraform refresh
  1. This will update and bring manually created resources into the terraform.tfstate file. So you don’t need to include it in the main.tf file.  
  2. It updates the state file to match the real infrastructure(ex: changed AWS instance type from micro to small from the console. So refresh will update the state file with this changes)
  1. We have existing Terraform infrastructure created in Azure/AWS; now, one particular resource needs to be re-created, when we do the next apply.
  1. Null resources: it is like a tool used to do specific tasks that do not create or modify any real part of your project.

When:

        Examples 

  1. Running a local script:
  1. You can use null_resource to run a script on your local machine whenever certain parts of your set-up change. 
  2. For example, you might want to add a line to a log every time a new server is created.
  1. Executing a command on a remote server:
  1. You could use null_resource to trigger a command on an existing server when something in your project changes.
  2. For example, suppose you have a server where you wish to upload a script or software only if the software script locally has changed.
  1. Debugging:
  1. null_resource can be used to output debugging information.
  2. For example, you could set it to print the values of certain variables whenever they change.
  1. State file
  1. What: holds the metadata of your cloud infra. When you apply your terraform, it will create a state file holding your infra details.
  2. Store & Secure:
  1. terraform Backend - backends define where terraform’s state snapshots are stored. Store it on the cloud. ex: S3, blob
  1. Lost state file
  1. It will duplicate your infra if you deploy. So you can either use Import to get into your state file
  1. How to resolve a large team working on the same code base, writing/pushing terraform code at the same time
  1. Have a remote state(S3). So people don’t write it locally
  2. Use lock to your state file so people won’t override at the same time
  3. Use version control and branching strategy to solve multiple people working on the same code base
  1. Terraform module
  1. If you make an update/new version to your terraform module - some of the code base that using your terraform module is no longer compatible with this new version, how would you solve this problem
  1. So you can have multiple versions of the modules
  2. When Callar calls the module it can specifically refer to the older version instead of the latest
  1. Types of META-Argument and Benefit
  1. Dynamic block
  1. Creating multiple security group rules: Suppose you have a list of different ports you want to open in a security group. You could write a separate ingress block for each port in your aws_security_group, or you could use a 'dynamic' block:
  1. Creating multiple AWS instances: Suppose you want to create multiple AWS instances with different instance types. You can use a dynamic block for this purpose.

  1. Who  creates terraform.tfstate.backup

Drawbacks

  1. No error handling
  2. HCL language specific unlike Pulumi
  3. Does not support script generation
  4. The backends are not accessible through the variable file

Terraform with Kubernetes

  1. Create a Kubernetes cluster using kind
  1. Configure kubectl to interact with Kubernetes Cluster
  1. Kubctl cluster-info --context EnterName
  1. Confirm if the cluster is created
  1. Kind get clusters
  1. Use kubectl to get information from our cluster to use in our variable file
  1. Kubectl config view --minify --flatten --context EnterName
  2. Get the details and add it to the variable file
  1. Host
  2. Client cert
  3. Client key
  4. Cluster ca cert
  1. Now we have what we need to allow terraform to communicate with our Kubernetes cluster
  1.    Terraform command
  1. Terraform init
  2. Terraform plan
  3. Terarform apply
  1. Kubctl get deployment // to see the resource is created

Callouts 

  1. Create cluster
  2. Get the details about that cluster so that terraform can communicate with it and provision resources
  3. Provision resources(nginx) using terraform