1. Infrastructure as Code(IaC)
Infrastructure as Code는 인프라를 코드로 관리하고 Provisioning하는 방법론이다.
서버, 네트워크, DB 등의 인프라 Resource 를 AWS, Azure 등의 Web GUI가 아닌, Code로 정의하고 관리함으로써 일관성과 효울성을 높일 수 있다.
특징
- 자동화 : 반복적인 인프라 설정 작업을 자동화하여 시간과 비용 절감 가능
- 일관성 : 동일한 코드로 동일한 환경을 언제든지 재현할 수 있어 환경 간 불일치 문제를 최소화 가능
- 버전 관리 : 코드 형태로 인프라를 관리하기 때문에 Git과 같은 버전 관리 시스템을 통해 변경 이력을 추적하고 복구할 수 있다.
- 협업 용이 : 개발자와 DevOps가 동일한 코드를 기반으로 협업할 수 있다.
주요 장점
- 신속한 배포 : 새로운 인프라 환경을 쉽고 빠르게 구축할 수 있어 개발 속도를 향상 시킬 수 있다.
- 오류 감소 : 자동화 된 스크립트를 사용하여 수동 설정에서 발생할 수 있는 실수를 줄일 수 있다.
- 확장성 : 대규모 인프라를 효율적으로 관리하고 확장할 수 있다.
- 비용 효율성 : 리소스 사용을 최적화하고 불필요한 비용을 줄일 수 있다.
- 테스트 가능성 : 인프라 변경사항을 미리 테스트하고 검증할 수 있어 안정성을 높일 수 있다.
IaC Tool
- HasiCorp Terraform
- AWS CloudFormation
- Azure Resource Manager
- Google Cloud Deplyment Manager
- Pulumi
2. Terraform
Terraform은 Hasicorp에서 개발한 Open Source IaC 도구로, 다양한 클라우드 플랫폼과 On-Premise 환경에서 인프라를 코드로 관리할 수 있다.
장점
- 플랜 기능 : 'terraform plan' 명령어를 통해 변경 사항을 미리 확인하고 검토할 수 있다.
- 상태 관리 : 현재 인프라 상태를 관리하여 변경 사항을 추적하고 일관성을 유지한다.
- 멀티 클라우드 지원 : AWS, Azure, GCP 등 다양한 클라우드 제공자를 지원하여 멀티 클라우드 환경에서도 사용 가능하다.
- 재사용성과 모듈화 : 코드의 모듈화를 통해 재사용성을 높이고 복잡한 인프라를 간단하게 관리할 수 있다.
3. Terraform 활용
멀티 클라우드 배포 예시
# AWS에 EC2 인스턴스 생성
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
# Google Cloud에 Compute Instance 생성
provider "google" {
project = "my-gcp-project"
region = "us-central1"
}
resource "google_compute_instance" "default" {
name = "terraform-instance"
machine_type = "f1-micro"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
access_config {}
}
}
AWS Infra 구축
- 사전 준비 사항 : AWS 계정, AWS CLI 설정, Terraform 설치
단계 1) 디렉토리 생성 및 초기화
mkdir terraform-aws-demo
cd terraform-aws-demo
terraform init
단계 2) Cloud Provider 설정
# main.tf
provider "aws" {
region = "us-west-2"
}
단계 3) VPC 생성
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "demo-vpc"
}
}
단계 4) 서브넷 생성
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
map_public_ip_on_launch = true
tags = {
Name = "demo-public-subnet"
}
}
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
availability_zone = "us-west-2a"
tags = {
Name = "demo-private-subnet"
}
}
단계 5) 인터넷 게이트 웨이 및 라우팅 테이블 생성
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "demo-igw"
}
}
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "demo-public-rt"
}
}
resource "aws_route_table_association" "public_rt_assoc" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_rt.id
}
단계 6) NAT 게이트웨이 및 프라이빗 서브넷 라우팅 테이블 생성
resource "aws_eip" "nat_eip" {
vpc = true
}
resource "aws_nat_gateway" "nat" {
allocation_id = aws_eip.nat_eip.id
subnet_id = aws_subnet.public_subnet.id
tags = {
Name = "demo-nat-gateway"
}
}
resource "aws_route_table" "private_rt" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat.id
}
tags = {
Name = "demo-private-rt"
}
}
resource "aws_route_table_association" "private_rt_assoc" {
subnet_id = aws_subnet.private_subnet.id
route_table_id = aws_route_table.private_rt.id
}
단계 7) 변경 사항 검토 및 생성
terraform plan
terraform apply
단계 8) 인프라 삭제
terraform destroy
주요 포인트
- 재사용성 : 동일한 코드에서 약간 수정하여 다른 환경이나 리전에 적용할 수 있다.
- 일관성 : 코드로 정의된 인프라는 항상 동일한 방식으로 배포된다.
- 가독성 : 선언적인 구문을 사용하여 인프라의 구조와 의도를 쉽게 파악할 수 있다.
4. 기타 활용 사례
- 자동 스케일링 및 로드 밸런싱 : Terraform을 사용하여 자동 스케일링 그룹과 로드 밸런서를 설정하여 트래픽 변화에 유연하게 대응 가능
- 컨테이너 오케스트레이션 : Kubernetes 클러스터를 Terraform으로 프로비저닝하고 관리하여 컨테이너화된 애플리케이션을 효율적으로 배포할 수 있다.
- CI/CD 파이프라인 : Terraform을 CI/CD 파이프라인에 통합하여 코드 변경사항과 함께 인프라 변경 사항을 자동으로 배포하고 관리할 수 있다.
- 멀티 클라우드 및 하이브리드 클라우드 관리 : 여러 클라우드 제공자와 온프레미스 환경을 동시에 관리하여 유연하고 탄력적인 인프라를 구축할 수 있다.
참고 자료
1. https://developer.hashicorp.com/terraform/intro/use-cases#multi-cloud-deployment
Use Cases | Terraform | HashiCorp Developer
Learn how Terraform enables multi-cloud deployments, application management, policy compliance, and self-service infrastructure.
developer.hashicorp.com
2. https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html
Environment variables to configure the AWS CLI - AWS Command Line Interface
This environment variable only applies to an assumed role with web identity provider it does not apply to the general assume role provider configuration.
docs.aws.amazon.com
3. https://developer.hashicorp.com/terraform/tutorials/aws-get-started/infrastructure-as-code
What is Infrastructure as Code with Terraform? | Terraform | HashiCorp Developer
Learn how infrastructure as code lets you safely build, change, and manage infrastructure. Try Terraform.
developer.hashicorp.com
4. https://www.hashicorp.com/blog/infrastructure-as-code-in-a-private-or-public-cloud
Infrastructure as Code in a Private or Public Cloud
Successfully managing the lifecycle of infrastructure is hard, and the impact of poor management decisions can be significant, ranging from financial and reputational losses to even loss of life when considering government and military dependencies on infr
www.hashicorp.com