Exam Prep Flashcards
(70 cards)
What does terraform init do?
The terraform init command initializes a working directory.
Initialization includes installing provider plugins, backend initialization, copy source modules, etc.
This is the first command that should be run after writing a new terraform config, its safe to run multiple times.
Terraform init -upgrade
The terraform init -upgrade installs the latest module and provider versions allowed within the configured constraints.
If you have the latest provider plugin already installed and define a new version constraint that matches different version you will need to run terraform init -upgrade.
Terraform plan
It allows you to create an execution plan.
The infrastructure is not modified as part of this plan.
The state file is not modified even when it detects drift in real-world and current infrastructure.
How to save the plan to a file?
-out=FILE option to save the generated plan to a file on disk, which can later execute by passing the file to terraform apply as an extra argument (terraform apply ec2.plan, for ex).
Terraform apply
Used to apply the changes required to reach the desired state of the config.
The state file gets modified in this command
Name of state file = terraform.tfstate.
Terraform apply can change, destroy, and provision resources but cannot import any resources.
Terraform destroy
Used to destroy the terraform-managed infrastructure
terraform destroy command is not the only command through which infrastructure can be destroyed.
* You can also either comment out the code in the code or delete the code entirely.
Terraform fmt
Used to rewrite terraform config files to a canonical format/style. It will directly perform “write” operation and not “read” as in it will not ask for confirmation and just format.
Two flags:
1. -check | Checks if input is formatted, files not modified.
2. -recursive, also process files in subdirectories, by default only the given directory (or current directory) is processed.
Terraform validate
It validates the config files in a directory.
It requires an initialized working directory with any referenced plugins/modules installed, i.e n
Terraform plan uses implied validation check (even if you don’t perform a terraform validate)
Whats a resource block?
A resource block declares a resource of a given type (“aws_instance) with a given local name (“web”)
Resource type and name together serve as an identifier for a given resource and so must be unique.
Address of the resource is resource type.localname
Ex: aws_instance.web
The statements inside the resource is made up of the argument name and argument value.
Ex: ami = “ami-123”
Terraform refresh? whats it do again?
The terraform refresh command reads the current settings from all managed remote objects and updates the terraform state to match.
This wont modify your real remote objects, but it will modify the terraform state.
This command is DEPRACATED, because its default behavior is unsafe.
Are arrays possible in Terraform?
No
Why is the terraform state command used?
Its used for advanced state management
Terraform import
You can use import blocks to import more than one resource at a time
Local values
Locals are used whne you want to avoid repeating the same expression multiple times
Local values are created by a locals block (plural), but you reference them as attributes on an object named local (singular)
Local values can reference values from other variables, locals, etc.
Terraform Modules
Terraform modules allow us to centralize the resource config, and it makes it easier for multiple projects to re-use the terraform code.
Instead of writing code from scratch, we can re-use.
Modules source code can be present in a wide variety of locations.
Github, local pths, terraform reg, s3 buckets.
To reference a module, you need to make use of module block and source.
Terraform uses this during the module installation step of terraform init to download the source code to a directory on local disk so that other terraform commands cna use it.
Module local paths
A local path must begin with either ./ or ../ to indicate a local path.
Module sourced from local paths do NOT support versions
Git repository module
Arbitrary Git repositories can be used by prefixing the address with the special git:: prefix.
Root vs child modules
Root modules reside in the main working directory of terraform config. This is the entry point of infrastructure definition.
A module that has been called by another module is referred to as the child module.
Module outputs
A child module can use outputs to expose a subset of its resource attributes to a parent module.
Format: <MODULE>.<OUTPUT></OUTPUT></MODULE>
Module versioning
When using modules installed from a module registry, Hashicorp recommends explicitly constraining the acceptable version #’s to avoid unexpected or unwanted changes.
It is not mandatory to specify a version argument
Terraform registry
Hosts a broad collection of public terraform modules
Each terraform module has an associated address
A module address has the syntax hostname/namespace/name/system.
The hostname/ portion of a module is optional, and if omitted defaults to the namespace and beyond.
Ex: registry.terraform.io would drop off if not included and reduce to
source = “terraform-aws-modules/ec2-instance/aws”
Functions in terraform
the terraform language includes a # of built-in functions that you can use to transform and combine values.
NO SUPPORT for user-defined functions
Function categories
Numeric = abs, ceil, floor, max min
String = concat, replace, split, join, tolower, toupper
Collection - element, keys, length, merge, sort, slice
Fiesystem - file, filebase64, dirname
Lookup function
Lookup retrieves the value of a single element from a map, given its key. If the given key does not exist, the given default value is returned instead.