Read, generate, and modify the configuration Flashcards

1
Q

How do you define a variable?

A

variable “region” {
default = “us-east-1”
}
This defines the region variable within your Terraform configuration.

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

How do you access the variable in the configuration?

A
// accessing a variable
provider "aws" {
    region = var.region
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many ways you can assign variables in the configuration?

A

Command-line flags:
terraform apply -var ‘region=us-east-1’

From a file:
To persist variable values, create a file and assign variables within this file. Create a file named terraform.tfvars with the following contents:
region = “us-east-1”

terraform apply \

  • var-file=”secret.tfvars” \
  • var-file=”production.tfvars”

From environment varibles:
Terraform will read environment variables in the form of TF_VAR_name to find the value for a variable. For example, the TF_VAR_region variable can be set in the shell to set the region variable in Terraform.

UI input:
If you execute terraform apply with any variable unspecified, Terraform will ask you to input the values interactively. These values are not saved, but this provides a convenient workflow when getting started with Terraform. UI input is not recommended for everyday use of Terraform.

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

Does environment variables support List and map types?

A

No
Environment variables can only populate string-type variables. List
and map type variables must be populated via one of the other
mechanisms.

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

How do you provision infrastructure in a staging environment or a production
environment using the same Terraform configuration?

A
You can use different varible files with the same configuration
// Example// For development
terraform apply -var-file="dev.tfvars"
// For test
terraform apply -var-file="test.tfvars"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you assign default values to variables?

A

If no value is assigned to a variable via any of these methods and the variable has a default key in its declaration, that value will be used for the variable.

variable “region” {
default = “us-east-1”
}

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

What are the data types for the variables?

A
string
number
bool
list()
set()
map()
object({ = , ... })
tuple([, ...])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Give an example of data type List variables?

A

Lists are defined either explicitly or implicitly.

variable “availability_zone_names” {
type = list(string)
default = [“us-west-1a”]
}

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

Give an example of data type Map variables?

A
variable "region" {}
variable "amis" {
    type = map(string)
}
amis = {
    "us-east-1" = "ami-abc123"
    "us-west-2" = "ami-def456"
}
// accessing
resource "aws_instance" "example" {
    ami = var.amis[var.region]
    instance_type = "t2.micro"
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the Variable Definition Precedence?

A

The above mechanisms for setting variables can be used together in any combination. If the same variable is assigned multiple values,
Terraform uses the last value it finds, overriding any previous values. Note that the same variable cannot be assigned multiple values within a single source.
Terraform loads variables in the following order, with later sources taking precedence over earlier ones:
* Environment variables
* The terraform.tfvars file, if present.
* The terraform.tfvars.json file, if present.
* Any *.auto.tfvars or *.auto.tfvars.json files, processed in lexical order of their filenames.
* Any -var and -var-file options on the command line, in the order they are provided. (This includes variables set by a Terraform Cloud workspace.)

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

What are the output variables?

A

output variables as a way to organize data to be easily queried and shown back to the Terraform user.Outputs are a way to tell Terraform what data is important. This data is outputted when apply is called, and can be queried using the terraform output command.

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

How do you define an output variable?

A

output “ip” {
value = aws_eip.ip.public_ip
}
Multiple output blocks can be defined to specify multiple output variables.

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

How do you view outputs and queries them?

A

You will see the output when you run the following command terraform apply
You can query the output with the following command
terraform output ip

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

What are the dynamic blocks?

A

some resource types include repeatable nested blocks in their arguments, which do not accept expressions
You can dynamically construct repeatable nested blocks like setting using a special dynamic block type, which is supported inside resource, data, provider, and provisioner blocks:
A dynamic block acts much like a for expression, but produces nested blocks instead of a complex typed value. It iterates over a given complex value, and generates a nested block for each element of that
complex value.
https://www.terraform.io/docs/configuration/expressions.html#dynamic-blocks

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

What are the best practices for dynamic blocks?

A

Overuse of dynamic blocks can make configuration hard to read and
maintain, so we recommend using them only when you need to hide
details in order to build a clean user interface for a re-usable
module.
Always write nested blocks out literally where possible.

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

What are the Built-in Functions?

A

The Terraform language includes a number of built-in functions that
you can call from within expressions to transform and combine values.
max(5, 12, 9)

17
Q

Does Terraform language support user-defined functions?

A

No
The Terraform language does not support user-defined functions, and so only the functions built in to the language are available for use.

18
Q

What is the built-in function to change string to a number?

A

parseint parses the given string as a representation of an integer in the specified base and returns the resulting number. The base must be between 2 and 62 inclusive.
> parseint(“100”, 10)
100

More Number Functions here
https://www.terraform.io/docs/configuration/functions/abs.html

19
Q

What is the built-in function to evaluates given expression and returns a boolean
whether the expression produced a result without any errors?

A

can
condition = can(formatdate(“”, var.timestamp))
https://www.terraform.io/docs/configuration/functions/can.html

20
Q

What is the built-in function to evaluates all of its argument expressions in turn
and returns the result of the first one that does not produce any errors?

A
try
locals {
example = try(
[tostring(var.example)],
tolist(var.example),
)
}
21
Q

What is Resource Address?

A
A Resource Address is a string that references a specific resource in
a larger infrastructure. An address is made up of two parts:
[module path][resource spec]
22
Q

What is the Module path?

A

A module path addresses a module within the tree of modules. It takes
the form:
module.A.module.B.module.C…
Multiple modules in a path indicate nesting. If a module path is specified without a resource spec, the address applies to every resource within the module. If the module path is omitted, this addresses the root module

23
Q

What is the Resource spec?

A
A resource spec addresses a specific resource in the config. It takes
the form:
resource_type.resource_name[resource index]
* resource_type - Type of the resource being addressed.
* resource_name - User-defined name of the resource.* [resource index] - an optional index into a resource with multiple
instances, surrounded by square brace characters ([ and ]).
// Examples
resource "aws_instance" "web" {
# ...
count = 4
}
aws_instance.web[3]
aws_instance.web
// Refers to only last instance
// Refers to all four "web" instances.
resource "aws_instance" "web" {
# ...
for_each = {
"terraform": "value1",
"resource": "value2",
"indexing": "value3",
"example":
"value4",
}
}
aws_instance.web["example"] // Refers to only the "example" instance
in the config.
24
Q

What are complex types and what are the collection types Terraform supports?

A

A complex type is a type that groups multiple values into a single
value.
There are two categories of complex types:
collection types (for grouping similar values)
* list(…): a sequence of values identified by consecutive whole
numbers starting with zero.
* map(…): a collection of values where each is identified by a
string label.
* set(…): a collection of unique values that do not have any
secondary identifiers or ordering.
structural types (for grouping potentially dissimilar values).
* object(…): a collection of named attributes that each have their
own type.* tuple(…): a sequence of elements identified by consecutive whole
numbers starting with zero, where each element has its own type.

25
Q

What are the named values available and how do we refer to?

A

Terraform makes several kinds of named values available. Each of
these names is an expression that references the associated value;
you can use them as standalone expressions, or combine them with
other expressions to compute new values.
* . is an object representing a managed resource
of the given type and name. The attributes of the resource can be
accessed using dot or square bracket notation.
* var. is the value of the input variable of the given name.
* local. is the value of the local value of the given name.
* module.. is the value of the specified
output value from a child module called by the current module.
* data.. is an object representing a data resource
of the given data source type and name. If the resource has the count
argument set, the value is a list of objects representing its
instances. If the resource has the for_each argument set, the value
is a map of objects representing its instances.
* path.module is the filesystem path of the module where the
expression is placed.
* path.root is the filesystem path of the root module of the
configuration.
* path.cwd is the filesystem path of the current working directory.
In normal use of Terraform this is the same as path.root, but some
advanced uses of Terraform run it from a directory other than the
root module directory, causing these paths to be different.
* terraform.workspace is the name of the currently selected
workspace.

26
Q

What is the built-in function that reads the contents of a file at the given path and
returns them as a base64-encoded string?

A

filebase64(path)

https://www.terraform.io/docs/configuration/functions/filebase64.html

27
Q

What is the built-in function that converts a timestamp into a different time format?

A

formatdate(spec, timestamp)

28
Q

What is the built-in function encodes a given value to a string using JSON syntax?

A

jsonencode({“hello”=”world”})

https://www.terraform.io/docs/configuration/functions/jsonencode.html

29
Q

What is the built-in function that calculates a full host IP address for a given host number within a given IP network address prefix?

A

> cidrhost(“10.12.127.0/20”, 16)

  1. 12.112.16
    https: //www.terraform.io/docs/configuration/functions/cidrhost.html