Bo Bhayani
Bo Bhayani

Reputation: 35

Terraform Multiple instance creation error

Hello I'm trying to create multiple instance based on a count variable. I currently have 1 private subnet with 2 cidr blocks however when i try to create the instance i get

"Error: Error launching source instance: InvalidSubnetID.NotFound: The subnet ID '10.7.90.96/27' does not exist status code: 400, request id: d7ef5147-ac30-4d31-815a-ad6a46bfe456

on .terraform\modules\vpc\AWS-VPC-Module\main.tf line 1427, in resource "aws_instance" "FID": 1427: resource "aws_instance" "FID" {"

when clearly the TF does create the subnets are present

resource "aws_instance" "FID" {
  depends_on = [aws_kms_key.aws-wm-wmad-prod] 
  

   count =  var.How_many_FID

  ami                         = var.windows_dc_ami_2016
  availability_zone           = element(var.availability_zones, count.index)
  ebs_optimized               = var.windows_dc_ebs_optimized
  instance_type               = var.windows_dc_instance_type_FID
  key_name                    = var.key_name
  monitoring                  = true
  subnet_id                   = element(var.private_subnet_cidr_blocks, count.index)
                               
  associate_public_ip_address = false

here is my subnet creation code:

resource "aws_subnet" "private" {
  count = length(var.private_subnet_cidr_blocks) # count = 2

  vpc_id            = aws_vpc.default.id #id34odfjdf
  cidr_block        = var.private_subnet_cidr_blocks[count.index] 
  availability_zone = var.availability_zones[count.index] 

  tags = merge(
    {
      Name_TF        = "dctr-ad-sbn-use1-az1A-prod-lan-0${count.index+1}",
      Project     = var.project,
      Environment = var.environment
    },
    var.tags
  )
}

Upvotes: 0

Views: 153

Answers (1)

Marcin
Marcin

Reputation: 238081

subnet_id must be actual subnet id, not its CIDR:

subnet_id  = element(aws_subnet.private, count.index).id

Upvotes: 0

Related Questions