loop over the subnet-ids and create ec2 instance in each subnet

3,062 views
Skip to first unread message

Arbab Nazar

unread,
Feb 12, 2016, 1:57:00 PM2/12/16
to Terraform
Hi, I am really new to terraform and want to make this work. I am able to create the vpc, public subnets and get their ids, now I want to create an ec2 instance inside each of this subnet, when I try to run the ec2 module, it only create the instance inside the first subnet and ignore the other subnet(s). Here is snippet of my code.

OUTPUT the subnet ids:

output "public_subnets_id" {
  value = "${join(",", aws_subnet.public.*.id)}"
}

-----------------------
here the example output of this:
public_subnets_id = subnet-84aae6f4,subnet-a12124e8
----------------------


Here is my my code, where I am trying to split it and create the instance inside each subnet but can only create to the first subnet.

subnet_id = "${element(split(",", var.subnet_id), count.index)}"

Any help will be greatly appreciated.

Thanks,

Paul Hinze

unread,
Feb 12, 2016, 2:06:16 PM2/12/16
to terrafo...@googlegroups.com
Hello!

Your config is looking good so far. You didn't include the full contents of your instance resource, but my first guess would be that perhaps you are missing the "count" parameter?

resource "aws_instance" "foo" {
  count     = "${length(split(",", var.subnet_id)}"
  subnet_id = "${element(split(",", var.subnet_id), count.index)}"
  # ...
}

If that doesn't turn out to be it, send along your whole "aws_instance" resource and we can look further.

Paul

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/62eeff05-e340-4b39-97af-12a1aa4a2ccc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Arbab Nazar

unread,
Feb 12, 2016, 2:29:55 PM2/12/16
to Terraform
Hello Paul,

Thanks for the help, I have add the count suggestion inside the module but get this error, when I run terraform plan

Error configuring: 1 error(s) occurred:

* Cycle: module.ec2.var.subnet_id, module.ec2.aws_instance.ec2 (destroy), module.vpc_subnets.aws_subnet.public (destroy), module.vpc_subnets.aws_subnet.public, module.vpc_subnets.output.public_subnets_id

Here is my complete contents of my resource:

resource "aws_instance" "ec2" {
  ami = "${var.ami_id}"
  vpc_security_group_ids = ["${split(",", var.security_group_id)}"]
  key_name = "${var.key_name}"
  count = "${length(split(",", var.subnet_id))}"
  subnet_id = "${element(split(",", var.subnet_id), count.index)}"
  instance_type = "${var.instance_type}"
  root_block_device {
        volume_type = "${var.ebs_root_volume_type}"
        volume_size = "${var.ebs_root_volume_size}"
        delete_on_termination = "${var.ebs_root_delete_on_termination}"
  }
  tags {
    Name        = "${var.name}-${var.environment}-${count.index}"
    environment = "${var.environment}"

Paul Hinze

unread,
Feb 12, 2016, 2:36:26 PM2/12/16
to terrafo...@googlegroups.com
I don't see any circular references in the config you included. The other thing to check for would be any usage of `create_before_destroy` lifecycle modifiers - these need to be handled carefully when dealing with cross module dependencies. Do you have any of these in your config? If so - I'd try removing them for now to see if the cycle goes away.

Paul



Arbab Nazar

unread,
Feb 12, 2016, 2:43:23 PM2/12/16
to terrafo...@googlegroups.com
Paul,

I have removed the `create_before_destroy`from my vpc module but still getting the same error, here is my complete vpc module for reference. Thanks

// Create the VPC
resource "aws_vpc" "vpc" {
  cidr_block           = "${var.vpc_cidr}"
  enable_dns_support   = "${var.enable_dns_support}"
  enable_dns_hostnames = "${var.enable_dns_hostnames}"

  tags {
    Name = "${var.name}-${var.environment}-vpc"
    environment =  "${var.environment}"
  }

}

output "vpc_id" {
  value = "${aws_vpc.vpc.id}"
}

// Create the IGW
resource "aws_internet_gateway" "igw" {
  vpc_id = "${aws_vpc.vpc.id}"
  tags { 
  Name = "${var.name}-${var.environment}-igw"
  }
  
}

// Create Public Subnets
resource "aws_subnet" "public" {
  vpc_id                  = "${aws_vpc.vpc.id}"
  cidr_block              = "${element(split(",", var.public_subnets_cidr), count.index)}"
  availability_zone       = "${element(split(",", var.azs), count.index)}"
  count                   = "${length(split(",", var.public_subnets_cidr))}"
  map_public_ip_on_launch = "${var.map_public_ip_on_launch}"

  tags {
    Name = "${var.name}-public-${element(split(",", var.azs), count.index)}"
  }
}

output "public_subnets_id" {
  value = "${join(",", aws_subnet.public.*.id)}"
}

resource "aws_route_table" "public" {
  vpc_id = "${aws_vpc.vpc.id}"

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.igw.id}"
  }

  tags {
    Name = "${var.name}-public"
  }
}

resource "aws_route_table_association" "public" {
  //count          = "${length(split(",", var.cidrs))}" also check count.index
  subnet_id      = "${element(aws_subnet.public.*.id, count.index)}"
  route_table_id = "${aws_route_table.public.id}"
}

// Create the Private Subnets
resource "aws_subnet" "private" {
  vpc_id                  = "${aws_vpc.vpc.id}"
  cidr_block              = "${element(split(",", var.private_subnets_cidr), count.index)}"
  availability_zone       = "${element(split(",", var.azs), count.index)}"
  count                   = "${length(split(",", var.private_subnets_cidr))}"
  map_public_ip_on_launch = false

  tags {
    Name = "${var.name}-private-${element(split(",", var.azs), count.index)}"
  }
}

resource "aws_route_table" "private" {
  vpc_id = "${aws_vpc.vpc.id}"

  tags {
    Name = "${var.name}-private"
  }
}

resource "aws_route_table_association" "private" {
  //count          = "${length(split(",", var.cidrs))}"
  subnet_id      = "${element(aws_subnet.private.*.id, count.index)}"
  route_table_id = "${aws_route_table.private.id}"
}



You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/8IjXboKp4MA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/CAJpeNAMNYBCL0swuH%2BEm3dpxwVchSdgQm5o_KUU-40rj-kXpow%40mail.gmail.com.

Arbab Nazar

unread,
Feb 12, 2016, 2:44:56 PM2/12/16
to Terraform
Paul, I have removed the `create_before_destroy` but still get the same error, here is my complete vpc module. Thanks

// Create the VPC
resource "aws_vpc" "vpc" {
  cidr_block           = "${var.vpc_cidr}"
  enable_dns_support   = "${var.enable_dns_support}"
  enable_dns_hostnames = "${var.enable_dns_hostnames}"

  tags {
    Name = "${var.name}-${var.environment}-vpc"
    environment =  "${var.environment}"
  }

}

output "vpc_id" {
  value = "${aws_vpc.vpc.id}"
}

// Create the IGW
resource "aws_internet_gateway" "igw" {
  vpc_id = "${aws_vpc.vpc.id}"
  tags { 
  Name = "${var.name}-${var.environment}-igw"
  }
  
}

// Create Public Subnets
resource "aws_subnet" "public" {
  vpc_id                  = "${aws_vpc.vpc.id}"
  cidr_block              = "${element(split(",", var.public_subnets_cidr), count.index)}"
  availability_zone       = "${element(split(",", var.azs), count.index)}"
  count                   = "${length(split(",", var.public_subnets_cidr))}"
  map_public_ip_on_launch = "${var.map_public_ip_on_launch}"

  tags {
    Name = "${var.name}-public-${element(split(",", var.azs), count.index)}"
  }
}

output "public_subnets_id" {
  value = "${join(",", aws_subnet.public.*.id)}"
}

Paul Hinze

unread,
Feb 12, 2016, 3:08:42 PM2/12/16
to terrafo...@googlegroups.com
Unfortunately, I can't see anything jumping out at me based on the config you've quoted.

If create_before_destroy has been removed from your config, the cycle error should be pointing out a legitimate circular reference in your config - I would take a close look at every resource mentioned in the cycle and audit them for circular references. You can experiment by subbing out dummy static values for references to help narrow down where the cycle might be.

If you still can't figure it out after doing that - I'd recommend pushing your full config contents up to a repository or GitHub Gist so we on the list can reproduce the error message and help you diagnose.

Hope this helps!

Paul

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

Arbab Nazar

unread,
Feb 13, 2016, 1:17:19 AM2/13/16
to Terraform
I have upload the complete configuration here:
kindly review it, I have tried different things but couldn't make it work because of my limited knowledge to Terraform. 
Appreciated you help.

Paul Hinze

unread,
Feb 16, 2016, 1:39:21 PM2/16/16
to terrafo...@googlegroups.com
Thanks for providing the full example, Arbab.

Looks like this is an expression of an existing open Terraform bug:


You can work around for now by switching the `count` on instances to be a static value. And you can track the above linked issue to learn when the bug has been fixed in Terraform.

Let me know if the workaround unblocks you,

Paul


Reply all
Reply to author
Forward
0 new messages