Ansible 每个客户EC2实例要部署的工具
我需要为每个客户部署一个EC2实例。我有数以百计的客户,而且还在增加 到目前为止,我发现Terraform支持count属性来提供我需要的实例的确切数量。为此,我维护了一个客户列表,创建了与列表长度相匹配的实例数。例如:Ansible 每个客户EC2实例要部署的工具,ansible,terraform,Ansible,Terraform,我需要为每个客户部署一个EC2实例。我有数以百计的客户,而且还在增加 到目前为止,我发现Terraform支持count属性来提供我需要的实例的确切数量。为此,我维护了一个客户列表,创建了与列表长度相匹配的实例数。例如: 顾客 顾客 客户 Terraform脚本如下所示: resource aws_instance x { count = length(var.customers) #... } 它最初起作用。但是,当我试图从列表中删除customer_b时,发生了意外行为,结果
- 顾客
- 顾客
- 客户
resource aws_instance x {
count = length(var.customers)
#...
}
它最初起作用。但是,当我试图从列表中删除customer_b时,发生了意外行为,结果customer_c的实例被销毁,customer_b的实例属性被customer_c替换。我需要的是保持customer_c的实例不受影响,并使用相关资源销毁customer_b的实例
我不确定是否有更好的方法使用Terraform来实现这一点,或者是否有其他更适合此用例的工具。请提供帮助。使用Terraform的
为每个
(从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
}
}
当我注释掉two
时,我得到了下面的地形图
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"
}
}
}
参考:太棒了!谢谢!
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.