I'm attempting to deploy multiple ec2 instances, in which each Web Server ec2 instances in different sub-nets and Application Server instance in specific sub-nets.
When using count parameter each webservers get properly deployed in different sub-nets, but application instance get deployed in same sub-net instead of two sub-net which i specified on "Application resource" section given below.
How can i specify application server to be created in specific sub-nets which created using count parameter as specified in below "Public Subnets Resource" section.
.
Variable declaration :
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 Subnets Resource :
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}"
}
}
Private Subnets Resource :
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}"
}
}
Webserver Resource :
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)}"
}
Application Resource :
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)}"
}