Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

amazon web services - 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 modulevpc folder

  1. Created rds.tf as follows in folder ds
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.

question from:https://stackoverflow.com/questions/65545541/terraform-using-output-from-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...