Terraform
Basics
Command |
Description |
terraform init |
initialize a new Terraform configuration |
terraform plan |
preview changes to infrastructure before applying |
terraform apply |
apply changes to infrastructure |
terraform destroy |
destroy the infrastructure created by Terraform |
terraform state |
view and manage the Terraform state file |
terraform output |
view the output values of Terraform resources |
terraform import |
import existing infrastructure into Terraform |
Configuration
Command |
Description |
provider |
define the provider (e.g. AWS, Google Cloud) for the Terraform configuration |
resource |
define a resource to be managed by Terraform (e.g. EC2 instance, Cloud SQL database) |
data |
define data sources (e.g. AMIs, VPCs) that can be used in the Terraform configuration |
module |
define a reusable Terraform module to be used in the configuration |
variable |
define variables to be used in the Terraform configuration |
output |
define output values to be shown after running Terraform |
State
Command |
Description |
terraform state list |
list all resources in the Terraform state file |
terraform state show |
show details about a resource in the Terraform state file |
terraform state mv |
move a resource in the Terraform state file |
terraform state rm |
remove a resource from the Terraform state file |
Modules
Command |
Description |
terraform get |
download any modules required for the configuration |
terraform init -upgrade |
upgrade any modules to the latest version |
terraform module list |
list all modules used in the Terraform configuration |
Workspaces
Command |
Description |
terraform workspace new |
create a new workspace |
terraform workspace select |
switch to a different workspace |
terraform workspace list |
list all workspaces |
Syntax
The most commonly used Terraform blocks are:
- Provider Block: Specifies the cloud provider and its configuration.
- Resource Block: Defines the infrastructure resources to be managed, such as instances, networks, or databases.
- Variable Block: Declares input variables that can be customized when running Terraform.
- Output Block: Displays the values of certain resources after Terraform applies the configuration.
- Data Block: Retrieves and uses information from external data sources.
- Module Block: Calls reusable modules that encapsulate a set of resources and configurations.
Provider block
provider "aws" {
region = "us-west-2"
}
Resource block
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
Variable block
variable "region" {
type = string
default = "us-west-2"
}
Output block
output "instance_ip" {
value = aws_instance.example.private_ip
description = "Private IP address of the instance"
}
Data block
data "aws_subnet" "example" {
id = "subnet-12345678"
}
Module block
module "vpc" {
source = "./modules/vpc"
vpc_cidr_block = var.vpc_cidr_block
subnets = var.subnets
}
Directory Structure
├── main.tf
├── variables.tf
├── outputs.tf
├── terraform.tfvars
├── modules/
│ └── <module_name>/
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
├── environments/
│ ├── dev/
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ └── outputs.tf
│ ├── prod/
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ └── outputs.tf
│ └── staging/
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
├── .terraform/
└── terraform.tfstate
main.tf
: Contains the main Terraform configuration for your project, including resource blocks and provider configuration.
variables.tf
: Defines input variables used in the main configuration, allowing customization and flexibility.
outputs.tf
: Declares output values that you want to display or use in other parts of your infrastructure.
terraform.tfvars
: Contains variable values specific to your project. (It's recommended to keep this file out of version control and use different tfvars files for each environment.)
modules/
: Directory containing reusable modules, each with its own set of .tf files for configuration.
environments/
: Directory for organizing environment-specific configurations. Each subdirectory represents a different environment (e.g., dev, prod, staging).
.terraform/
: Directory where Terraform stores its local state and provider plugins.
terraform.tfstate
: File that captures the state of your infrastructure. (It's recommended to use remote state storage for team collaboration and state management.)
Naming Convention
- Use descriptive names: Choose names that accurately describe the purpose or function of each resource, module, or variable.
- Be consistent: Maintain a consistent naming convention throughout your project to ensure clarity and readability.
- Avoid special characters: Stick to alphanumeric characters and underscores, as some cloud providers may have restrictions on naming.