Read, generate, and modify the configuration Flashcards
How do you define a variable?
variable “region” {
default = “us-east-1”
}
This defines the region variable within your Terraform configuration.
How do you access the variable in the configuration?
// accessing a variable provider "aws" { region = var.region }
How many ways you can assign variables in the configuration?
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.
Does environment variables support List and map types?
No
Environment variables can only populate string-type variables. List
and map type variables must be populated via one of the other
mechanisms.
How do you provision infrastructure in a staging environment or a production
environment using the same Terraform configuration?
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 do you assign default values to variables?
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”
}
What are the data types for the variables?
string number bool list() set() map() object({ = , ... }) tuple([, ...])
Give an example of data type List variables?
Lists are defined either explicitly or implicitly.
variable “availability_zone_names” {
type = list(string)
default = [“us-west-1a”]
}
Give an example of data type Map variables?
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" }
What is the Variable Definition Precedence?
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.)
What are the output variables?
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 do you define an output variable?
output “ip” {
value = aws_eip.ip.public_ip
}
Multiple output blocks can be defined to specify multiple output variables.
How do you view outputs and queries them?
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
What are the dynamic blocks?
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
What are the best practices for dynamic blocks?
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.