Terraform quick reference
Quick reference for commonly used terraform commands and how-tos.
Commonly used terraform commands
terraform init
Initializes a working directory containing Terraform configuration files. This command downloads and installs the necessary provider plugins and sets up the backend for storing the state file.
terraform plan
Generates an execution plan, showing what actions Terraform will take to achieve the desired state of the infrastructure as defined in the configuration files. This is a dry run and does not change any real resources.
terraform apply
Applies the changes required to reach the desired state of the configuration. This command executes the plan generated by terraform plan and makes the necessary changes to the infrastructure.
terraform destroy
Destroys the infrastructure managed by Terraform. This command is used to remove all resources defined in the configuration files.
terraform show
Shows the current state or a saved plan. This command is useful for inspecting the current state of the infrastructure or a previously generated plan.
terraform output
Displays the output values defined in the configuration. This is useful for retrieving information about the deployed infrastructure.
How to define a basic terraform configuration
A basic Terraform configuration file typically consists of several key components: the provider configuration, resource definitions, and optionally, variables and outputs. Below is a simple example of a Terraform configuration file that provisions an AWS EC2 instance.
# Define the provider
provider "aws" {
region = "ap-northest-1a"
}
# Define a variable for the AMI ID
variable "ami_id" {
description = "The AMI ID to use for the instance"
default = "ami-xxx"
}
# Define a variable for the instance type
variable "instance_type" {
description = "The type of instance to use"
default = "t2.micro"
}
# Define the EC2 instance resource
resource "aws_instance" "app_server" {
ami = var.ami_id
instance_type = var.instance_type
tags = {
Name = "ExampleAppServerInstance"
}
}
# Output the public IP of the instance
output "instance_ip" {
description = "The public IP address of the instance"
value = aws_instance.app_server.public_ip
}
How to define a terraform configuration with modules
- structure
.
├── main.tf
└── modules
└── ec2_instance
├── main.tf
├── variables.tf
└── outputs.tf
- main.tf
# Define the provider
provider "aws" {
region = "ap-northeast-1a"
}
# Define a variable for the AMI ID
variable "ami_id" {
description = "The AMI ID to use for the instance"
default = "ami-xxx"
}
# Define a variable for the instance type
variable "instance_type" {
description = "The type of instance to use"
default = "t2.micro"
}
# Call the local module
# The label immediately after the module keyword is a local name,
# which the calling module can use to refer to this instance of the module.
module "app_server" {
source = "./modules/ec2_instance"
instance_type = var.instance_type
ami_id = var.ami_id
}
# Output the public IP of the instance
output "instance_ip" {
description = "The public IP address of the instance"
value = module.app_server.instance_ip
}
- modules/ec2_instance/main.tf
# Define the EC2 instance resource
resource "aws_instance" "this" {
ami = var.ami_id
instance_type = var.instance_type
tags = {
Name = "ExampleAppServerInstance"
}
}
- modules/ec2_instance/variables.tf
# Define a variable for the AMI ID
variable "ami_id" {
description = "The AMI ID to use for the instance"
type = string
}
# Define a variable for the instance type
variable "instance_type" {
description = "The type of instance to use"
type = string
}
- modules/ec2_instance/outputs.tf
# Output the public IP of the instance
output "instance_ip" {
description = "The public IP address of the instance"
value = aws_instance.this.public_ip
}
What is the purpose of terraform block
The terraform block in a Terraform configuration file is used to define settings that configure the behavior of Terraform itself. This block can include various settings such as specifying the required Terraform version, configuring the backend for state storage, defining required providers, and enabling experimental features.
Here is an example that combines several of these settings:
terraform {
required_version = ">= 1.1.0"
backend "s3" {
bucket = "my-terraform-state"
key = "terraform.tfstate"
region = "ap-northest-1a"
}
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.71.0"
}
}
experiments = ["example"]
provider_meta "aws" {
example_key = "example_value"
}
}
References