Terraform

1. 테라폼의 개요

테라폼(Terraform) 은 Infrastructure 자동화 도구로서, 클라우드 서비스 및 온프레미스 환경에서 Infrastructure를 코드로 정의하고 프로비저닝하는 데 사용됩니다.

Infrastructure 코드(Infrastructure as Code, IaC) 개념을 바탕으로 동작하며, 코드 버전 관리 및 협업, Infrastructure 변경사항 추적 등의 장점을 제공합니다. 다양한 클라우드 서비스(예: AWS, Azure, GCP 등) 및 온프레미스 환경에서 동작하며, 클라우드 서비스별로 제공하는 리소스(예: EC2 인스턴스, S3 버킷 등)를 코드로 정의하여 프로비저닝할 수 있습니다.

2. 테라폼 설치 및 설정 (MacOs)

패키지 저장소인 hashicorp tap 설치 후 terraform 설치

1
2
3
brew tap hashicorp/tap

brew install hashicorp/tap/terraform

terraform 작동 확인

1
2
3
4
5
6
7
8
9
10
11
12
13
$ terraform -help
Usage: terraform [global options] <subcommand> [args]

The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.

Main commands:
init Prepare your working directory for other commands
validate Check whether the configuration is valid
plan Show changes required by the current configuration
apply Create or update infrastructure
destroy Destroy previously-created infrastructure

3. Infrastructure 코드 작성

해당 코드를 실행시키기 위해서는 Docker가 실행되어 있어야 합니다.

1
open -a Docker

main.tf 파일 생성

1
2
3
touch main.tf

vim main.tf

nginx docker 컨테이너 생성을 위한 코드 작성 후 저장

image: nginx:latest
internal port: 80
external port: 8000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.13.0"
}
}
}

provider "docker" {}

resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}

resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "nginx"
ports {
internal = 80
external = 8000
}
}

4. 프로비저닝

프로젝트 초기화

1
2
3
4
5
6
$ terraform init
Initializing the backend...

...

Terraform has been successfully initialized!

NGINX 서버 컨테이너 프로비저닝

1
2
3
4
5
6
7
8
9
$ terraform apply
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes


Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

컨테이너 실행 여부 확인

1
2
3
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd2b2665dc90 114aa6a9f203 "/docker-entrypoint.…" 23 seconds ago Up 22 seconds 0.0.0.0:8000->80/tcp nginx

웹 페이지 접속 여부 확인


컨테이너 종료

1
terraform destroy

5. 테라폼 모듈

테라폼에서 모듈은 리소스, 변수, 출력값 등을 포함하며, 다른 모듈에서 재사용할 수 있습니다.

이를 통해 코드 중복을 줄이고, 모듈화된 코드를 관리함으로써 코드를 보다 쉽게 유지보수할 수 있습니다.
모듈은 모듈 디렉토리 안에 .tf 파일들로 구성됩니다. 테라폼은 이러한 .tf 파일들을 읽어서 모듈을 로드하고 사용합니다. 모듈은 모듈 내부에서 정의된 리소스와 데이터를 관리하며, 다른 모듈과 변수 및 출력값을 공유할 수 있습니다.

6. 테라폼 확장성

테라폼은 클라우드 서비스를 비롯한 다양한 Infrastructure를 코드로 정의하여 관리할 수 있는 확장성이 뛰어난 도구입니다.

클라우드 서비스(예: AWS, Azure, GCP)를 비롯한 다양한 Infrastructure를 지원하며, 확장성과 유연성을 제공하기 위해 모듈화된 코드 구조와 플러그인 아키텍처를 사용하고 있습니다.
또한, 테라폼은 Infrastructure 변경사항을 추적하고 코드 버전 관리를 할 수 있는 기능을 제공하여, 대규모 Infrastructure를 효과적으로 관리할 수 있습니다.
이러한 확장성과 유연성을 통해, 테라폼은 다양한 Infrastructure 관리 요구사항에 대응할 수 있으며, 새로운 환경에 대한 대응도 빠르게 이루어질 수 있습니다.




참고자료: ChatGPT, Terraform 공식 사이트
사진 출처: https://www.scalefactory.com/blog/2021/12/22/terraform-v1.1-the-journey-continues/tf11x.png