728x90
반응형
1. 테라폼이란?
1.1 정의
테라폼(Terraform)은 HashiCorp에서 개발한 인프라스트럭처를 코드로 관리하는 도구(IaC: Infrastructure as Code)입니다. 클라우드 인프라(예: AWS, GCP, Azure 등)를 코드로 정의하고 자동으로 생성, 변경, 삭제할 수 있게 해줍니다.
1.2 주요 특징
- 선언형 문법: 어떤 상태가 되어야 하는지만 기술하면, 테라폼이 그 상태를 만들기 위한 작업을 자동으로 수행합니다.
- 멀티 클라우드 지원: AWS, GCP, Azure, Oracle Cloud, Alibaba Cloud 등 다양한 클라우드를 동시에 다룰 수 있습니다.
- 모듈화와 재사용: 하나의 인프라 구성을 모듈화하여 여러 프로젝트에서 재사용할 수 있습니다.
- 상태 관리: 현재 인프라 상태를 .tfstate 파일로 관리하여, 변경사항을 비교(diff)하고 적용합니다.
2. 테라폼 설치 방법
# macOS
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
# Ubuntu/Linux
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update
sudo apt install terraform
# 설치 확인
terraform -v
3. 테라폼 기본 개념 정리
개념 설명
Provider | 인프라 제공자 (예: AWS, GCP, Azure 등) |
Resource | 생성하고자 하는 리소스 (예: EC2, S3 등) |
Module | 여러 리소스를 묶어 재사용 가능한 구성 요소 |
Variable | 사용자 정의 변수 |
Output | 실행 후 출력값 |
State | 현재 인프라 상태를 저장한 파일 (.tfstate) |
4. 테라폼 프로젝트 구조 예시
my-terraform-project/
├── main.tf
├── variables.tf
├── outputs.tf
├── terraform.tfvars
└── provider.tf
5. AWS EC2 인스턴스 생성 예제
5.1 provider.tf
provider "aws" {
region = var.aws_region
}
5.2 variables.tf
variable "aws_region" {
description = "AWS region to deploy to"
default = "ap-northeast-2"
}
variable "instance_type" {
description = "Type of EC2 instance"
default = "t2.micro"
}
5.3 main.tf
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Ubuntu 20.04 LTS (서울 리전)
instance_type = var.instance_type
tags = {
Name = "TerraformExampleInstance"
}
}
5.4 outputs.tf
output "instance_id" {
value = aws_instance.example.id
}
output "public_ip" {
value = aws_instance.example.public_ip
}
5.5 실행 순서
terraform init # 테라폼 초기화 (플러그인 설치)
terraform plan # 실행 계획 확인
terraform apply # 인프라 생성
terraform destroy # 인프라 제거
6. 테라폼 주요 명령어
명령어 설명
terraform init | 플러그인 다운로드 및 초기화 |
terraform plan | 어떤 리소스를 생성/수정/삭제할지 확인 |
terraform apply | 실제로 인프라 생성 |
terraform destroy | 인프라 제거 |
terraform validate | 문법 검증 |
terraform fmt | 코드 포맷팅 |
terraform taint | 특정 리소스를 강제로 재생성 대상 표시 |
7. 변수와 출력값 정교하게 다루기
variable "tags" {
type = map(string)
default = {
Project = "TerraformIntro"
Owner = "jangbosun"
}
}
resource "aws_s3_bucket" "example" {
bucket = "terraform-bucket-${random_id.bucket_id.hex}"
tags = var.tags
}
output "bucket_name" {
value = aws_s3_bucket.example.id
}
8. 모듈 사용 예제
디렉토리 구조:
.
├── main.tf
├── modules/
│ └── ec2/
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
8.1 modules/ec2/main.tf
resource "aws_instance" "this" {
ami = var.ami
instance_type = var.instance_type
}
8.2 modules/ec2/variables.tf
variable "ami" {}
variable "instance_type" {}
8.3 main.tf (루트)
module "my_ec2" {
source = "./modules/ec2"
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
9. 테라폼 상태(state) 관리 꿀팁
- 기본적으로는 terraform.tfstate 파일로 관리되며, 협업을 위해서는 원격 저장소(backends) 사용 추천
- AWS S3를 백엔드로 사용하는 예:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "dev/terraform.tfstate"
region = "ap-northeast-2"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
10. 마무리: 테라폼의 확장성과 미래
- 테라폼은 DevOps 자동화, 배포 파이프라인, GitOps, 멀티클라우드 전략에 필수 도구로 자리잡고 있습니다.
- HCL(HashiCorp Configuration Language)을 기반으로 하며, JSON도 지원합니다.
- Pulumi, AWS CDK 등 경쟁 도구도 있지만, 선언형이고 직관적인 구조로 인해 여전히 가장 널리 사용되는 IaC 도구 중 하나입니다.
11. 참고 자료
- 공식 홈페이지
- 공식 AWS Provider 문서
- Terraform Best Practices
- GitHub에 있는 공개 모듈 예제들 참고하기
728x90
반응형