Key concepts of Terraform.
Initializes a Terraform working directory, setting up necessary local data.
-backend=false: Disables backend initialization.-reconfigure: Reconfigures backend, ignoring saved settings.-migrate-state: Migrates existing state during reconfiguration.-upgrade: Installs the latest module and provider versions.
Enables tab auto-completion. Requires re-login.
Formats Terraform configuration files (.tf and .tfvars) to a standard format.
-check: Checks if files are formatted without overwriting.-recursive: Processes files in subdirectories.
Checks configuration files for errors without accessing remote services.
Displays a tree of modules with their provider requirements.
Creates an execution plan without applying changes.
-out=path: Saves the plan to a file.-input=true: Prompts for variable input if not set.-var 'foo=bar': Sets a variable value.-var-file=filename: Loads variable values from a file.-destroy: Plans to destroy all managed objects.-refresh-only: Checks if remote objects match the last apply.-target=resource: Limits planning to specific resources.
Applies changes to infrastructure based on configuration files.
-auto-approve: Skips approval prompt.-replace: Forces replacement of a resource.-var 'foo=bar': Sets a variable value.-var-file=filename: Loads variable values from a file.-parallelism=n: Limits concurrent operations (default is 10).
Examples:
- To apply changes without approval and use a specific variable file:
terraform apply -auto-approve -var-file=web-prod.tfvars - To replace a specific resource:
terraform apply -replace="aws_instance.server"
Destroys managed infrastructure.
-auto-approve: Skips approval prompt.-target: Limits destruction to specific resources.
Example: To destroy a specific VPC without approval: terraform destroy -target aws_vpc.my_vpc -auto-approve
Manages Terraform state.
list: Lists resources in the state.show: Displays a resource in the state.mv: Moves an item in the state.rm: Removes instances from the state.pull: Outputs the current state.
Examples:
- To show details of a specific instance:
terraform state show aws_instance.my_vm - To pull the current state to a file:
terraform state pull my_terraform.tfstate - To move a role in the state:
terraform state mv aws_iam_role.my_ssm_role - To list all resources:
terraform state list - To remove a specific instance:
terraform state rm aws_instance.my_server
Updates the state file with current metadata without modifying infrastructure.
Marks a resource as needing replacement.
Removes the taint from a resource, marking it as functional.
Imports existing infrastructure into Terraform state.
Example: To import an EC2 instance with a specific ID: terraform import aws_instance.new_server i-123abc
Manages workspaces.
delete: Deletes a workspace.list: Lists workspaces.new: Creates a new workspace.select: Selects a workspace.show: Displays the current workspace name.
Displays output variables from the state file.
Examples:
- To list all outputs in JSON format:
terraform output -json - To display a specific output value:
terraform output instance_public_ip
Generates a dependency graph in DOT format.
Linux Example:
- To install Graphviz and generate a PNG graph:
sudo apt install graphviz terraform graph | dot -Tpng graph.png
Retrieves and saves an authentication token for the specified hostname.
Removes stored credentials for the specified hostname.
Displays the version of Terraform and all installed plugins.
A provider is a Terraform plugin that allows users to manage an external API.
A provider usually provides resources to manage a cloud or infrastructure platform, such as AWS or Azure, or technology (for example Kubernetes).
There are providers for Infrastructure as a Service (IaaS), Platform as a Service (PaaS), and Software as a Service (SaaS).
terraform {
required_providers {
aws = { # provider local name
source = "hashicorp/aws" # global and unique source address
version = "~3.0" # version constraint
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-central-1" # provider configuration options
}Resources are the core elements in Terraform, representing infrastructure objects to manage. Each resource is identified by a unique combination of its type and local name within a module, such as aws_vpc.main.
resource "<provider>_<resource_type>" "local_name" {
argument1 = value
argument2 = value
...
}
# Example:
resource "aws_vpc" "primary" {
cidr_block = "192.168.1.0/24"
enable_dns_support = true
tags = {
"Name" = "Primary VPC"
}
}Data sources in Terraform allow you to access information defined outside of Terraform, from other configurations, or modified by functions. Unlike managed resources, which Terraform creates, updates, and deletes, data sources only read existing objects.
# Output the content of a local file
output "os_version" {
value = data.local_file.os.content
}
# Define a data source to read a local file
data "local_file" "os" {
filename = "/etc/os-release"
}lifecycle is a nested block that can appear within a resource block. The lifecycle block and its contents are meta-arguments, available for all resource blocks regardless of type.
resource "aws_instance" "example" {
instance_type = "t2.micro"
ami = "ami-abc123"
lifecycle {
# first create new resource then destroy old
create_before_destroy = true
# This meta-argument, when set to true, will cause Terraform to reject with an error any plan that would destroy the infrastructure object associated with the resource, as long as the argument remains present in the configuration.
prevent_destroy = true,
# By default, Terraform detects any difference in the current settings of a real infrastructure object and plans to update the remote object to match configuration.
ignore_changes = [
# Ignore changes to tags, e.g. because a management agent
# updates these based on some ruleset managed elsewhere.
tags,
]
replace_triggered_by = [
# Replace `aws_appautoscaling_target` each time this instance of
# the `aws_ecs_service` is replaced.
aws_ecs_service.svc.id
]
# The AMI ID must refer to an AMI that contains an operating system
# for the `x86_64` architecture.
precondition {
condition = data.aws_ami.example.architecture == "x86_64"
error_message = "The selected AMI must be for the x86_64 architecture."
}
}
}Input variables allow customization of Terraform configurations without hard-coding values.
Variables can be declared anywhere in configuration files, but it's best practice to place them in a variables.tf file.
# Variable declaration
variable "subnet_cidr_block" {
description = "CIDR block for the subnet"
default = "10.0.1.0/24"
type = string
}- Use the default argument in the variable block.
- Define in a terraform.tfvars file. Example:
subnet_cidr_block = "10.0.2.0/24" - Use the -var option in the command line. Example:
terraform apply -var="subnet_cidr_block=10.0.3.0/24" - Use the -var-file option. Example:
terraform apply -var-file=prod.tfvars - Set as an environment variable. Example:
export TF_VAR_subnet_cidr_block="10.0.4.0/24"
- Command-line options -var and -var-file.
- terraform.tfvars file.
- Environment variables with TF_VAR prefix.
Interpolate values in strings using ${}, like ${var.subnet_cidr_block}. This allows referencing variables, resource attributes, and functions. Escape interpolation with $$, e.g., $${foo} renders as ${foo}.
-
Simple Types
- number
- string
- bool
- null
-
Complex Types
- Collection Types
- list
- map
- set
- Structural Types
- tuple
- object
- Collection Types
Defines a numeric variable.
variable "max_connections" {
description = "Maximum number of connections"
default = 100
type = number
}Defines a string variable.
variable "project_name" {
description = "Name of the project"
type = string
default = "MyProject"
}Defines a boolean variable.
variable "enable_logging" {
description = "Enable logging feature"
type = bool
default = false
}Defines a list of strings.
variable "supported_languages" {
description = "Supported languages"
type = list(string)
default = ["English", "Spanish", "French"]
}Defines a map of strings.
variable "region_amis" {
type = map(string)
default = {
"us-east-1" = "ami-12345678",
"us-west-2" = "ami-87654321"
}
}Defines a tuple with mixed types.
variable "server_config" {
type = tuple([string, number, bool])
default = ["m5.large", 2, false]
}Defines an object with specific attributes.
variable "firewall_rule" {
type = object({
from_port = number
to_port = number
protocol = string
cidr_blocks = list(string)
})
default = {
from_port = 22,
to_port = 22,
protocol = "tcp",
cidr_blocks = ["0.0.0.0/0"]
}
}Data sources in Terraform allow you to retrieve information about resources outside of Terraform's management, such as the public IP of an EC2 instance. Each data block specifies a data source (e.g., "aws_ami") and assigns it a local name (e.g., "ubuntu"), which must be unique within a module. The block body contains query constraints.
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["self"]
tags = {
Name = "web-server"
Tested = "true"
}
}Output values display information about your infrastructure and can be used by other Terraform configurations.
Use an output block to declare each output value. The label after the output keyword is the name.
output "server_ip" {
value = aws_instance.web.private_ip
}Terraform provides several looping constructs for different scenarios:
- count: Loop over resources.
- for_each: Loop over resources and inline blocks.
- for expressions: Loop over lists and maps.
The count meta-argument is used to manage multiple similar resources. It can be applied to any resource type or module.
# Create multiple EC2 instances
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t2.micro"
count = 3 # Creates 3 instances
}variable "users" {
type = list(string)
default = [ "/root/user1", "/root/user2", "/root/user3", "/root/user1" ]
}
variable "content" {
default = "password: abc"
}
resource "local_sensitive_file" "name" {
filename = var.users[count.index]
content = var.content
count = length(var.users)
}
In blocks with count, count.index provides the index number (starting at 0) for the current object.
for_each is used to duplicate resources that require different configurations. It is more flexible than count when resources need distinct values.
# Declare a variable
variable "usernames" {
type = list(string)
default = ["user1", "admin", "guest"]
}
# Create IAM users
resource "aws_iam_user" "user" {
for_each = toset(var.usernames) # Convert list to set
name = each.key
}variable "users" {
type = list(string)
default = [ "/root/user1", "/root/user2", "/root/user3", "/root/user1" ]
}
variable "content" {
default = "password: abc"
}
resource "local_sensitive_file" "name" {
filename = each.value
content = var.content
for_each = toset( var.users )
}
A for expression transforms a complex type into another complex type.
variable "names" {
type = list
default = ["alice", "bob", "charlie"]
}
output "uppercase_names" {
value = [for n in var.names : upper(n)]
}
output "filtered_names" {
value = [for name in var.names : upper(name) if length(name) > 3]
}Running terraform apply -auto-approve will produce:
Outputs:
filtered_names = [
"ALICE",
"CHARLIE",
]
uppercase_names = [
"ALICE",
"BOB",
"CHARLIE",
]A splat expression offers a concise way to perform operations that could be done with a for expression.
# Launch multiple EC2 instances
resource "aws_instance" "web" {
ami = "ami-87654321"
instance_type = "t2.micro"
count = 3
}
output "private_ips" {
value = aws_instance.web[*].private_ip # Splat expression
}Dynamic blocks in Terraform generate nested blocks by iterating over a complex value. They are used within resource, data, provider, and provisioner blocks to create multiple nested configurations.
# Define a list variable
variable "ingress_ports" {
description = "List of ingress ports"
type = list(number)
default = [22, 80, 443]
}
resource "aws_security_group" "web_sg" {
vpc_id = aws_vpc.main.id
# Use dynamic blocks to create ingress rules
dynamic "ingress" {
for_each = var.ingress_ports
iterator = port
content {
from_port = port.value
to_port = port.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
}Conditional expressions in Terraform select one of two values based on a boolean condition. The syntax is condition ? true_val : false_val.
variable "is_production" {
type = bool
default = false
}
# Create an instance based on the environment
resource "aws_instance" "app_server" {
ami = "ami-12345678"
instance_type = var.is_production ? "t2.large" : "t2.micro" # Conditional expression
}Local values in Terraform are named values used within a module. They are calculated within the configuration and do not change between runs.
locals {
owner = "DevOps Team"
project = "E-commerce Platform"
cidr_blocks = ["10.0.1.0/24", "10.0.2.0/24"]
tags = {
Name = "dev"
Environment = "development"
}
}
# Create a VPC with local tags
resource "aws_vpc" "dev_vpc" {
cidr_block = "10.0.0.0/16"
tags = local.tags
}
# Create a subnet using local CIDR blocks
resource "aws_subnet" "dev_subnet" {
vpc_id = aws_vpc.dev_vpc.id
cidr_block = local.cidr_blocks[0]
tags = local.tags
}Note: Local values are defined in a locals block but referenced as local.<name>.
Terraform provides built-in functions for transforming and combining values. These include functions for numbers, strings, collections, and more. User-defined functions are not supported.
Examples:
max(5, 12, 9) # Returns 12
min(12, 54, 3) # Returns 3
format("There are %d servers", 3) # Returns "There are 3 servers"
join(", ", ["apple", "banana", "cherry"]) # Returns "apple, banana, cherry"
split(",", "apple,banana,cherry") # Returns ["apple", "banana", "cherry"]
replace("hello world", "world", "Terraform") # Returns "hello Terraform"
substr("hello world", 1, 4) # Returns "ello"
element(["x", "y", "z"], 1) # Returns "y"
lookup({a="alpha", b="beta"}, "a", "unknown") # Returns "alpha"
slice(["x", "y", "z", "w"], 1, 3) # Returns ["y", "z"]
timestamp() # Returns current timestamp
formatdate("DD MMM YYYY", "2022-01-02T23:12:01Z") # Returns "02 Jan 2022"
cidrhost("192.168.1.0/24", 5) # Returns "192.168.1.5"You can experiment with these functions using the terraform console.
Terraform uses backends to define how operations are executed and where the state is stored. The default is a local backend, storing state as a file in the current directory. Backends must be initialized with terraform init. To switch backends, use terraform init -migrate-state.
- Local Backend: Stores state in a local JSON file.
- Remote Backends: Store state remotely, offering features like remote operations, state locking, encryption, and versioning. Examples include AzureRM, Consul, GCS, Amazon S3, and Terraform Cloud.
- Create an S3 bucket in the AWS console.
- Configure Terraform to use the S3 bucket for remote state.
terraform {
backend "s3" {
bucket = "my-bucket"
key = "terraform/state.tfstate"
region = "us-west-2"
access_key = "YOUR_ACCESS_KEY"
secret_key = "YOUR_SECRET_KEY"
}
}- Run
terraform initto initialize the backend.
- Sign up for a Terraform Cloud account.
- Create or join an organization.
- Configure Terraform to use Terraform Cloud.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
cloud {
organization = "my-organization"
workspaces {
name = "production"
}
}
}- Authenticate with Terraform Cloud using
terraform login. - Run
terraform initto initialize the backend.
Modules in Terraform help you reuse code and adhere to the DRY principle ("Don't Repeat Yourself"). They organize, encapsulate, and ensure best practices in configuration.
- Local Modules: Stored locally, referenced with
./or../. - Remote Modules: Stored externally, support versioning, and can be found on the Terraform Registry.
A module is a set of configuration files in a directory. The directory where you run Terraform commands is the root module. Modules imported into the root module are child modules.
module "my_ec2" {
source = "../modules/ec2"
ami_id = var.ami_id
instance_type = var.instance_type
servers = var.servers
}It's best to build infrastructure as reusable modules and share them with your team. After modifying modules, run terraform init to update them.
Terraform logging is controlled by the TF_LOG environment variable, which can be set to TRACE, DEBUG, INFO, WARN, or ERROR. Logs can be saved to a file using TF_LOG_PATH.
Example:
export TF_LOG_PATH=terraform.log
terraform applySeparate logs can be generated for the core application and providers using TF_LOG_CORE and TF_LOG_PROVIDER.
