Terraform CLI Tutorials Flashcards

1
Q

Terraform Variables vs Conventional Programming Language Variables

A

Terraform’s input variables don’t change values during a Terraform run such as plan, apply, or destroy.

Instead, they allow users to more safely customize their infrastructure by assigning different values to the variables before execution begins.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Terraform Variables can be defined where…

A

Anywhere in a configuration file (not recommended)

In separate variables.tf file (recommended)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A Variable Blocks three optional args

A

Description: A short description to document the purpose of the variable.

Type: The type of data contained in the variable.

Default: The default value.

e.g.,

variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "us-west-2"
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Referencing a variable in Terraform configuration file

A

var. variable_name
e. g.,

provider “aws” {
region = var.aws_region
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Terraform simple variable types

A

string, int, bool

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Terraform collection variable types

A

List: A sequence of values of the same type.

Map: A lookup table, matching keys to values, all of the same type.

Set: An unordered collection of unique values, all of the same type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Defining a List Type variable

A

e.g.,

variable "public_subnet_cidr_blocks" {
  description = "Available cidr blocks for public subnets."
  type        = list(string)
  default     = [
    "10.0.1.0/24",
    "10.0.2.0/24",
    "10.0.3.0/24",
    "10.0.4.0/24",
    "10.0.5.0/24",
    "10.0.6.0/24",
    "10.0.7.0/24",
    "10.0.8.0/24",
  ]
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Accessing multiple items in a list

A

slice() function

slice(list_object, start_index, end_index)

[‘a’, ‘b’] = slice([‘a’, ‘b’, ‘c’, ‘d’], 0, 2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Defining a Map Type variable

A

map(Type)

e.g. map(string) -> a map of strings. Keys are always strings.

Note: A map is like a python dict

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

You will be prompted to assign a value to variable if…

A

there is no default value defined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Variable string interpolation

A

“The value of the variable is ${var.var_name}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Variable string interpolation

A

“The value of the variable is ${var.var_name}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Variable validation

A

Use a validation block within the variable block to perform validation on the values provided for a variable.

Validation block contains a condition and an error message (string)

variable "example"{
 ...
 ...
 validation{
  condition           = length(var.example) < 10
  error_message = "Example is too long"
}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

terraform init

A

Command to initialize your terraform configuration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Terraform lock file

A

.terraform.lock.hcl

A file that records the versions and hashes of the providers used in this run. It ensures consistent Terraform runs in different environments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

.terraform directory

A

an artifact of terraform init

Stores the providers and modules defined in the project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

terraform plan

A

provides a preview of the actions Terraform would take to modify your infrastructure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Plan ( .configuration object )

A

is a snapshot of your configuration at the time of the terraform plan.

This configuration snapshot captures the versions of the providers recorded in your .terraform.lock.hcl

19
Q

Plan ( .resource_changes object )

A

action field= action taken for this resource
before field=resource state prior to this run
after field=state to define for the resource
after_unknown field=the list of values that will be computed or determined through the operation
before_sensitive and after_sensitive fields = list of any values marked sensitive

20
Q

Plan ( .planned_values object)

A

another view of the differences between the “before” and “after” values of your resources, showing you the planned outcome for a run that would use this plan file.

21
Q

Steps taken by Terraform when running terraform apply

  1. Lock the Projects State.
  2. ______
  3. ______
  4. ______
  5. ______
  6. Print out report of the changes made
A
1 Lock Projects state
2 Create a plan (same as terraform plan) and prompt your approval
3 Execute the steps defined in the plan
4 Update state file
5 Unlock the state file
6 Print out report of the changes made
22
Q

What does Terraform do when there are errors during terraform apply (4 events)

A

1 Errors are logged and reported to the console
2 Updates the state file with any changes to resources
3 Unlocks the state file
4 Exits

23
Q

State of infrastructure after a partially completed apply step (i.e. errors during terraform apply)

A

Infrastructure may be in an invalid state.

You must resolve the error then apply the config again to update your infrastructure to the desired state

24
Q

terraform apply -replace

A

when a resource has become unhealthy or stops working in ways that are outside of Terraform’s control.

You want to instruct Terraform to reprovision the resource using the same configuration.

The -replace argument requires a resource address. List the resources in your configuration with terraform state list.

25
Q

Output blocks

A

Output declarations can appear anywhere in your Terraform configuration files.

However, recommended putting them into a separate file called outputs.tf

output “vpc_id” {
description = “ID of project VPC”
value = module.vpc.vpc_id
}

26
Q

terraform output

A

terraform stores output values in its state file.

These values can be queried using the terraform output command

27
Q

Terraform version constraints

required_version = 0.15.0 ?
required_version >= 0.15 ?
required_version ~> 0.15.0 ?
required_version >= 0.15, < 2.0.0 ?

A

required_version = 0.15.0 means only version 0.15.0
required_version >= 0.15 version 0.15 or greater
required_version ~> 0.15.0 any version 0.15.x
required_version >= 0.15, < 2.0.0 ? version 0.15 or later, but less than version 2.0.0

28
Q

terraform [plan/apply/destroy] -target

A

You can use Terraform’s -target option to target specific resources, modules, or collections of resources when you plan, apply, or destroy your infrastructure.

29
Q

terraform show

A

command to review resources in state file.

Information is presented in JSON.

30
Q

terraform refresh

A

updates the state file when physical resources change outside of the terraform workflow

deprecated (use -refresh-only)

31
Q

terraform [plan/apply] -refresh-only

A

updates your state file without making modifications to your infrastructure

used for when physical resources change outside of the terraform workflow

32
Q

terraform login

A

In order to authenticate with Terraform Cloud, run the terraform login subcommand.

Terraform will prompt you to confirm that you want to authenticate by typing yes in your terminal.

33
Q

terraform validate

A

Validate runs checks that verify whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state. It is thus primarily useful for general verification of reusable modules, including correctness of attribute names and value types.

34
Q

Importing infrastructure form outside of Terraform (5 Steps)

A

Identify the existing infrastructure to be imported.
Import infrastructure into your Terraform state.
Write Terraform configuration that matches that infrastructure.
Review the Terraform plan to ensure the configuration matches the expected state and infrastructure.
Apply the configuration to update your Terraform state.

35
Q

Importing infrastructure into terraform overview

A

Currently state-only. Import resources into the state does not generate configuration.

It is necessary to manually write the configuration.

36
Q

terraform import

A

is used to import existing resources into Terraform.

Usage: terraform import [options] ADDRESS ID

ADDRESS must be a valid resource address.

ID is dependent on the resource type being imported.

37
Q

Provisioner

A

allow you to upload files, run shell scripts, install and trigger other software

resource “aws_instance” “example” {
ami = “ami-b374d5a5”
instance_type = “t2.micro”

provisioner “local-exec” {
command = “echo hello > hello.txt”
}
}

38
Q

local-exec provisioner

A

The local-exec provisioner executing a command locally on your machine running Terraform.

39
Q

remote-exec provisioner

A

Another useful provisioner is remote-exec which invokes a script on a remote resource after it is created.

40
Q

When do provisioners run?

A

Provisioners are only run when a resource is created or destroyed. Provisioners that are run while destroying are Destroy provisioners.

41
Q

tainted resource

A

A resource that is tainted has been physically created, but can’t be considered safe to use since provisioning failed.

42
Q

scope of terraform fmt command

A

scans the current directory for configuration files.

-recursive flag to scan subdirectories as well

43
Q

terraform taint

A

manually marks a Terraform-managed resource as tainted, forcing it to be destroyed and recreated on the next apply.

Note: This command does NOT modify the infrastructure, it modifies the state file. the next plan will show that the tainted resource must be destroyed and recreated, the next apply will implement the change.

44
Q

What is a workspace

A

Persistent data stored in backend belongs to a workspace.