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

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

问题描述

main.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}"
  }
}

Variable.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//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天全站免登陆