Terraform - AWS 多子网问题
Terraform - AWS Multiple subnets issue
我正在尝试部署多个 ec2 实例,其中每个 Web 服务器 ec2 实例位于不同的子网中,应用程序服务器实例位于特定的子网中。
当使用计数参数时,每个网络服务器都正确部署在不同的子网中,但应用程序实例部署在同一个子网中,而不是我在 "Application resource" 中指定的两个子网中 下面给出的部分。
如何指定要在特定子网中创建的应用程序服务器,这些子网使用下面 "Public Subnets Resource" 部分中指定的计数参数创建。
.
变量声明:
variable "subnet_private_cidr" {
type = "list"
default = ["10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24", "10.10.4.0/24"]
}
variable "subnet_public_cidr" {
type = "list"
default = ["10.10.0.0/24", "10.10.5.0/24", "10.10.6.0/24", "10.10.7.0/24"]
}
variable "azs" {
type = "list"
default = ["us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d"]
}
Public 子网资源:
resource "aws_subnet" "qasubnet_public" {
count = "${length(var.subnet_public_cidr)}"
vpc_id = "${aws_vpc.qa_vpc.id}"
cidr_block = "${element(var.subnet_public_cidr,count.index)}"
availability_zone = "${element(var.azs,count.index)}"
tags {
Name = "QASUBNET_PUBLIC-${count.index+1}"
}
}
私有子网资源:
resource "aws_subnet" "qasubnet_private" {
count = "${length(var.subnet_private_cidr)}"
vpc_id = "${aws_vpc.qa_vpc.id}"
cidr_block = "${element(var.subnet_private_cidr,count.index)}"
availability_zone = "${element(var.azs,count.index)}"
tags {
Name = "QASUBNET_PRIVATE-${count.index+1}"
}
}
网络服务器资源:
resource "aws_instance" "webserver" {
count = "${length(var.subnet_public_cidr)}"
ami = "${var.webserver_ami}"
instance_type = "t2.medium"
vpc_security_group_ids = ["${aws_security_group.webserver.id}"]
key_name = "${var.aws_key_name}"
subnet_id = "${element(aws_subnet.qasubnet_public.*.id,count.index)}"
}
应用程序资源:
resource "aws_instance" "appserver" {
count = 2
ami = "${var.appserver_ami}"
instance_type = "t2.medium"
vpc_security_group_ids = ["${aws_security_group.appserver.id}"]
key_name = "${var.aws_key_name}"
subnet_id = "${element(aws_subnet.qasubnet_private.*.id,0)}"
subnet_id = "${element(aws_subnet.qasubnet_private.*.id,1)}"
}
我不确定你想在你的中做什么:
subnet_id = "${element(aws_subnet.qasubnet_private.*.id,0)}"
subnet_id = "${element(aws_subnet.qasubnet_private.*.id,1)}"`
在您的资源 "aws_instance.appserver" 中,它应该只是 subnet_id 参考:
subnet_id = "${element(aws_subnet.qasubnet_private.*.id,count.index)}"
使用计数语句,这将为每个子网提供一个实例。
我正在尝试部署多个 ec2 实例,其中每个 Web 服务器 ec2 实例位于不同的子网中,应用程序服务器实例位于特定的子网中。
当使用计数参数时,每个网络服务器都正确部署在不同的子网中,但应用程序实例部署在同一个子网中,而不是我在 "Application resource" 中指定的两个子网中 下面给出的部分。
如何指定要在特定子网中创建的应用程序服务器,这些子网使用下面 "Public Subnets Resource" 部分中指定的计数参数创建。
.
变量声明:
variable "subnet_private_cidr" {
type = "list"
default = ["10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24", "10.10.4.0/24"]
}
variable "subnet_public_cidr" {
type = "list"
default = ["10.10.0.0/24", "10.10.5.0/24", "10.10.6.0/24", "10.10.7.0/24"]
}
variable "azs" {
type = "list"
default = ["us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d"]
}
Public 子网资源:
resource "aws_subnet" "qasubnet_public" {
count = "${length(var.subnet_public_cidr)}"
vpc_id = "${aws_vpc.qa_vpc.id}"
cidr_block = "${element(var.subnet_public_cidr,count.index)}"
availability_zone = "${element(var.azs,count.index)}"
tags {
Name = "QASUBNET_PUBLIC-${count.index+1}"
}
}
私有子网资源:
resource "aws_subnet" "qasubnet_private" {
count = "${length(var.subnet_private_cidr)}"
vpc_id = "${aws_vpc.qa_vpc.id}"
cidr_block = "${element(var.subnet_private_cidr,count.index)}"
availability_zone = "${element(var.azs,count.index)}"
tags {
Name = "QASUBNET_PRIVATE-${count.index+1}"
}
}
网络服务器资源:
resource "aws_instance" "webserver" {
count = "${length(var.subnet_public_cidr)}"
ami = "${var.webserver_ami}"
instance_type = "t2.medium"
vpc_security_group_ids = ["${aws_security_group.webserver.id}"]
key_name = "${var.aws_key_name}"
subnet_id = "${element(aws_subnet.qasubnet_public.*.id,count.index)}"
}
应用程序资源:
resource "aws_instance" "appserver" {
count = 2
ami = "${var.appserver_ami}"
instance_type = "t2.medium"
vpc_security_group_ids = ["${aws_security_group.appserver.id}"]
key_name = "${var.aws_key_name}"
subnet_id = "${element(aws_subnet.qasubnet_private.*.id,0)}"
subnet_id = "${element(aws_subnet.qasubnet_private.*.id,1)}"
}
我不确定你想在你的中做什么:
subnet_id = "${element(aws_subnet.qasubnet_private.*.id,0)}"
subnet_id = "${element(aws_subnet.qasubnet_private.*.id,1)}"`
在您的资源 "aws_instance.appserver" 中,它应该只是 subnet_id 参考:
subnet_id = "${element(aws_subnet.qasubnet_private.*.id,count.index)}"
使用计数语句,这将为每个子网提供一个实例。