Terraform for the Absolute Beginners - udemy - labs
lab 01: HCL Basics
Terraform extension: .tf
main.tf
:
resourc "local_file" "games" {
file = "/root/favorit-games"
content = "FIFA 21"
}
- resource type:
local_file
- resource name:
games
- provider:
local
- 3 phases of terraform:
- init
- plan
- apply
- in the
main.tf
there are two errors:- a missing required argument:
filename
- an invalid argument:
file
- this info can be checked in the docs here
- a missing required argument:
If you don't want to have the contents of the file being created to be echoed on the screen, use the resource type local_sensitive_file
.
lab 02: Terraform Providers
main.tf
:
resource "local_file" "things-to-do" {
filename = "/root/things-to-do.txt"
content = "Clean my room before Christmas\nComplete the CKA Certification!"
}
resource "local_file" "more-things-to-do" {
filename = "/root/more-things-to-do.txt"
content = "Learn how to play Astronomia on the guitar!"
}
In both resources, the provider is local
.
Provider types:
- official
hashicorp/<provider>
- partner
registry.terraform.io/<organization>/<provider>
- community
registry.terraform.io/<username>/<provider>
In the lab there was an example with registry.terraform.io/<somename>/ansible
. What makes it clear that it refers to a community plugin is the fact that ansible
is managed by redhat
. So, previous knowledge is required.
lab 03: Multiple Providers
Nothing very significant...
When we change the .tf
file we need to run terraform init
again.
lab 04: Variables
variable types:
- string
- number
- bool
- any
- list
- map
- set (like a list, but with no repetition)
- object
- tuple (like a list, but with mixed types)
Variables are accessed by prefixing the var name with var.
lab 05: Using variables in terraform
getting vars from env: ENV_VAR_<variable_name>
The argument for -var-file
is a a *.tfvars
file.
variáveis em arquivos .tfvars
são definidas simplesmente assim:
var_name = var_value
para que as variáveis sejam carregadas, é necessário que isso seja explicitado no arquivo variables.tf
.
lab 06: Resource attributes
Some resources don't need any arguments. Example:
resource "time_static" "time_update" {
}
To access the id
we use time_static.time_update.id
.
Interpolation: `"Time stamp of this file is ${time_static.time_update.id}"
lab 07: Resource Dependencies
Declaring an explicit dependency:
resource "local_file" "pet" {
filename = var.filename
content = "My favorite pet is Mr.Cat"
# here #############
depends_on = [
random_pet.my-pet
]
####################
}
resource "random_pet" "my-pet" {
prefix = var.prefix
separator = var.separator
length = var.length
}
lab 08: Output Variables
output "<variable-name>" {
value = "<variable-value>"
<arguments>
}
inspect the value with terraform output
or terraform output <variable-name>
.
lab 09: Terraform State
- Terraform State file is placed in the same dir as the
.tf
files. - Default name:
terraform.tfstate
- The extension:
.tfstate
- Format is JSON.
The command terraform show
is useful to check the terraform state.
NOTE: Terraform State stores sensitive data.
lab 10: Terraform Commands
Use terraform validate
to check your .tf
config files.
Use terraform fmt
to format your code.
Use terraform providers
to list the providers currently in use.
lab 11: Data Sources
Data Sources go in a data
block.
resource vs. data source:
resource | data source |
---|---|
keyword: resource | keyword: data |
creates, updates and destroys infrastructure | reads infrastructures |
aka "managed resources" | aka "data resources" |
lab 12: count and for_each
count
# main.tf
resource "local_file" "pet" {
count = 3
filename = var.filename[count.index]
}
# variables.tf
variable "filename" {
default = [
"/root/pets.txt",
"/root/dogs.txt",
"/root/cats.txt"
]
}
Note that count = 3
is hardcoded above 👆
A more dynamic way would be to use count = length(var.filename)
for_each
# main.tf
resource "local_file" "pet" {
filename = each.value
for_each = toset(var.filename)
}
# variables.tf
variable "filename" {
type = list(string)
default = [
"/root/pets.txt",
"/root/dogs.txt",
"/root/cats.txt"
]
}
- count vs. for_each
count
creates resources as a listfor_each
creates resources as a map