Terraform:如何附加服务器计数并将服务器分配给多个可用区? [英] Terraform: How to append the server count and assign servers to multiple AZ's?

查看:10
本文介绍了Terraform:如何附加服务器计数并将服务器分配给多个可用区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ma​​in.tf

resource "aws_instance" "service" {
  ami           = "${lookup(var.aws_winamis, var.awsregion)}"
  count         = "${var.count}"
  key_name      = "${var.key_name}"
  instance_type = "t2.medium"
  subnet_id     = "${aws_subnet.private.id}"
  # private_ip    = "${lookup(var.server_instance_ips, count.index)}"
  vpc_security_group_ids = ["${aws_security_group.private-sg.id}"]
  associate_public_ip_address = false
  availability_zone           = "${var.awsregion}a"
  tags {
    Name        = "${format("server-%01d", count.index + 1)}"
    Environment = "${var.environment}"
  }
}

变量.tf

variable "awsregion" {default =""}
variable "count" {default = ""}
variable "server_instance_ips" {default = ""}

dev.tfvars

server_instance_ips = ["10.0.2.25", "10.0.2.26"] #doesn't work as a list but works with single IP address
count = "4"
awsregion = "us-east-1"

我希望服务器有标签 - dla-server-1/2/3/4在创建它们之后,但使用上面的代码,我只能执行 server-1/2/3/4 但不是 dla/sla/pla 取决于环境;IP 地址是随机分配给服务器的,因为我无法传递 IP 列表,最后,如何在不同的 AZ 中创建服务器,即 AZ1a 中的 2 个服务器和 AZ1b 中的 2 个服务器?

I want the servers to have tags - dla-server-1/2/3/4 after they're created but with my above code I can only do server-1/2/3/4 but not dla/sla/pla depending on the environments; IP addresses are randomly assigned to the servers as I'm not able to pass the list of IP's and lastly, how can I create servers in different AZ's i.e. 2 servers in AZ1a and 2 servers in AZ1b?

推荐答案

第一个答案很简单:在标签名称中添加前缀.

The first answer is easy: add the prefix in the tag name.

tags {
  Name        = "${var.environment}-${format("server-%01d", count.index + 1)}"
  Environment = "${var.environment}"
}

需要指定变量的类型为列表:

You need to specify the type of the variable to be a list:

variable "server_instance_ips"
{
  type = "list"
  default = []
}

最后一个问题可以通过多种方式解决,我会选择类似的方法

The last question can be solved in many ways, I would go for something similar

availability_zone = "${data.aws_availability_zones.available.names[count.index]}"

这篇关于Terraform:如何附加服务器计数并将服务器分配给多个可用区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆