Terraform Basics Flashcards
Providers, Resources, State File, Keys, Variable Types, User Input, Dependencies, Built-In Functions, Versioning, Modules (70 cards)
Write a basic **provider block **for AWS for region “eu-west-2”
provider "aws" { region = "eu-west-2" }
What are “1” and “2” in the resource block? Provide examples for creating an EC2 instance.
resource "1" "2" { ... }
1: Resource Type
ex: “aws_instance”
2: Resource Name
ex: “my_instance”
terraform init
creates a ____ file called ____
State file
terraform.tfstate
What happens if you:
1. terraform apply
2. Delete the terraform.tfstate
file
3. terraform apply
again
Why does this happen?
The resources are created a second time.
This is because there is no state file to tell Terraform which resources already exist.
One way to avoid hard-coding variables into Terraform is by using ____ variables that are set in the Windows CLI with the ____ command or in Linux with the ____ command
Env variables
Linux: export
Windows CLI: setx
Another way to avoid hard-coding variables into Terraform, specifically for AWS, is to use ____, which stores credentials in a ____ folder.
AWS CLI
Stores variables in a hidden folder
Declare a** string** variable called “vpcname” with a default value “myvpc”
variable "vpcname" { type = "string" default = "myvpc" }
Declare a number variable called “sshport” with a default value 22
variable "sshport" { type = "number" default = 22 }
Declare a boolean variable called “enableDebug” that defaults to false
variable "enableDebug" { type = bool default = false }
What behavior does a list variable mimic from other languages?
Declare a list of strings variable called “mylist” with default entries of “First” and “Second”
List is like an array.
variable "mylist" { type = list(string) default = ["First", "Second"] }
What is a map variable?
Declare a map variable called “mymap” with default values of your choice.
A map is a set of one or more key-value pairs.
variable "mymap" { type = "map" default = { Key1 = "Value1" Key2 = "Value 2" } }
- Declare a string variable called “vpcname” with default value “myvpc”
- Declare a list variable called “mylist” with default values “First” and “Second”
- Declare an aws_vpc resource called “myvpc” with CIDR block “10.0.0.0/16”, a tag called “Name” equal to the value of the vpcname variable, and a tag called “Item” equal to the value of the first item in “mylist”.
variable "vpcname" { type = "string" default = "myvpc" } variable "mylist" { type = "list" default = ["First", "Second"] } aws_vpc "myvpc" { cidr_block = "10.0.0.0/16" tags = { Name = var.vpcname Item = var.mylist[0] } }
What is string interpolation? How do you do it in terraform?
Replace the value inside of a string with a variable.
“My name is ${var.name}”
variable "mymap" { type = map default = { Key1 = "Value1" Key2 = "Value2" } }
How do you reference the value of Key1 from this variable?
var.mymap[“Key1”]
What denotes a variable as being set by user input?
What are the rules for when Terraform prompts for this input?
A variable that lacks a default value.
Always prompted with plan
Only prompted with apply if there is no saved value from plan
Declare a string variable called “instance_name” that prompts the user “Set the name of the Instance”.
variable "instance_name" { type = string description = "Set the name of the Instance" }
How would you reference the “id” attribute of a resource that is created?
Give an example for an EC2 instance called “myinstance”
Hint: ____.____.id
resource type.resource name.id
ex: aws_instance.myinstance.id
Given that your Terraform is creating an EC2 instance called “mySQL_instance”, create an output to display it’s ID.
output "instanceid" { value = aws_instance.mySQL_instance.id }
What is the only step in which outputs are available? Why?
apply because the resource must actually be created.
Fill in the block to assign tags from a map variable called “my-tags” to the instance:
aws_instance "my-instance" { ami = "ami-053b0d53c279acc90" instance_type = "t2.micro" tags = \_\_\_\_ }
tags = var.my-tags
Variable types:
A ____ is an ordered collection that allows duplicates. Uses ____ characters to contain items.
A ____ is an unordered collection with unique values. Uses ____ characters to contain items.
A ____ is an unordered collection of key-value pairs with unique keys. Uses ____ characters to contain items.
A ____ is a fixed-length, ordered collection that allows multiple data types. Uses ____ characters to contain items.
Which types can be used with for_each to create multiple EC2 instances? Why?
A list is ordered and allows duplicates. Uses brackets.["First", "Second", "Second", "Third"]
A set is unordered and must be unique values. Uses brackets.["John", "Joe", "Jim"]
A map is unordered key-value pairs and must be unique keys. Uses curly brackets.
{ Key1 = Value1 Key2 = Value2 }
A tuple is ordered, fixed-length, and allows multiple data types. Uses brackets. Used to contain multiple values in a strict structure. ["web-server", 8080, true]
Can use for_each with sets or maps to create multiple EC2 instances because they enforce unique values.
Fill in the block to create a separate EC2 instance for each item in the set.
variable instance_names { type = set(string) default = ["First", "Second", "Third"] } resource "aws_instance" "example" { \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ ami = "ami-053b0d53c279acc90" tags = { Name = \_\_\_\_\_\_ } }
variable instance_names { type = set(string) default = ["First", "Second", "Third"] } resource "aws_instance" "example" { for_each = var.instance_names ami = "ami-053b0d53c279acc90" tags = { Name = each.value } }
When using for_each to loop through a list, Terraform provides ____ for accessing the values.
When using for_each to loop through a map, Terraform provides ____ for accessing the values.
**List: ** each.value
**Map: ** each.key, each.value
What is a dynamic block? What 2 variable types is it usually paired with? What 2 components are usually in the dynamic block?
A way to generate multiple blocks dynamically from a list or map variable.
Paired with list or map variable.
Usually there’s also a for_each and a content block.