How do you specify a VPC within a aws_elb resource definition?

548 views
Skip to first unread message

Henry Franco

unread,
Oct 29, 2018, 2:03:43 PM10/29/18
to Terraform
I've looked over aws_elb Terraform without any luck. I don't have a default VPC defined and I can't list a VPC in the aws_elb resource. How do I specify a VPC within my aws_elb resource? I'm getting the following error:

Error: Error applying plan:


1 error(s) occurred:


* module.rtr_nexus.aws_elb.my_elb: 1 error(s) occurred:


* aws_elb.nexus_elb: InvalidConfigurationRequest: Default VPC not found
 status code
: 409, request id: 7ad595ce-dba3-11e8-88ba-c3211a9a6526


Here's what my code looks like.
resource "aws_elb" "my_elb" {
  name                    
= "terraform-asg"
  availability_zones      
= ["${data.aws_availability_zones.all.names}"]
  security_groups        
= ["${aws_security_group.elb.id}"]

  listener
{
    lb_port              
= 80
    lb_protocol          
= "http"
    instance_port        
= "${var.elb_server_port}"
    instance_protocol    
= "http"
 
}


  health_check
{
    healthy_threshold    
= 2
    unhealthy_threshold  
= 2
    timeout              
= 3
    interval              
= 30
    target                
= "HTTP:${var.elb_server_port}/"
 
}
}


David Adams

unread,
Oct 29, 2018, 2:34:03 PM10/29/18
to terrafo...@googlegroups.com
You have to specify the subnets (one per AZ) that you want to use, using the `subnets` property. How you get the list of subnets depends on a lot of things. We typically have one public and one private subnet per AZ that we're using in each VPC. And so an Internet facing ELB would need to be in the public subnets. We tag our public subnets with `Visibility=Public`. So then we use essentially this code to get our list of public subnets for a VPC:

    data "aws_subnet_ids" "public" {
      vpc_id = "${var.vpc_id}"
      tags   = { Visibility = "Public" }
    }

And then you can just dump those into your ELB:

    resource "aws_elb" "example" {
      subnets = ["${data.aws_subnet_ids.public.ids}"]
      #...
    }

This _does_ depend on tagging discipline etc. So you may prefer to just hardcode a list of subnet IDs, or use some other method to look them up.

-dave


--
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/6787004e-2205-422a-9510-8b78ac4a109f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henry Franco

unread,
Oct 29, 2018, 3:29:42 PM10/29/18
to Terraform
David, that actually makes sense. I made the necessary adjustments using the code snippets you provided and it appears the subnet IDs and availability zones aren't matching?

Here's the error I'm getting:

Error: Error applying plan:


1 error(s) occurred:


* aws_elb.nexus_elb: 1 error(s) occurred:


* aws_elb.my_elb: ValidationError: Only one of SubnetIds or AvailabilityZones may be specified
 status code
: 400, request id: 8af03d68-dbaf-11e8-8e30-f1e0485f6068

Here's what my code now looks like. I also changed the tags to make the filter work:

resource "aws_elb" "my_elb" {
  name                  
= "terraform-asg-example"
  availability_zones    
= ["${data.aws_availability_zones.all.names}"]
  subnets              
= ["${data.aws_subnet_ids.public.ids}"]


  listener
{
    lb_port            
= "80"
    lb_protocol        
= "http"

    instance_port      
= "80"

    instance_protocol  
= "http"
 
}

  health_check
{
    healthy_threshold  
= 2
    unhealthy_threshold
= 2
    timeout            
= 3
    interval            
= 30

    target              
= "HTTP:80/"
 
}
}


data
"aws_availability_zones" "all" {}


data
"aws_subnet_ids" "public" {
  vpc_id                
= "${var.vpc_id}"
  tags                  
= { Visibility = "Public" }
}

Ryan Hartkopf

unread,
Oct 29, 2018, 11:32:49 PM10/29/18
to Terraform
Hi Henry,

Only one of SubnetIds or AvailabilityZones may be specified

Now that you're specifying the subnet IDs, the availability zones will be inferred from the subnet IDs provided. Try removing the `availability_zones` argument and planning again.
Reply all
Reply to author
Forward
0 new messages