Terraform Flashcards
What’s Terraform?
Terraform is a tool for building, changing and versioning infrastructure safely and efficiently, locally or in the cloud.
Terraform files have .tf extension
What are the key features of Terraform?
- Infrastructure as Code
Your infrastructure is described using high-level configuration syntax. This allows your infrastructure to be versioned and treated as you would any other code. It can also be shared and re-used - Execution Plans
Terraform generates an execution plan with its “planning” step. This shows what Terraform will do when you apply the configuration. - Resource Graph
Terraform builds infrastructure as efficiently as possible, and operators get insight into dependencies in their infrastructure. It accomplishes this by building a graph of all your resources. - Change Automation
Complex changes can be applied to your infrastructure with minimal interaction. With the combination of the execution plan and resource graph, you will know exactly what Terraform will change and in what order. This will help avoid many possible human errors.
What’s Terraform written in?
Terraform is written in a HashiCorp configuration language.
Terraform language’s main purpose is to declare resources. This represents infrastructure objects. All the different features are present to accommodate more flexible and convenient resource definition.
- > Blocks are containers for objects like resources
- > arguments assign a value to a name
- > expressions represent a value
Explain the concept of Providers in Terraform
Terraform relies on plugins called “providers” to interact with cloud providers, SaaS providers, and other APIs.
Terraform configurations must declare which providers they require so that Terraform can install and use them.
Every resource type is implemented by a provider; without providers, Terraform can’t manage any kind of infrastructure.
What is the boilerplate of connecting to AWS Provider?
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 3.0" } } }
# Configure the AWS Provider provider "aws" { region = "us-east-1" }
How do you create resources within a provider?
resource “_” “name” {
config options…
= # Argument
}
Example of creating an EC2 Instance
What are the common Terraform CLI commands?
$ terraform init (initializes the directory)
$ terraform -chdir=
$ terraform plan (create an execution plan - it will show what my configuration intends to do)
$ terraform apply (apply changes in the configuration)
$ terraform destroy (destroy the managed infrastructure)
What are Plan, Deploy, and Cleanup Commands for terraform?
$ terraform plan -out (outputs a deployment plan to a file)
$ terraform plan -destroy (output a destroy plan)
$ terraform apply (apply a specific plan)
$ terraform apply -target= (only apply changes to a targeted resource)
$ terraform apply -var my_variable= (pass a variable via the command line)
$ terraform apply -var-file=”testing.tfvars” (pass a tf vars file as an argument)
$ terraform providers (get a provider info used in configuration)
What are Terraform resources?
Resources are the most imporant part of the Terraform language. Resource blocks describe infrastructure objects like virtual networks, compute instances, or components like DNS records.
Resource Types:
- > Providers, which are plugins for Terraform that offer a collection of resources types. (Need to be configurated in the root folder)
- > Arguments, which are specific to the selected resource type.
- > Documentation, which every provider uses to describe its resource types and arguments.
What are meta arguments?
Meta-Arguments can be used with any resource type to change the behaviour of the resource.
depends_on (specify hidden dependencies)
count (create multiple resource instances according to a count)
for_each (create multiple instaces according to a map or a set of strings instead of a number)
provider (select a non-default provider configuration)
lifecycle (set lifecycle customizations)
provisioner and connection (take extra actions after resource creation)
What are operation timeouts?
There are some resource types that provide special timeouts, nested block arguments that allow for customization of how long certain operations are allowed to take before they are deemed failed.
resource "aws_db_instance" "example" { # ... timeouts { create = "60m" delete = "2h" } }
How Terraform configuration is applied?
- Create (create resources that exist in the configuration but are not associated with a real infrastructure object in the state.)
- Destroy (destroy resources that exist in the state but no longer exist in the configuration)
- Update in-place (update in-place resources whose arguments have changed)
- Destroy and re-create (Destroy and re-create resources whose arguments have changed, but which cannot be updated in-place due to remote API limitation)
What are Input Variables and how do you use them?
Input variables serve as parameters for a Terraform module. They allow aspects of the module to be customized without altering the actual module.
Declaring an Input Variable:
variables “image_id” {
type = string
}
Optional Arguments: default, type, description, validation, sensitive
How can you assign values to variables:
- In a Terraform Cloud workspace
- Individually, with the -var command like option
- In variable definitions files like .tfvars or .tfvars.json
- As environment variables
A value can b accessed from an expression using var. keyword.
What are Output Variables?
Output Variables are like Return Values.
- A child module can use them to expose a subset of resource attributes to the parent module.
- A root module can use them to print values in the CLI.
- Root module outputs can be accessed by other configurations via the terraform_remote_state data source.
Example:
output “instance_ip_addr” { // valid identifier
value = aws_instance.server.private_ip // takes an expression whose result will be returned to the user
}
What are local values?
Local values are like temporary function’s local variables.
Allow to:
- Assign a name to an expression
- Use the variable multiple times within a module without repeating it.
You can reference them in expressions as local.
Local values can only be accessed in expressions within the module where they were declared.