Amit
Amit

Reputation: 1672

Terraform Error while Passing output variable from one module to another

I am using two modules vpc and lb and passing an output varibale in vcp module to lb module as below in the code. But I am getting following error -

Error: Unsupported attribute

  on main.tf line 35, in module "lb":
  35:   vpc-id = module.vpc.vpc-id

This value does not have any attributes.

Here is code - main.tf

provider "aws" {
  shared_credentials_file = "~/.aws/credentials"
  region =var.region      
}
# Include modules
module "vpc" {
  count = var.create_default_vpc ? 1:0
  source = "./modules/vpc"
  region = var.region
  zones = var.zones
}

module "lb" {
  source  = "./modules/lb"
  image = var.image
  instance_type = var.instance_type
  vpc-id = module.vpc.vpc-id
}

./modules/vpc/vpc.tf

resource "aws_vpc" "xcloud-vpc" {
  cidr_block       = "10.0.0.0/16"
  enable_dns_hostnames = true

  tags = {
    Name = "xcloud-vpc"
  }
}

./modules/vpc/vpc-output.tf

output "vpc-id" {
  value = aws_vpc.xcloud-vpc.id
}

./modules/lb/lb.tf

resource "aws_security_group" "allow_http" {
  name        = "xcloud-sg-allow-http"
  description = "Allow HTTP & ICMP inbound connections"
  vpc_id = var.vpc-id
  # vpc_id = module.vpc.vpc-id
  <some more ingress, egress code>

./modules/lb/lb-vars.tf

variable image {}
variable instance_type {}
variable vpc-id{}

Though I am passing vpc-id = module.vpc.vpc-id while including "lb" module in the main.tf , the above error is given for this line of code. Any pointer will be appreciated.

Upvotes: 1

Views: 1320

Answers (1)

Marcin
Marcin

Reputation: 238847

Since you are using count in your module, you should refer to individual instances of the module, even if you have only one. So in your case, it would be:

vpc_id = module.vpc[0].vpc-id

Upvotes: 3

Related Questions