2. Read, Generate, Modify Configurations Flashcards

1
Q

Attributes and Output Values

  • Terraform has the capability to output the attribute of a resource with the output values.
  • An outputed attributes can not only be used for the user reference but it can also act as an input to other resources being created via terraform
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Resource block

The format of resource block configurations is as follows:

< block type > “< resource type >” “< local name/label >“

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

Variables - Assignment

Variables in Terraform can be assigned values in multiple ways.

Some of these include:

  • Environment variables
    • export TF_VAR_instancetype=”t2.nano”
  • echo $TF_VAR*
  • Command Line Flags
    • terraform plan -var=”instancetype=t2.small”
  • terraform plan -var-file=”custom.tfvars”*
  • From a File (e.g. terraform.tfvars)
  • Variable Defaults
    • variable “instancetype” {
  • default = “t2.micro”*
  • }*
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Variables - Type constraints

  • The type argument in a variable block allows you to restrict the type of value that will be accepted as the value for a variable
    • variable “image_id” {

type = string

}

  • If no type constraint is set then a value of any type is accepted.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Variables - Data Types

  • string
  • number
  • list
  • map
  • set
  • object
  • tuple

A collection type allows multiple values of one other type to be grouped together as a single value. This includes list, map, and set.

A structural type allows multiple values of several distinct types to be grouped together as a single value. This includes object and tuple.

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

Count Parameter

  • The count parameter on resources can simplify configurations and let you scale resources by simply incrementing a number.
  • In resource blocks where the count is set, an additional count object is available in expressions, so you can modify the configuration of each instance.
  • This object has one attribute:
    • count.index — The distinct index number (starting with 0) corresponding to this instance.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Variables - Local Values

  • A local value assigns a name to an expression, allowing it to be used multiple times within a module without repeating it.
  • Local Values can be used for multiple different use-cases like having a conditional expression.
  • Local values can be helpful to avoid repeating the same values or expressions multiple times in a configuration.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Terraform Functions

  • The Terraform language includes a number of built-in functions that you can use to transform and combine values.
  • The general syntax for function calls is a function name followed by comma-separated arguments in parentheses:
    • function (argument1, argument2)
  • The Terraform language does not support user-defined functions, and so only the functions built into the language are available for use
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Debugging in Terraform

  • Terraform has detailed logs that can be enabled by setting the TF_LOG environment variable to any value.
  • You can set TF_LOG to one of the log levels TRACE, DEBUG, INFO, WARN or ERROR to change the verbosity of the logs
  • TRACE is the most verbose and it is the default if TF_LOG is set to something other than a log level name.
  • To persist logged output you can set TF_LOG_PATH in order to force the log to always be appended to a specific file when logging is enabled.
  • Environment variables can be used to set variables. The environment variables must be in the format TF_VAR_name and this will be checked last for a value.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Dynamic Block

  • Dynamic Block allows us to dynamically construct repeatable nested blocks which is supported inside resource, data, provider, and provisioner blocks
  • 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 (“ingress” in the example above).
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Terraform Taint

  • The terraform taint command manually marks a Terraform-managed resource as tainted, forcing it to be destroyed and recreated on the next apply.
  • This command will not modify infrastructure but does modify the state file in order to mark a resource as tainted.
  • Once a resource is marked as tainted, the next plan will show that the resource will be destroyed and recreated and the next apply will implement this change.
  • Note that tainting a resource for recreation may affect resources that depend on the newly tainted resource.
  • If a resource successfully creates but fails during provisioning, Terraform will error and mark the resource as “tainted”.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Dealing with large infrastructure

  • We can prevent terraform from querying the current state during operations like terraform plan with the -refresh=false flag
  • The -target=resource flag can be used to target a specific resource.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Backend Initialization​

You do not need to specify every required argument in the backend configuration. Omitting certain arguments may be desirable to avoid storing secrets, such as access keys, within the main configuration. When some or all of the arguments are omitted, we call this a partial configuration.

With a partial configuration, the remaining configuration arguments must be provided as part of the initialization process. There are several ways to supply the remaining arguments:

  • Interactively: Terraform will interactively ask you for the required values unless interactive input is disabled. Terraform will not prompt for optional values.
  • File: A configuration file may be specified via the init command line. To specify a file, use the -backend-config=PATH option when running terraform init. If the file contains secrets it may be kept in a secure data store, such as Vault, in which case it must be downloaded to the local disk before running Terraform.
  • Command-line key/value pairs: Key/value pairs can be specified via the init command line. Note that many shells retain command-line flags in a history file, so this isn’t recommended for secrets. To specify a single key/value pair, use the -backend-config=”KEY=VALUE” option when running terraform init.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Terraform Configuration Block

  • The special terraform configuration block type is used to configure some behaviors of Terraform itself, such as requiring a minimum Terraform version to apply your configuration.
  • For production use, you should constrain the acceptable provider versions via configuration file to ensure that new versions with breaking changes will not be automatically installed by terraform init in the future. When terraform init is run without provider version constraints, it prints a suggested version constraint string for each provider.
  • Use required_providers flag
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly