terraform init | 작업 디렉토리 초기화 |
terraform plan | 실행 계획 표시 |
terraform apply | 변경 사항 적용 |
terraform apply -auto-approve | 확인 없이 적용 |
terraform destroy | 인프라 삭제 |
terraform validate | 구성 검증 |
terraform fmt | 코드 포맷 |
terraform fmt -recursive | 재귀적으로 모든 파일 포맷 |
terraform state list | 상태의 리소스 목록 |
terraform state show aws_instance.example | 리소스 상세 정보 |
terraform state mv old_name new_name | 리소스 이동/이름 변경 |
terraform state rm aws_instance.example | 상태에서 제거 |
terraform state pull | 원격 상태 가져오기 |
terraform state push | 원격에 상태 푸시 |
terraform refresh | 상태 새로고침 |
terraform output | 출력 표시 |
terraform output -json | JSON으로 출력 |
terraform console | 대화형 콘솔 |
terraform graph | 의존성 그래프 생성 |
terraform import aws_instance.example i-1234 | 기존 리소스 가져오기 |
terraform taint aws_instance.example | 재생성 표시 |
terraform untaint aws_instance.example | 표시 제거 |
terraform workspace list | 워크스페이스 목록 |
terraform workspace new dev | 워크스페이스 생성 |
terraform workspace select prod | 워크스페이스 전환 |
provider "aws" {
region = "us-west-2"
} resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
} data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-*"]
}
owners = ["099720109477"]
} resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
} variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
} variable "name" {
type = string
}
variable "count" {
type = number
}
variable "enabled" {
type = bool
}
variable "tags" {
type = map(string)
}
variable "subnets" {
type = list(string)
} resource "aws_instance" "web" {
instance_type = var.instance_type
tags = var.tags
} # terraform.tfvars
instance_type = "t3.medium"
environment = "production"
tags = {
Project = "web-app"
Team = "devops"
} variable "db_password" {
type = string
sensitive = true
} output "instance_ip" {
description = "Public IP of the instance"
value = aws_instance.web.public_ip
} output "db_password" {
value = random_password.db.result
sensitive = true
} name = "web-${var.environment}" instance_type = var.environment == "prod" ? "t3.large" : "t3.micro" upper_names = [for name in var.names : upper(name)] instance_ids = { for k, v in aws_instance.web : k => v.id } resource "aws_instance" "web" {
count = 3
tags = {
Name = "web-${count.index}"
}
} resource "aws_instance" "web" {
for_each = var.instances
ami = each.value.ami
instance_type = each.value.type
tags = {
Name = each.key
}
} resource "aws_iam_user" "users" {
for_each = toset(var.user_names)
name = each.value
} lower("HELLO") # "hello"
upper("hello") # "HELLO"
trim(" hello ") # "hello"
split(",", "a,b,c") # ["a", "b", "c"]
join(",", ["a", "b"]) # "a,b" length(["a", "b"]) # 2
concat([1,2], [3,4]) # [1,2,3,4]
merge(map1, map2) # Merge maps
lookup(map, key, default)
element(list, index)
contains(list, value) tostring(42) # "42"
tonumber("42") # 42
tolist(set) # Convert to list
tomap(object) # Convert to map file("script.sh") # Read file content
fileexists("path") # Check if exists
templatefile("tpl.sh", { name = "foo" }) module "vpc" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
name = "my-vpc"
} module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
} module "vpc" {
source = "github.com/org/repo//modules/vpc?ref=v1.0.0"
} resource "aws_instance" "web" {
subnet_id = module.vpc.public_subnet_ids[0]
} modules/vpc/
├── main.tf # Resources
├── variables.tf # Input variables
├── outputs.tf # Output values
└── versions.tf # Provider versions # modules/vpc/outputs.tf
output "vpc_id" {
value = aws_vpc.main.id
}
output "subnet_ids" {
value = aws_subnet.main[*].id
} terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-west-2"
encrypt = true
dynamodb_table = "terraform-locks"
}
} data "terraform_remote_state" "vpc" {
backend = "s3"
config = {
bucket = "my-terraform-state"
key = "vpc/terraform.tfstate"
region = "us-west-2"
}
}
# Use: data.terraform_remote_state.vpc.outputs.vpc_id resource "aws_instance" "web" {
# ...
lifecycle {
create_before_destroy = true
prevent_destroy = true
ignore_changes = [tags]
}
} resource "aws_instance" "web" {
# ...
depends_on = [
aws_iam_role_policy.example
]
}