Loading...
Loading...
Terraform Expert Engineer Skill - Comprehensive guide for Infrastructure as Code, resource management, module design, and state management Use when: - Running terraform init, plan, apply, destroy - Managing Terraform state (import, move, rm) - Designing modules or configuring backends - Working with HCP Terraform / Terraform Cloud
npx skill4agent add i9wa4/dotfiles terraform# Initialize workspace (download providers)
terraform init
# Initialize with backend config
terraform init -backend-config="bucket=my-terraform-state"
# Check execution plan
terraform plan
# Save execution plan to file
terraform plan -out=tfplan
# Plan specific resource only
terraform plan -target=aws_instance.exampleterraform plan# Apply changes
terraform apply
# Apply saved plan
terraform apply tfplan
# Auto-approve apply (for CI/CD)
terraform apply -auto-approve
# Destroy resources
terraform destroy
# Destroy specific resource only
terraform destroy -target=aws_instance.example# Check state
terraform state list
# Show resource details
terraform state show aws_instance.example
# Move resource (for refactoring)
terraform state mv aws_instance.old aws_instance.new
# Import existing resource
terraform import aws_instance.example i-1234567890abcdef0
# Remove resource from state (keeps actual resource)
terraform state rm aws_instance.example# Validate configuration
terraform validate
# Format
terraform fmt
# Format recursively
terraform fmt -recursive
# Check outputs
terraform output
# Output in JSON format
terraform output -json
# Interactive console (for testing expressions)
terraform console
# Lock providers
terraform providers lock -platform=linux_amd64 -platform=darwin_amd64resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
}depends_oncountfor_eachproviderlifecycleresource "aws_instance" "example" {
# ...
lifecycle {
create_before_destroy = true # Create new first on replacement
prevent_destroy = true # Prevent deletion
ignore_changes = [tags] # Attributes to ignore changes
replace_triggered_by = [null_resource.trigger.id]
}
}module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
}./modules/vpchashicorp/consul/awsgithub.com/hashicorp/examples3::https://s3-eu-west-1.amazonaws.com/bucket/module.zipmain.tfvariables.tfoutputs.tfterraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "ap-northeast-1"
encrypt = true
dynamodb_table = "terraform-locks"
}
}terraform statevariable "instance_type" {
type = string
description = "EC2 instance type"
default = "t2.micro"
validation {
condition = contains(["t2.micro", "t2.small", "t2.medium"], var.instance_type)
error_message = "Please specify an allowed instance type"
}
}-var-var-file*.auto.tfvarsterraform.tfvars.jsonterraform.tfvarsTF_VAR_*variable "db_password" {
type = string
sensitive = true # Mask in output
}
output "connection_string" {
value = "postgres://user:${var.db_password}@host/db"
sensitive = true # Output contains sensitive data
}terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "ap-northeast-1"
}provider "aws" {
alias = "us_east"
region = "us-east-1"
}
provider "aws" {
alias = "ap_northeast"
region = "ap-northeast-1"
}
resource "aws_instance" "us" {
provider = aws.us_east
# ...
}# String operations
join("-", ["foo", "bar"]) # "foo-bar"
split(",", "a,b,c") # ["a", "b", "c"]
format("Hello, %s!", "World") # "Hello, World!"
# Collection operations
length(["a", "b", "c"]) # 3
lookup(map, key, default) # Get value from map
merge(map1, map2) # Merge maps
flatten([["a"], ["b", "c"]]) # ["a", "b", "c"]
# Type conversions
tostring(123) # "123"
tolist(set) # Set to list
tomap(object) # Object to map
# Conditional expressions
coalesce("", "default") # "default" (first non-empty value)
try(expression, fallback) # Fallback on errorterraform {
cloud {
organization = "my-org"
workspaces {
name = "my-workspace"
}
}
}.terraform/.gitignore*.tfvars.gitignore