每个客户EC2实例部署工具

pap*_*agi 2 ansible terraform

我需要为每个客户部署一个EC2实例。我有数百名客户,而且还在不断增加。

到目前为止,我发现Terraform支持count属性以提供所需的确切实例数。为此,我维护一个客户列表,创建实例数以匹配列表的长度。例如:

  • customer_a
  • customer_b
  • customer_c

Terraform脚本如下所示:

resource aws_instance x {
  count = length(var.customers)

  #...
}
Run Code Online (Sandbox Code Playgroud)

它最初可以工作。但是,当我尝试从列表中删除customer_b时,发生了意外的行为,结果,customer_c的实例被销毁,而customer_b的实例属性被customer_c的实例替换。我需要的是保持customer_c的实例不变,并使用相关资源销毁customer_b的实例。

我不确定是否有更好的方法可以使用Terraform来完成此操作,或者是否有其他适合此用例的工具。请帮忙。

Car*_*lli 5

使用Terraform for_each(可从v0.12.x开始)实际上可以完成此任务。

这将创建3个AWS实例:

variable "hosts" {
  default = {
    "one" = {
      "name"    = "one",
      "machine" = "t2.micro",
      "ami"     = "ami-009d6802948d06e52",
      "subnet"  = "subnet-002df68a36948517d"
    },
    "two" = {
      "name"    = "two",
      "machine" = "t3.micro",
      "ami"     = "ami-009d6802948d06e52",
      "subnet"  = "subnet-01c13b00a5531828e"
    },
    "three" = {
      "name"    = "three",
      "machine" = "t2.nano",
      "ami"     = "ami-009d6802948d06e52",
      "subnet"  = "subnet-0166523e6bd98ebd8"
    }
  }
}

resource "aws_instance" "instances" {
  for_each      = var.hosts
  ami           = each.value.ami
  instance_type = each.value.machine
  subnet_id     = each.value.subnet
  tags = {
    Name = each.value.name
  }
}
Run Code Online (Sandbox Code Playgroud)

当我注释掉后two,我得到下面的Terraform计划

variable "hosts" {
  default = {
    "one" = {
      "name"    = "one",
      "machine" = "t2.micro",
      "ami"     = "ami-009d6802948d06e52",
      "subnet"  = "subnet-002df68a36948517d"
    },
    # "two" = {
    #   "name"    = "two",
    #   "machine" = "t3.micro",
    #   "ami"     = "ami-009d6802948d06e52",
    #   "subnet"  = "subnet-01c13b00a5531828e"
    # },
    "three" = {
      "name"    = "three",
      "machine" = "t2.nano",
      "ami"     = "ami-009d6802948d06e52",
      "subnet"  = "subnet-0166523e6bd98ebd8"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)
Terraform will perform the following actions:

  # aws_instance.instances["two"] will be destroyed
  - resource "aws_instance" "instances" {
      - ami                          = "ami-009d6802948d06e52" -> null
      - arn                          = "arn:aws:ec2:us-east-1:XXXXXXXX:instance/i-03a8285fc49f48a69" -> null
      - associate_public_ip_address  = true -> null
      - availability_zone            = "us-east-1b" -> null
      - cpu_core_count               = 1 -> null
      - cpu_threads_per_core         = 2 -> null
      - disable_api_termination      = false -> null
      - ebs_optimized                = false -> null
      - get_password_data            = false -> null
      - id                           = "i-03a8285fc49f48a69" -> null
      - instance_state               = "running" -> null
      - instance_type                = "t3.micro" -> null
      - ipv6_address_count           = 0 -> null
      - ipv6_addresses               = [] -> null
      - monitoring                   = false -> null
      - primary_network_interface_id = "eni-05f51ded8af0b5033" -> null
      - private_dns                  = "ip-172-31-36-204.ec2.internal" -> null
      - private_ip                   = "172.31.36.204" -> null
      - security_groups              = [
          - "default",
        ] -> null
      - source_dest_check            = true -> null
      - subnet_id                    = "subnet-01c13b00a5531828e" -> null
      - tags                         = {
          - "Name" = "two"
        } -> null
      - tenancy                      = "default" -> null
      - volume_tags                  = {} -> null
      - vpc_security_group_ids       = [
          - "sg-06aabe12f0a1b34fd",
        ] -> null

      - credit_specification {
          - cpu_credits = "unlimited" -> null
        }

      - root_block_device {
          - delete_on_termination = true -> null
          - encrypted             = false -> null
          - iops                  = 100 -> null
          - volume_id             = "vol-06ffd6ab6d4e8f671" -> null
          - volume_size           = 8 -> null
          - volume_type           = "gp2" -> null
        }
    }

Plan: 0 to add, 0 to change, 1 to destroy.
Run Code Online (Sandbox Code Playgroud)

参考:Terraform资源For_Each