Variance in attributes based on count.index in terraform

后端 未结 3 1207
-上瘾入骨i
-上瘾入骨i 2020-12-21 10:37

I\'m using Hashicorp terraform to create a MySQL cluster on AWS. I created a module named mysql and want to tag the first instance created as the master

3条回答
  •  清酒与你
    2020-12-21 11:09

    The way you have count in the module resource would infer that you want 3 modules created, rather than 3 resources within the module created. You can stipulate the count from the module resource but any logic using count.index needs to sit within the module.

    main.tf

    module "mysql_cluster" {
      source          = "./modules/mysql"
      instance_count  = 3
    }
    

    mysql.tf

    resource "aws_instance" "mysql" {
      count         = "${var.instance_count}"
      ami           = "ami-123456"
      instance_type = "t2.xlarge"
      key_name      = "rsa_2048"
    
      tags {
        Role        = "${count.index == "0" ? "master" : "slave"}"
      }
    }
    

提交回复
热议问题