nimbleclick
nimbleclick

Reputation: 1

How to pass AZ information from Terraform VPC Module

I'm trying to grab the Availability Zones from a VPC module and pass it into the ELB resource parameters. How do I specify the AZs that are hosting the two public subnets where I want the ELBs to send traffic?

Here is the error I am getting:

Error: Error creating ELB: ValidationError: Invalid Availability Zone: module.network_stack.availability_zones[*]
│   status code: 400, request id: f9e5fc9b-d158-443f-8032-8706578f3b0c
│ 
│   with aws_elb.web_elb,
│   on main.tf line 55, in resource "aws_elb" "web_elb":
│   55: resource "aws_elb" "web_elb" {

Here is a link to the GitHub Repo:

https://github.com/cdziachan/Terraform-HA-Web-App

I'm specifically getting the error from this ELB Resource in Main.tf:

resource "aws_elb" "web_elb" {
    name               = "web-elb"
    availability_zones = ["module.network_stack.availability_zones[*]"]
    security_groups    = [module.network_stack.web_sg_id]
    listener {
        instance_port     = 80
        instance_protocol = "http"
        lb_port           = 80
        lb_protocol       = "http"
    }
    health_check {
        healthy_threshold   = 2
        unhealthy_threshold = 2
        timeout             = 10
        target              = "HTTP:80/"
        interval            = 30
    }
}

Outputs.tf from the VPC Module:

output "availability_zones" {
  value = data.aws_availability_zones.available[*].names
}

Public Subnets from Main.tf in VPC Module:

resource "aws_subnet" "public_subnets" {
  count                   = length(var.public_subnet_cidrs)
  vpc_id                  = data.aws_vpc.prod.id
  cidr_block              = var.public_subnet_cidrs[count.index]
  availability_zone       = data.aws_availability_zones.available.names[count.index]
  map_public_ip_on_launch = true

  tags = merge(var.tags, {
    Name   = "Public Subnet ${count.index + 1}"
    Region = "${data.aws_region.current.description}"
    Env    = "${var.env}"
    VPC    = "${data.aws_vpc.prod.id}"
  })
}

Upvotes: 0

Views: 401

Answers (1)

Marcin
Marcin

Reputation: 238797

Your availability_zones is a literal string: "module.network_stack.availability_zones[*]". Instead it should be:

 availability_zones = module.network_stack.availability_zones[*]

Upvotes: 0

Related Questions