본문 바로가기

IaC

스터디 5주차 - 모듈 추가 내용

nested module과 child module의 차이?

 

Nested Module

A nested module is a reference to invoke another module from the current module (including from the root module). Nested modules can be located externally and are referred to as "child modules", or embedded inside the current workspace and are referred to as "submodules".
  • 현재 모듈 (루트 모듈 포함) 에서 또 다른 모듈을 호출하는 구조
  • 예를 들어, 1.루트 모듈 내부에서 다른 모듈을 호출하거나 2.child module 내부에서 다른 하위 모듈을 호출하는 경우
  • nested module은 외부에 위치하여 child module로써 참조되거나 현재 작업공간에 내장되어 submodule로써 참조되어질 수 있다.

 

Child Module

  • 루트 모듈에서 직접 호출되는 모듈

 

Nested module은 모듈 안에서 또다른 모듈을 호출하는 계층적 "구조"를 의미하고, child module은 루트 모듈에서 직접 호출되는 "모듈"을 의미한다.

 


모듈 source에 git tag외에 다른 것도 명시가 가능한가?

 

  • 브랜치
    module "vpc" {
      source = "git::https://example.com/vpc.git?depth=1&ref=main"
    }


  • 태그
    module "vpc" {
      source = "git::https://example.com/vpc.git?depth=1&ref=v1.2.0"
    }
  • 커밋 해시
    module "vpc" {
      source = "git::https://example.com/vpc.git?depth=1&ref=3f2e4d6"
    }


  • git에서는 브랜치와 태그 이름이 동일한 경우, 혼동이 발생할 수 있기에 아래와 같은 규칙이 적용된다.
    • 테라폼에선 git 명령어를 사용하므로, 태그를 우선적으로 참조하게 된다.
  • git 명령어를 사용하므로, git이 설치되어 있어야 한다.

 


테라폼 module 관리 패턴

github에 다른 프로젝트 폴더 외 module 폴더를 따로 빼서 버전 및 소스코드 관리하는 것이 일반적일까?

 

 

1. 모듈을 별도의 git 저장소로 관리

  • project-repo : 프로젝트별 terraform 구성
  • vpc-module-repo : VPC 관련 모듈
  • ec2-module-repo : EC2 관련 모듈

  • 독립적인 버전관리 
  • 프로젝트 코드와 모듈 코드의 분리
module "vpc" {
  source = "git::https://github.com/your-org/vpc-module.git?ref=v1.2.0"
  cidr_block = "10.0.0.0/16"
}

 

 

2. 프로젝트 내에서 모듈 디렉토리 관리

  • 작은 프로젝트나 재사용이 필요하지 않은 경우
  • 프로젝트와 모듈을 동일한 저장소에서 관리
  • 간단한 관리
  • 빠른 개발
  • 확장성 부족
  • 버전 관리 어려움 / 모듈의 특정 버전을 사용하는 다른 프로젝트가 있을 경우
project-repo/
├── main.tf
├── variables.tf
├── modules/
│   ├── vpc/
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   ├── ec2/
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf

 

module "vpc" {
  source = "./modules/vpc"
  cidr_block = "10.0.0.0/16"
}

 

3. Terraform registry 혹은 Private registry 사용

  • 모듈의 중앙화
  • 간편한 버전 관리
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "3.5.0"
}

 

 

모듈 관리 예시 - 테라폼 클라우드

https://creboring.net/blog/terraform-cloud-intro/

 

[Terraform] 테라폼 클라우드 도입기

Terraform Cloud를 활용하여 리소스 편하게 관리하기

creboring.net

 

팀과 속해있는 환경에 따라 다르지만, 적은 인원과 리소스를 투입해서 빠르게 사용하기 위해선 Terraform Cloud와 같은 관리형 Module registry를 이용하고, 규모가 크고 많은 인원이 있는 상황에서 구체적인 요구사항이나 관리 전략이 필요하며, git workflow에 초점을 둔다면, git repo를 이용하는 것 같다.