728x90
반응형
- Server 모듈에서 하나의 서버만 만들도록 변경
- 명령어를 이용하여 따로 따로 실행 가능하게 하기 위함
- Modules 안 server 폴더 안쪽에 하위 폴더를 만들면 테라폼에서 접근을 하지 않음
- 이 작업의 접근 방식은 server 에 있는 main 을 분할은 하되, staging 에서 module 접근자로 해결
previous code
staging/main.tf
# servers
module "servers" {
source = "../modules/server"
# going to variabels.tf
env = local.env
region = var.region
site = var.site
support_vpc = var.support_vpc
username = var.username
password = var.password
NCP_ACCESS_KEY = var.NCP_ACCESS_KEY
NCP_SECRET_KEY = var.NCP_SECRET_KEY
NCP_CONTAINER_REGISTRY = var.NCP_CONTAINER_REGISTRY
IMAGE_TAG = var.IMAGE_TAG
db = var.db
db_user = var.db_user
db_password = var.db_password
db_port = var.db_port
DJANGO_SETTINGS_MODULE = var.DJANGO_SETTINGS_MODULE
DJANGO_SECRET_KEY = var.DJANGO_SECRET_KEY
vpc_id = module.network.vpc_id
}
servers/main.tf
# web
resource "ncloud_network_interface" "web" {
name = "be-nic-${var.env}"
description = "for Django web backend server"
subnet_no = ncloud_subnet.be-server.id
access_control_groups = [
data.ncloud_vpc.main.default_access_control_group_no,
ncloud_access_control_group.web.id,
]
}
# db
resource "ncloud_network_interface" "db" {
name = "db-nic-${var.env}"
description = "for DB server"
subnet_no = ncloud_subnet.be-server.id
access_control_groups = [
data.ncloud_vpc.main.default_access_control_group_no,
ncloud_access_control_group.db.id,
]
}
- 모듈 1개 & 변수리스트 1개 → 2개의 생성인스턴스
- 인스턴스 2개 생성
Revised code
staging/main.tf
# servers -> be & db
module "be" {
source = "../modules/server"
env = local.env
name = "be"
region = var.region
site = var.site
support_vpc = var.support_vpc
NCP_ACCESS_KEY = var.NCP_ACCESS_KEY
NCP_SECRET_KEY = var.NCP_SECRET_KEY
vpc_id = module.network.vpc_id
subnet_be_server = module.network.subnet_be_server
acg_port_range = "8000"
server_product_code = data.ncloud_server_products.sm.server_products[0].product_code
init_script_envs = {
username = var.username
password = var.password
NCP_ACCESS_KEY = var.NCP_ACCESS_KEY
NCP_SECRET_KEY = var.NCP_SECRET_KEY
NCP_CONTAINER_REGISTRY = var.NCP_CONTAINER_REGISTRY
IMAGE_TAG = var.IMAGE_TAG
db = local.db
db_port = local.db_port
db_host = ncloud_public_ip.db.public_ip
db_user = var.db_user
db_password = var.db_password
DJANGO_SETTINGS_MODULE = "lion_app.settings.staging"
DJANGO_SECRET_KEY = var.DJANGO_SECRET_KEY
}
init_script_path = "be_init_script.tftpl"
}
module "db" {
source = "../modules/server"
env = local.env
name = "db"
region = var.region
site = var.site
support_vpc = var.support_vpc
NCP_ACCESS_KEY = var.NCP_ACCESS_KEY
NCP_SECRET_KEY = var.NCP_SECRET_KEY
vpc_id = module.network.vpc_id
subnet_be_server = module.network.subnet_be_server
acg_port_range = "5432"
server_product_code = data.ncloud_server_products.sm.server_products[0].product_code
init_script_envs = {
username = var.username
password = var.password
db = local.db
db_port = local.db_port
db_user = var.db_user
db_password = var.db_password
}
init_script_path = "db_init_script.tftpl"
}
servers/main.tf
...
## server setup start
resource "ncloud_server" "main" {
subnet_no = var.subnet_be_server
name = "${var.name}-server-${var.env}"
server_image_product_code = "SW.VSVR.OS.LNX64.UBNTU.SVR2004.B050"
server_product_code = var.server_product_code
login_key_name = ncloud_login_key.loginkey.key_name
init_script_no = ncloud_init_script.main.id
network_interface {
network_interface_no = ncloud_network_interface.main.id
order = 0
}
}
## init script setup start
resource "ncloud_init_script" "main" {
name = "set-${var.name}-${var.env}-tf"
content = templatefile("${path.module}/${var.init_script_path}", var.init_script_envs)
}
...
- 모듈 2개 & 변수리스트 2개 → 생성인스턴스 1개 에서 2번 실행
- 인스턴스 2개 생성
Result
Check point
staging/main.tf → locals
locals {
env = "staging"
db = "lionforum"
db_port = "5432"
}
- 공개되어도 되는 내용
- 해당 main.tf 파일에서 여러번 사용되는 내용
init_script_env: servers/main.tf & servers/variables.tf & staging/main.tf
# servers/main.tf
# init script setup start
resource "ncloud_init_script" "main" {
name = "set-${var.name}-${var.env}-tf"
content = templatefile("${path.module}/${var.init_script_path}", var.init_script_envs)
}
# servers/variables.tf
variable "init_script_envs" {
type = map(any)
}
# staging/main.tf
module "be" {
source = "../modules/server"
env = local.env
name = "be"
region = var.region
site = var.site
support_vpc = var.support_vpc
NCP_ACCESS_KEY = var.NCP_ACCESS_KEY
NCP_SECRET_KEY = var.NCP_SECRET_KEY
vpc_id = module.network.vpc_id
subnet_be_server = module.network.subnet_be_server
acg_port_range = "8000"
server_product_code = data.ncloud_server_products.sm.server_products[0].product_code
init_script_envs = {
username = var.username
password = var.password
NCP_ACCESS_KEY = var.NCP_ACCESS_KEY
NCP_SECRET_KEY = var.NCP_SECRET_KEY
NCP_CONTAINER_REGISTRY = var.NCP_CONTAINER_REGISTRY
IMAGE_TAG = var.IMAGE_TAG
db = local.db
db_port = local.db_port
db_host = ncloud_public_ip.db.public_ip
db_user = var.db_user
db_password = var.db_password
DJANGO_SETTINGS_MODULE = "lion_app.settings.staging"
DJANGO_SECRET_KEY = var.DJANGO_SECRET_KEY
}
init_script_path = "be_init_script.tftpl"
}
- 딕셔너리로 들어가는 변수의 경우 map(any) 에 담아서 전달
728x90
반응형
'Dev. > Terraform' 카테고리의 다른 글
Terraform: AWS 서버구축 (0) | 2023.09.08 |
---|---|
Terraform: Status 가 꼬이는 현상 (0) | 2023.09.07 |
Terraform: Modules (0) | 2023.09.05 |
Terraform: LoadBalancer (0) | 2023.09.05 |
Terraform: AWS - VPC 생성 (0) | 2023.09.04 |
댓글