Dev./Terraform

Terraform: Modules

Ivan'show 2023. 9. 5.
728x90
반응형

Staging code refactoring

기존에는 코드를 하나의 main.tf 파일에 담아서 동작시켰지만 실제로는 기능별로 분리해서 관리하는게 가독성이 좋다.

hasicorp 의 제공하는 문서에 의하면,

The standard module structure is a file and directory layout we recommend for reusable modules distributed in separate repositories. Terraform tooling is built to understand the standard module structure and use that structure to generate documentation, index modules for the module registry, and more.

해석하자면, 표준 모듈 구조라는게 있는데 이 모듈 구조는 추천하는 파일이나 디렉토리의 레이아웃으로 재사용 가능한 모듈들을 구분지어 둔 것을 말한다.

[main.tf](<http://main.tf>), [variables.tf](<http://variables.tf>), [outputs.tf](<http://outputs.tf>). These are the recommended filenames for a minimal module, even if they're empty. main.tf should be the primary entrypoint. For a simple module, this may be where all the resources are created. For a complex module, resource creation may be split into multiple files but any nested module calls should be in the main file. variables.tf and outputs.tf should contain the declarations for variables and outputs, respectively.

해서, main.tf, variables.tf, outputs.tf 등으로 레이아웃이 되는데, main.tf 는 리소스를, variables.tf 는 변수 선언을, outputs.tf 는 출력값을 포함하게 된다.

복잡한 구조의 모듈은 main.tf 파일을 여러 파일로 분리할 수 있다.

$ tree minimal-module/
.
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
$ tree complete-module/
.
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
├── ...
├── modules/
│   ├── nestedA/
│   │   ├── README.md
│   │   ├── variables.tf
│   │   ├── main.tf
│   │   ├── outputs.tf
│   ├── nestedB/
│   ├── .../
├── examples/
│   ├── exampleA/
│   │   ├── main.tf
│   ├── exampleB/
│   ├── .../

위와 같은 구조를 적용하기 전, main 파일에서 코드를 분리하는 과정부터 진행해 보자.

기존에 작성했던 main.tf 파일을 여러 파일로 구분짓게 되면,

목적별, 기능별 등으로 구분지어서 코드를 정리할 수 있다.

Production Terraform

그 다음 분리 작업은 staging 과 production 이다.

productions 폴더에 staging 에 작성했던 것 처럼 새롭게 구분지어서 코드를 생성해보자. 이때 staging 과 prod 의 차이에 대해서 유의하면서 작성해준다.

# terrform.tftpl
...
# Django setup
DJANGO_SETTINGS_MODULE="lion_app.settings.prod"
...

 

 

 

728x90
반응형

'Dev. > Terraform' 카테고리의 다른 글

Terraform: Status 가 꼬이는 현상  (0) 2023.09.07
Terraform: Separating  (0) 2023.09.06
Terraform: LoadBalancer  (0) 2023.09.05
Terraform: AWS - VPC 생성  (0) 2023.09.04
Terrform: NCP 서버 생성  (0) 2023.09.02

댓글