简体   繁体   中英

Terraform using output from module

I just started with Terraform infrastructure. Trying to create a vpc module that will contain code for vpc, subnets, internet gateway, rout table. Also creating a separate tf file for rds, which will refer to the vpc module and utilize the private subnets declared in vpc module.

  1. Created a vpc module that has vpc.tf with following
provider "aws" {
    region = var.region
}

terraform {
  backend "s3"  {}
}

resource "aws_vpc" "production-vpc" {
    cidr_block = var.vpc_cidr
    enable_dns_hostnames = true
    tags = {
        Name = "Dev-VPC"
    }
}

// Private Subnets
resource "aws_subnet" "private-subnet-1" {
    cidr_block = var.private_subnet_1_cidr
    vpc_id = aws_vpc.production-vpc.id
    availability_zone = "us-east-1a"
    tags = {
        Name = "Private-Subnet-1"
    }
}

resource "aws_subnet" "private-subnet-2" {
    cidr_block = var.private_subnet_2_cidr
    vpc_id = aws_vpc.production-vpc.id
    availability_zone = "us-east-1b"
    tags = {
        Name = "Private-Subnet-2"
    }
}

The output.tf has following

output "private-subnet1-id" {
    description = "Private Subnet1 Id"
    value = aws_subnet.private-subnet-1.*.id
}

output "private-subnet2-id" {
    description = "Private Subnet2 Id"
    value = aws_subnet.private-subnet-2.*.id
}

The file is saved in folder \module\vpc folder

  1. Created rds.tf as follows in folder \rds
provider "aws" {
    region = var.region
}

terraform {
  backend "s3"  {}
}


module "vpc" {
    source = "../module/vpc"

}
resource "aws_db_subnet_group" "subnetgrp" {
    name = "dbsubnetgrp"
    subnet_ids = [module.vpc.private-subnet1-id.id, module.vpc.private-subnet2-id.id]
}
  1. When I run terraform plan, I get following error
Error: Unsupported attribute

  on rds.tf line 16, in resource "aws_db_subnet_group" "subnetgrp":
  16:     subnet_ids = [module.vpc.private-subnet1-id.id, module.vpc.private-subnet2-id.id]
    |----------------
    | module.vpc.private-subnet1-id is tuple with 1 element

This value does not have any attributes.

Error: Unsupported attribute

  on rds.tf line 16, in resource "aws_db_subnet_group" "subnetgrp":
  16:     subnet_ids = [module.vpc.private-subnet1-id.id, module.vpc.private-subnet2-id.id]
    |----------------
    | module.vpc.private-subnet2-id is tuple with 1 element

This value does not have any attributes.

You don't need the splat expression in the output.tf . Try the following,

output "private-subnet1-id" {
    description = "Private Subnet1 Id"
    value = aws_subnet.private-subnet-1.id
}

output "private-subnet2-id" {
    description = "Private Subnet2 Id"
    value = aws_subnet.private-subnet-2.id
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM