Terraform Commands/Random Features Flashcards

1
Q

What does the terraform fmt command do

A

It is used to rewrite Terraform config files to take care of the overall formatting.

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

How does Terraform load config files if there are a number of them in the working directory?

A

Terraform loads the config files in the same directory in alphabetical order.

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

What file format will Terraform load using alphabetical order?

A

The files loaeded must end in either .tf or .tf.json to specify the format that is in use.

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

How can you report Terraform bugs?

A

You can report bugs in the Terraform Core Github or appropriate provider page

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

Whats debugging?

A

The process of finding the root cause of a specific issue.

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

What’s an important requirement for debugging?

A

Getting detailed log information.

Depending on the app, the approach to get detailed logs will differ.

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

What flag do you add to Terraform to output log information?

A

-v

If you need more log info you’d add more v’s.

Ex. -vvvv

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

How to get detailed logs?

A

You can set the TF_LOG environment variable to any value.

The value for TF_LOG can be set to the values below where TRACE is the most verbose and ERROR is the least verbose.

TRACE
DEBUG
INFO
WARN
ERROR

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

Can you store log from Terraform to a file?

A

Yes, you can set the TF_LOG_PATH environment variable to force the log to always be appended to a specific file when logging is enabled.

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

How to set the TF_VAR env variable in Windows?

A

The command,

set TF_LOG=VALUE

where VALUE is the log level (TRACE,DEBUG,etc)

The command

set TF_LOG_PATH=file.txt
saves the information to a text file

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

How to set the TF_VAR env variable in Linux?

A

export TF_LOG=VALUE

where VALUE is the log level (TRACE,DEBUG,etc)

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

How do you output to terraform?

A

output “name” {
value = resource
}

Ex:
output “iam_names” {
value = aws_iam_user.lb[*].name
}

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

Whats the purpose of the terraform output command?

A

It’s used to extract the value of an output variable from the state file.

terraform output variable

Ex: terraform output iam_names

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

What are terraform settings used for?

A

Terraform settings are used to configure project specific terraform behaviors, such as requiring a minimum terraform version to apply to your configuration.

Terraform settings are gattered in a “terraform block”

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

What is a terraform block used for?

A

To configure specific figures.

Ex:
terraform {
required_version = “1.8.1”
}

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

How do you specify a specific terraform version in code?

A

If your coat is compatible with specific version of terraform, you can use the “required_aversion “block to add your constraints.

Ex:
required_version = “1.8.1”

17
Q

What use cases are best in using terraform blocks?

A
  1. Specificying certain provider versions (using required_version command)
  2. Specifying provider requirements (using the required_providers block)
  3. Backend configuration setttings
  4. Experimental features settings
18
Q

Whats the required_providers block used for?

A

It can be used to specific certain provider requirements.

Ex:
required_providers {
aws = {
version = “5.6.0”
source = “hashicorp/aws”
}
}

19
Q

What is resource targeting in Terraform used for?

A

If you only want to create a specific resource even though they may be defined in a .tf file it allows to apply changes to a specific subset of resources rather than applying changes to your entire infrastructure through a terraform plan/apply/destroy

The flag tied to this concept is “-target”

Ex: terraform plan -target resource.name

terraform plan -target aws_iam_user.dev

20
Q

Whats a great use case of terraform resource targeting?

A

Occasionally, you may want to apply only part of a plan, such as when terraform state is become out of sync with your resources due to a network failure, a problem with the upstream cloud platform, or a bug and terraform or its providers.

Targeting individual resources can be useful for troubleshooting errors, but it should not be part of your normal workflow

21
Q

What are some solutions to large infrastructure with Terraform?

A

Using a cloud provider does not mean you have access to a limited resources, there are some limits that apply to all of the services

One thing you have to be careful of when using architecture for the cloud is API throttling, particularly the types of calls in the frequency with which they are called

When allotted rate limit for an API call is exceeded, youll receive an error response and the call with be throttled.

22
Q

What are some workarounds for large-infra and possible issues?

A
  1. Convert a big project into multiple smaller projects and when you run terraform commands.
  2. Make use of resource targeting
  3. Setting refresh=false
23
Q

Whats the zipmap function in Terraform?

A

The zip map function constructs a map from a list of keys and a corresponding list of values

Ex
zipmap(keyslist, valueslist)

zipmap([“a”, “b”], [1,2])

Output
“a” = 1
“b” = 2

24
Q

What’s a simple used case for zip map function?

A

When creating multiple EAM users, you need output, which contains direct mapping of IAM names and ARNs

25
Q

How do you add a comment in Terraform?

A
  1. Put a # before the line for a single-line comment
  2. Comment a block out by adding start/end delimiters
    /*
    */
  3. Put a // before the line for a single-line comment
26
Q

How does terraform apply a configuration as a default behavior?

A
  1. Create resources that exist in the configuration, but are not associated with a real infrastructure object in the state.
  2. Destroy resources that exist in the state, but no longer exist in the configuration.
  3. Update in place resources whose arguments have changed.
  4. Destroy and re-create resources whose arguments have changed, but which cannot be updated in place due to remote API limitations
27
Q

How do you change the default behavior of terraform for config changes?

I.e some modifications happen in real infrastructure object that is not part of terraform, but you want to ignore those changes during terraform apply

A

With the use of meta- arguments “INSIDE” the resource block, this can be accomplished. The meta argument example below is the “lifecycle” meta-argument. There are many other arguments available.

Ex.

resource “aws_instance” “myec2” {
lifecycle {
ignore_changes = [tags]
}

28
Q

What arguments are available within the life cycle meta – argument block?

A
  1. Create_before_destroy - new replacement object is created first, and the prior object is destroyed after the replacement is created
  2. Prevent_destroy - terraform to reject with an error any plan that would destroy the infrastructure object associated with the resource.
  3. Ignore_changes - ignore certain changes to the live resource that does not match the configuration.
  4. Replace_triggered_by - replace the resource when any of the referenced items change
29
Q

Create_before_destroy lifecycle meta-argument

A

By default, when terraform must change a resource argument that cannot be updated in place due to remote API limitations, Teflon will instead destroy the existing object and create a new replacement object with the new configured arguments.

I.e creating an EC2 instance and then changing the AMI to a different OS triggers Terraform to destroy existing EC2 instance then create the new one based on the new OS/AMI ID

Issue: want new EC2 instance to be running prior to destruction of first instance.

Solution: use create_before_destroy argument.

30
Q

What are some challenges of the count meta-argument?

A

If the order of elements of index is changed, this can impact all of the other resources.

If your resources are almost identical, count is appropriate. If distinctive values are needed in the arguments, usage of

for_each

is recommended.

31
Q

What is a list data type?

A

Lists are used to store multiple items in a single variable and these items can be duplicated

The items that you add are indexed starting with an index of 0.

32
Q

What is a SET data type

A

Sets can only store unique elements but not duplicates, any duplicates are automatically removed.

When you define a SET, you also need to define the type of value expected.

ex: type = set(string)

33
Q

Whats some distinction of the SET variable

A

A set does not store the order of the elements like the list data type that stores them based on an index.

34
Q

How does the for_each argument work?

A

Allows you to manage multiple resources with different configurations for each resource

If a resource block includes a for_each meta argument whose value is a map or a set of strings, terraform creates one instance for each member of that map or set.

35
Q

In block where for_each