Hi Team , i was practicing aws ec2 instance creation using aws ec2 module and i want to deploy 2 ec2 instances for 2 subnet ids. But i am unable to do that , Can you please help me ?
Here is the aws ec2 module tf file that i am using
# private ec2 instance host
module "private_instance" {
depends_on = [module.vpc]
source = "terraform-aws-modules/ec2-instance/aws"
version = "4.3.0"
count = length(var.instance_count)
tags = {
"Name" = var.instance_count[count.index]
}
ami = data.aws_ami.amzlinux2.id
instance_type = var.instance_type
subnet_id = (var.vpc_private_subnets [count.index])
user_data = file("${path.module}/app1-user-data.sh")
vpc_security_group_ids = [module.private-security-group.security_group_id]
key_name = var.instance_keypair
}
And also i am creating vpc and my private subnets using vpc module
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.19.0"
#version = "~> 2.78"
# VPC Basic Details
name = "${local.name}-${var.vpc_name}"
cidr = var.vpc_cidr_block
azs = var.vpc_availability_zones
public_subnets = var.vpc_public_subnets
private_subnets = var.vpc_private_subnets
# NAT Gateways - Outbound Communication
enable_nat_gateway = var.vpc_enable_nat_gateway
single_nat_gateway = var.vpc_single_nat_gateway
# VPC DNS Parameters
enable_dns_hostnames = true
enable_dns_support = true
tags = local.common_tags
vpc_tags = local.common_tags
# Additional Tags to Subnets
public_subnet_tags = {
Type = "Public Subnets"
}
private_subnet_tags = {
Type = "Private Subnets"
}
database_subnet_tags = {
Type = "Private Database Subnets"
}
}
And i am using 2 subnets for 2 ec2 instances meaning i want to create ec2 instance in each subnet
# VPC Variables
vpc_name = "myvpc"
vpc_cidr_block = "10.0.0.0/16"
vpc_availability_zones = ["us-west-2a", "us-west-2b"]
vpc_public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
vpc_private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
vpc_database_subnets = ["10.0.151.0/24", "10.0.152.0/24"]
vpc_create_database_subnet_group = true
vpc_create_database_subnet_route_table = true
vpc_enable_nat_gateway = true
vpc_single_nat_gateway = true