Terraform: module化する話
モノはこんな
$ cd terra-modulized-es $ tree . . ├── modules │ └── elasticsearch │ └── aws_es_node.tf └── services └── elasticsearch ├── production │ ├── main.tf └── staging └── main.tf 6 directories, 4 files
ここに変数の中身を書く
$cat terra-modulized-es/services/elasticsearch/production/main.tf module "production-es-cluster" { region = "ap-northeast-1" source = "../../../modules/elasticsearch" lb_name = "production-es-alb" internal_option = "false" lb_type = "application" es_alb_sg = "sg-xxxxxxxxxxxxxxxxx" es_alb_subnet = ["subnet-xxxxxxxx", "subnet-xxxxxxxx"] env = "production" }
こっちは変数の定義とリソースのオプションを書くだけ
$cat terra-modulized-es/modules/elasticsearch/aws_es_node.tf provider "aws" { region = "${var.region}" } variable "region" {} variable "lb_name" {} variable "internal_option" {} variable "lb_type" {} variable "es_alb_sg" {} variable "es_alb_subnet" { type = "list" } variable "env" {} resource "aws_lb" "es-alb" { name = "${var.lb_name}" internal = "${var.internal_option}" load_balancer_type = "${var.lb_type}" security_groups = ["${var.es_alb_sg}"] subnets = ["${var.es_alb_subnet}"] enable_deletion_protection = false tags = { Environment = "${var.env}" } }
できた。
$ cd terra-modulized-es/services/elasticsearch/production $ terraform apply An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: + module.production-es-cluster.aws_lb.es-alb 〜略〜 module.production-es-cluster.aws_lb.es-alb: Still creating... (2m10s elapsed) module.production-es-cluster.aws_lb.es-alb: Creation complete after 2m13s (ID: arn:aws:elasticloadbalancing:ap-northea...app/production-es-alb/xxxxxxxx) Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
variableも外だしした方がいいな。