인프라스트럭처 as 코드(IaC)를 위한 포괄적인 Terraform 명령어와 워크플로우, 리소스 관리, 상태 작업, 멀티 클라우드 배포 포함.
설치 및 설정
| 명령어 | 설명 |
|---|
terraform version | Terraform 버전 표시 |
terraform -help | 도움말 정보 표시 |
terraform -help plan | 특정 명령어에 대한 도움말 표시 |
핵심 워크플로우
기본 작업
| 명령어 | 설명 |
|---|
terraform init | 작업 디렉토리 초기화 |
terraform plan | 실행 계획 생성 |
terraform apply | 변경 사항 적용 |
terraform destroy | 인프라 파괴 |
terraform validate | 구성 확인 |
terraform fmt | 구성 파일 형식 지정 |
고급 계획
| 명령어 | 설명 |
|---|
terraform plan -out=tfplan | 파일에 계획 저장 |
terraform apply tfplan | 저장된 계획 적용 |
terraform plan -target=resource.name | 특정 리소스 계획 |
terraform plan -var="key=value" | 변수로 계획하기 |
terraform plan -var-file="vars.tfvars" | 변수 파일로 계획하기 |
상태 관리
상태 작업
| 명령어 | 설명 |
|---|
terraform state list | 상태의 리소스 나열 |
terraform state show resource.name | 리소스 세부 정보 표시 |
terraform state mv old_name new_name | 상태에서 리소스 이동 |
terraform state rm resource.name | 상태에서 리소스 제거 |
terraform state pull | 원격 상태 다운로드 |
terraform state push | 원격에 상태 업로드 |
상태 백업 및 복구
| 명령어 | 설명 |
|---|
terraform state backup | 상태 백업 생성 |
terraform force-unlock LOCK_ID | 강제 잠금 해제 상태 |
terraform refresh | 실제 리소스로 상태 업데이트 |
워크스페이스
| 명령어 | 설명 |
|---|
terraform workspace list | 작업 공간 목록 |
terraform workspace new dev | 새 워크스페이스 생성 |
terraform workspace select dev | 작업 공간으로 전환 |
terraform workspace delete dev | 작업 공간 삭제 |
terraform workspace show | 현재 작업 공간 표시 |
가져오기 및 출력
리소스 가져오기
| 명령어 | 설명 |
|---|
terraform import resource.name id | 기존 리소스 가져오기 |
terraform import aws_instance.example i-1234567890abcdef0 | AWS 인스턴스 가져오기 |
출력
| 명령어 | 설명 |
|---|
terraform output | 모든 출력 표시 |
terraform output instance_ip | 특정 출력 표시 |
terraform output -json | JSON으로 출력 표시 |
구성 예시
기본 AWS EC2 인스턴스
provider "aws" \\\\{
region = "us-west-2"
\\\\}
resource "aws_instance" "example" \\\\{
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
tags = \\\\{
Name = "example-instance"
\\\\}
\\\\}
output "instance_ip" \\\\{
value = aws_instance.example.public_ip
\\\\}
변수
variable "instance_type" \\\\{
description = "EC2 instance type"
type = string
default = "t2.micro"
\\\\}
variable "environment" \\\\{
description = "Environment name"
type = string
\\\\}
데이터 소스
data "aws_ami" "ubuntu" \\\\{
most_recent = true
owners = ["099720109477"] # Canonical
filter \\\\{
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
\\\\}
\\\\}
모듈
module "vpc" \\\\{
source = "terraform-aws-modules/vpc/aws"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["us-west-2a", "us-west-2b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_nat_gateway = true
enable_vpn_gateway = true
\\\\}
원격 상태 구성
S3 백엔드
terraform \\\\{
backend "s3" \\\\{
bucket = "my-terraform-state"
key = "state/terraform.tfstate"
region = "us-west-2"
\\\\}
\\\\}
Azure 백엔드
terraform \\\\{
backend "azurerm" \\\\{
resource_group_name = "tfstate"
storage_account_name = "tfstate"
container_name = "tfstate"
key = "prod.terraform.tfstate"
\\\\}
\\\\}
Google Cloud 백엔드
terraform \\\\{
backend "gcs" \\\\{
bucket = "tf-state-bucket"
prefix = "terraform/state"
\\\\}
\\\\}
프로바이더 구성
AWS 프로바이더
provider "aws" \\\\{
region = "us-west-2"
profile = "default"
default_tags \\\\{
tags = \\\\{
Environment = "production"
Project = "my-project"
\\\\}
\\\\}
\\\\}
Azure 프로바이더
provider "azurerm" \\\\{
features \\\\{\\\\}
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
\\\\}
Google Cloud 프로바이더
provider "google" \\\\{
project = "my-project-id"
region = "us-central1"
zone = "us-central1-c"
\\\\}
고급 기능
조건부 리소스
Would you like me to continue with the remaining translations or provide placeholders for the empty sections?```hcl
resource “aws_instance” “example” \\{
count = var.create_instance ? 1 : 0
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
\\}
**상태 보안**: 암호화된 원격 상태 사용hcl
resource “aws_instance” “example” \\{
for_each = toset(var.instance_names)
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
tags = \\{
Name = each.key
\\}
\\}
**비밀 관리**: 외부 비밀 저장소 사용hcl
resource “aws_security_group” “example” \\{
name = “example”
dynamic “ingress” \\{
for_each = var.ingress_rules
content \\{
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr_blocks
\\}
\\}
\\}
| 명령어 | 설명 |
|---------|-------------|
| `terraform validate` | 구문 유효성 검사 |
| `terraform validate -json` | JSON 출력으로 검증하기 |**코드 리뷰**: 모든 인프라 변경 사항 검토
| 명령어 | 설명 |
|---------|-------------|
| `terraform plan -detailed-exitcode` | 상세한 종료 코드로 계획하기 |
| `terraform show` | 현재 상태 표시 |
| `terraform show -json` | JSON으로 상태 표시하기 |**스캐닝**: 보안 스캐닝 도구 사용
### Performance Optimization
| 명령어 | 설명 |
|---------|-------------|
| `tflint` | Terraform 린터 |
| `terraform-docs` | 문서 생성 |
| `checkov -f main.tf` | 보안 스캐닝 |
| `tfsec .` | 보안 분석 |**병렬성**: 대규모 배포를 위해
| 명령어 | 설명 |
|---------|-------------|
| `TF_LOG=DEBUG terraform plan` | 디버그 로깅 활성화 |
| `TF_LOG=TRACE terraform apply` | 추적 로깅 활성화 |
| `TF_LOG_PATH=terraform.log terraform plan` | 파일에 로그 기록 |플래그 사용
| 명령어 | 설명 |
|---------|-------------|
| `terraform refresh` | 현실과 상태 동기화 |
| `terraform taint resource.name` | 여가를 위한 자원 표시 |
| `terraform untaint resource.name` | 리소스에서 taint 제거 |**타겟팅**: 특정 리소스를 위해 ```
project/
├── main.tf # Main configuration
├── variables.tf # Variable definitions
├── outputs.tf # Output definitions
├── versions.tf # Provider versions
├── terraform.tfvars # Variable values
└── modules/
└── vpc/
├── main.tf
├── variables.tf
└── outputs.tf
```사용```hcl
terraform \\\\{
required_version = ">= 1.0"
required_providers \\\\{
aws = \\\\{
source = "hashicorp/aws"
version = "~> 5.0"
\\\\}
\\\\}
\\\\}
```**상태 분할**: 대규모 상태를 더 작은 상태로 분할```hcl
resource "aws_instance" "web_server" \\\\{
# Use descriptive names
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
tags = \\\\{
Name = "$\\\\{var.project_name\\\\}-web-$\\\\{var.environment\\\\}"
Environment = var.environment
Project = var.project_name
\\\\}
\\\\}
```**모듈 설계**: 재사용 가능한 모듈 설계
Would you like me to clarify or complete the other sections? Some sections appear to be missing text or have placeholders.`-parallelism` flag for large deployments
2. **Targeting**: Use `-target` for specific resources
3. **State Splitting**: Split large states into smaller ones
4. **Module Design**: Design reusable modules