Terraform Configuration Language Tutorials Flashcards

1
Q

resource block

A

Resource blocks declare a resource type and name.

Together, the type and name form a resource identifier (ID) in the format resource_type.resource_name

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

Resource types always start with the _________ followed by an underscore.

A

provider name

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

Resource Arguments

A

configure a particular resource; because of this, many arguments are resource-specific.

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

Resource Attributes

A

Attributes are values exposed by an existing resource.

References to resource attributes take the format resource_type.resource_name.attribute_name

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

Resource Meta-arguments

A

Meta-arguments change a resource’s behavior, such as using a count meta-argument to create multiple resources.

Meta-arguments are a function of Terraform itself and are not resource or provider-specific.

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

Terraform Core

A

reads the configuration and builds the resource dependency graph.

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

Terraform Plugins

A

(providers and provisioners) bridge Terraform Core and their respective target APIs.

Terraform provider plugins implement resources via basic CRUD (create, read, update, and delete) APIs to communicate with third party services.

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

Sensitive values in state file

A

Terraform stores the state as plain text, including variable values, even if you have flagged them as sensitive.

Since Terraform state can contain sensitive values, you must keep your state file secure to avoid exposing this data.

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

locals

A

named values that you can refer to in your configuration

Unlike input variables, locals are not set directly by users of your configuration!

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

Data sources

A

Data sources allow Terraform to use information defined outside of Terraform, defined by another separate Terraform configuration, or modified by functions.

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

A data block requests that Terraform ….

A

read from a given data source and export the result under the given local name

data "data_source" "local_name" {
 #Most arguments in this section depend on the data source
}

e.g.,

data “azurerm_virtual_machine” “example” {
name = “production”
resource_group_name = “networking”
}

output “virtual_machine_id” {
value = data.azurerm_virtual_machine.example.id
}

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

Managed resources (resource {}) vs. data resources (data {})

A

Both kinds of resources take arguments and export attributes

Managed resources cause Terraform to create, update, and delete infrastructure objects

Data resources cause Terraform only to read objects.

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

terraform_remote_state data source

A

Retrieves state data from a Terraform backend.

Allows you to use the root-level outputs of one or more Terraform configurations as input data for another configuration.

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

Backends

A

Backends define where Terraform’s state snapshots are stored.

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

Resource Dependencies

A

Terraform infers the dependencies between resources in most cases.

Occasionally, an dependency will need be defined explicitly with the depends_on argument

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

depends_on

A

An argument to define explicit dependencies

Accepted by any resource of module block

You can specify multiple resources in the depends on argument

e.g.,
resource "aws_instance" "example"{
    ...
    depends_on = [aws_s3_bucket.example]
}

module “example_sqs_queue” {

depends_on = [aws_s3_bucket.example, aws_instance.example]
}

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

The count argument

A

replicates the given resource or module a specific number of times with an incrementing counter. It works best when resources will be identical, or nearly so.

resource "azurerm_virtual_network" "example" {
 #create 6 instances of a vnet
  count = 6
  name                = "example-vnet0${count.index}"
  ...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

The for_each argument

A

meta-argument accepts a map or a set of strings, and creates an instance for each item in that map or set. Each instance has a distinct infrastructure object associated with it, and each is separately created, updated, or destroyed when the configuration is applied.

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

The each object

A

In blocks where for_each is set, an additional each object is available in expressions, so you can modify the configuration of each instance.

This object has two attributes:

each. key — The map key (or set member) corresponding to this instance.
each. value — The map value corresponding to this instance. (If a set was provided, this is the same as each.key.)

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

Limitations on values used in for_each

A

The keys of the map (or all the values in the case of a set of strings) must be known values

Sensitive values cannot be used as arguments to for_each

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

Referring to instances (within the config file) that were declared using for_each

A

TYPE.NAME refers to the block (e.g., azure_resource_group.rg)
TYPE.NAME.[KEY] refers to individual instances (e.g., azure_resource_group.rg[“rg01”], azure_resource_group.rg[“rg02”])

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

the templatefile Function

A

reads the file at the given path and renders its content as a template using a supplied set of template variables.

templatefile(path, vars)

*.tftpl is the recommended naming pattern to use for your template files

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

the file function

A

reads the contents of a file at the given path and returns them as a string.

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

template sequences

A

Within quoted and heredoc string expressions, the sequences ${ and %{ begin template sequences. Templates let you directly embed expressions into a string literal, to dynamically construct strings from other values.

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

String Interpolation

A

A ${ … } sequence is an interpolation, which evaluates the expression given between the markers, converts the result to a string if necessary, and then inserts it into the final string:

“Hello, ${var.name}!”

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

string directives

A

A %{ … } sequence is a directive, which allows for conditional results and iteration over collections, similar to conditional and for expressions.

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

string directives: if else

A

The %{if }/%{else}/%{endif} directive chooses between two templates based on the value of a bool expression:

e.g., “Hello, %{ if var.name != “” }${var.name}%{ else }unnamed%{ endif }!”

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

string directives: for

A

The %{for in } / %{endfor} directive iterates over the elements of a given collection

e.g., “%{ for ip in aws_instance.example.*.private_ip } server ${ip} %{ endfor }”

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

whitespace stripping in template directives

A

template sequences can include strip markers ( ~ ) immediately after the opening chars or immediately before the end.

30
Q

heredoc style strings

A

A heredoc string consists of:

An opening sequence consisting of:
A heredoc marker (&laquo_space;or <

31
Q

the lookup function

A

lookup retrieves the value of a single element from a map, given its key. If the given key does not exist, the given default value is returned instead.

lookup(map, key, default)

32
Q

the concat function

A

The concat function takes two or more lists and combines them into a single list.

33
Q

conditional expressions syntax

A

condition ? true value : false value

name = (var.name != “” ? var.name : random_id.id.hex)

34
Q

the splat expression

A

The splat expression captures all objects in a list that share an attribute. The special * symbol iterates over all of the elements of a given list and returns information based on the shared attribute you define.

e.g.,

value = aws_instance.ubuntu[*].private_dns

35
Q

a dynamic block

A

A dynamic block acts much like a for expression, but produces nested blocks instead of a complex typed value.

36
Q

dynamic block syntax (for_each)

A

The for_each argument provides the complex value to iterate over.

resource "azurerm_virtual_network" "example" {
    ....
  dynamic "subnet"{
    for_each = var.subnets
    ...
  }
}
37
Q

dynamic block syntax (label)

A

The label of the dynamic block (e.g., subnet) specifies what kind of nested block to generate.

resource "azurerm_virtual_network" "example" {
  ...
  dynamic "subnet"{
    ...
    }
  }
38
Q

dynamic block syntax (iterator)

A

The iterator argument (optional) sets the name of a temporary variable that represents the current element of the complex value.

If omitted, the name of the variable defaults to the label of the dynamic block (“setting” in the example above).

resource "azurerm_virtual_network" "example" {
    ...
  dynamic "subnet"{
    for_each = var.subnets-iter
    #adding iterator to override the default label (i.e., "subnet") as iterator
    iterator = iter
    content{
        name = iter.value["name"]
        address_prefix = iter.value["address_prefix"]
        security_group = azurerm_network_security_group.example.id
    }
  }
}
39
Q

dynamic block syntax (content)

A

The nested content block defines the body of each generated block. You can use the temporary iterator variable inside this block or the default label ofthe dynamic block.

resource “azurerm_virtual_network” “example” {

dynamic “subnet”{
for_each = var.subnets
content{
name = subnet.value[“name”]
address_prefix = subnet.value[“address_prefix”]
security_group = azurerm_network_security_group.example.id
}
}
}

40
Q

lifecycle block

A

is nested block that can appear in a resource block

lifecycle block and its contents are meta-arguments

41
Q

lifecycle {} - create_before_destroy

A

The create_before_destroy (bool) meta-argument changes default behavior so that a new replacement object is created first, and the prior object is destroyed after the replacement is created.

42
Q

lifecycle {} - prevent_destroy

A

prevent_destroy (bool) - This meta-argument, when set to true, will cause Terraform to reject with an error any plan that would destroy the infrastructure object associated with the resource, as long as the argument remains present in the configuration.

43
Q

lifecycle {} - ignore_changes

A

The ignore_changes list (list of attribute names) is intended to be used when a resource is created with references to data that may change in the future, but should not affect said resource after its creation.

44
Q

debugging in terraform

A

set the TF_LOG environment variable to any value.

This will cause detailed logs to appear on stderr.

45
Q

location of terraform crash logs

A

crash.log

46
Q

Directory-separated vs workspace-separated environments?

A

Directory separated environments rely on duplicate Terraform code

Workspace-separated environments use the same Terraform code but have different state files

47
Q

terraform state pull

A

command to pull the remote state

will download the state from its current location and output the raw format to stdout.

48
Q

terraform state push

A

command to upload the remote state

49
Q

When you are using workspaces where does the Terraform save the state file for the local state?

A

terraform.tfstate.d

50
Q

When you are using workspaces where does the Terraform save the state file for the remote state?

A

For remote state, the workspaces are stored directly in the configured backend.

51
Q

How do you remove items from the Terraform state?

A

terraform state rm command is used to remove items from the Terraform state. This command can remove single resources, single instances of a resource, entire modules, and more.

52
Q

When you are doing initialization with terraform init, you want to skip backend initialization. What should you do?

A

terraform init -backend=false

53
Q

When you are doing initialization with terraform init, you want to skip child module installation. What should you do?

A

terraform init -get=false

54
Q

When you are doing initialization with terraform init, you want to skip plugin installation. What should you do?

A

terraform init -get-plugins=false

55
Q

What does the command terraform validate do?

A

terraform validate command validates the configuration files in a directory

runs checks that verify whether a configuration is syntactically valid and internally consistent

56
Q

You are applying the infrastructure with the command apply and you don’t want to do interactive approval. Which flag should you use?

A

terraform apply -auto-approve

57
Q

How do you preview the behavior of the command terraform destroy?

A

terraform plan -destroy

58
Q

How do you save the execution plan?

A

terraform plan -out=tfplan

59
Q

the try function

A

try evaluates all of its argument expressions in turn and returns the result of the first one that does not produce any errors.

local.foo
{
  "bar" = "baz"
}
> try(local.foo.bar, "fallback")
baz
> try(local.foo.boop, "fallback")
fallback
60
Q

A locals block

A

defines local variables

locals{
name_suffix = “${var.project_name}-${var.environment}”
}

Note: Unlike input variables locals are not directly set by the user

61
Q

Terraform command to create new workspace

A

terraform workspace new [workspace-name]

62
Q

how to run provisioners that are not associated with any resource

A

Null_resource

63
Q

When writing Terraform code, HashiCorp recommends that you use how many spaces between each nesting level?

A

2

64
Q

In order to reduce the time it takes to provision resources, Terraform uses parallelism. By default, how many resources will Terraform provision concurrently during a terraform apply?

A

10

65
Q

When using providers that require the retrieval of data, such as the HashiCorp Vault provider, in what phase does Terraform actually retrieve the data required, assuming you are following the standard workflow of write, plan, and apply?

A

terraform plan

It is important to consider that Terraform reads from data sources during the plan phase and writes the result into the plan

66
Q

the lookup function

A

lookup(map, key, default)

> lookup({a=”ay”, b=”bee”}, “a”, “what?”)
ay
lookup({a=”ay”, b=”bee”}, “c”, “what?”)
what?

67
Q

what command removes the lock on the state for the current configuration. Be very careful forcing an unlock, as it could cause data corruption and problems with your state file.

A

terraform force-unlock

68
Q

Where does Terraform OSS store the local state for workspaces?

A

For local state, Terraform stores the workspace states in a directory called terraform.tfstate.d.

69
Q

In Terraform Enterprise, a workspace can be mapped to how many VCS repos?

A

A workspace can only be configured to a single VCS repo, however, multiple workspaces can use the same repo, if needed.

70
Q

In the terraform block, which configuration would be used to identify the specific version of a provider required?

A

required_providers