날짜: 2024-12-13
Terraform은 HashiCorp에서 개발한 Infrastructure as Code (IaC) 도구로, 클라우드 인프라를 코드로 정의하고 관리할 수 있게 해줍니다. Terraform은 다양한 클라우드 제공자(AWS, Azure, GCP 등) 및 온프레미스 환경을 지원하며, 인프라 리소스를 선언적으로 정의하고 이를 자동으로 생성, 수정, 삭제합니다.
주요 용도:
terraform/
ㄴ main.tf = 리소스가 선언되는 메인 파일
ㄴ outputs.tf = 출력 확인용 파일
ㄴ variables.tf = main.tf 에서 var 로 참조할 변수선언 (default 지정)
ㄴ terraform.tfvars = variables.tf 에서 선언한 변수의 값을 지정 (default 덮어씀)
# Terraform 상태 파일 및 백업파일
*.swp
*.tfstate
*.tfstate.*
*.backup
.terraform/
.terraform.lock.hcl
# 기본 명령어
terraform --version
terraform init # 프로젝트 초기화
terraform plan # 실행계획 확인
terraform apply # 변경사항 반영 (저장)
# 응용 명령어
terraform output # 테라폼 outputs.tf 출력
terraform refresh # AWS 실제 상태와 동기화
terraform show # 테라폼과 실제 상태간의 불일치 확인
TF_LOG=DEBUG terraform plan # 실행계획 확인 (상세)
terraform destroy # 테라폼으로 생성한 모든 리소스 삭제
# 고급 명령어
# 이미 원격에 만들어져있는 공용 리소스를 테라폼 관리 대상으로 import
terraform import aws_ecr_repository.ecr baro-api-v2-celery
terraform import aws_iam_instance_profile.ecs baro-api-v2-celery-ecs-instance-profile
# 테라폼에서 생성한 리소스의 수동관리
terraform state rm aws_instance.ecs_instance # 연결 해제 (AWS 콘솔에서 수동 삭제)
terraform import aws_instance.ecs_instance i-0c1e69863aa1bc0f1 # 임의 연결
# 특정 리소스만 제거
terraform destroy \
-target=aws_instance.ecs_instance \
-target=aws_ecs_service.ecs_service \
-target=aws_ecs_task_definition.ecs_task
.tfstate
파일에 저장합니다. 이 파일은 민감한 정보를 포함할 수 있으므로 버전 관리 시스템에 업로드하지 말고, S3 및 DynamoDB를 사용해 원격 관리하세요.terraform lock
설정을 사용하세요.terraform plan
으로 항상 변경 내용을 확인하세요.terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">= 1.5.0"
}
Terraform 모듈은 반복적으로 사용하는 코드 블록을 재사용 가능하게 만들어 인프라 관리의 효율성을 높여줍니다.
my-module/
├── main.tf # 리소스 정의
├── variables.tf # 입력 변수 정의
├── outputs.tf # 출력 변수 정의
main.tf
예:
resource "aws_instance" "example" {
ami = var.ami
instance_type = var.instance_type
tags = {
Name = var.name
}
}
variables.tf
예:
variable "ami" {
description = "AMI ID"
type = string
}
variable "instance_type" {
description = "Instance type"
type = string
default = "t2.micro"
}
variable "name" {
description = "Instance name"
type = string
}
outputs.tf
예:
output "instance_id" {
value = aws_instance.example.id
}
module "example_instance" {
source = "./my-module" # 모듈 경로 (로컬)
ami = "ami-12345678"
instance_type = "t2.micro"
name = "ExampleInstance"
}
source
값은 로컬 디렉토리, Git URL, Terraform Registry 등에서 가져올 수 있습니다.
terraform apply
를 실행해야 합니다.Terraform은 .tfstate
파일을 원격으로 관리하여 상태 충돌 방지 및 협업 효율성을 높일 수 있습니다.
.tfstate
파일 저장에 사용, DynamoDB는 상태 잠금(lock)에 사용.
aws s3api create-bucket --bucket terraform-state-bucket --region ap-northeast-2
aws dynamodb create-table \
--table-name terraform-lock-table \
--attribute-definitions AttributeName=LockID,AttributeType=S \
--key-schema AttributeName=LockID,KeyType=HASH \
--billing-mode PAY_PER_REQUEST
backend
블록을 사용해 원격 상태를 설정합니다.
terraform {
backend "s3" {
bucket = "terraform-state-bucket"
key = "dev/terraform.tfstate" # 환경별로 경로 구분
region = "ap-northeast-2"
dynamodb_table = "terraform-lock-table"
encrypt = true
}
}
terraform init
을 실행합니다.
terraform init
dev
, stage
, prod
환경별로 S3 키를 다르게 설정하여 충돌 방지.
key = "${terraform.workspace}/terraform.tfstate"
terraform workspace new dev
terraform workspace select dev