问题描述
我的vpc状态文件位于dev / vpc / main / terraform.tfstate。我想使用vpc中的私有子网来预配置mariaDB。 我遇到错误:
- module.csc_db_sbnet_group.var.db_subnet_group_ids:element:element()不得与以下列表中的空列表一起使用:
$ {element(data.terraform_remote_state.vpc_subnets_ids.vpc_private_subnets_ids,0)}
MariaDB代码(注意:MariaDB代码具有自己的terrafrom状态文件):
data_sources.tf:
data "terraform_remote_state" "vpc_subnets_ids" {
backend = "s3"
config {
bucket = "dev-terraform-state"
key = "dev/vpc/main/terraform.tfstate"
region = "us-west-2"
}
}
resources.tf:
module "csc_db_sbnet_group" {
source = "modules/rds-subnet-group"
db_subnet_group_name = "${var.db_subnet_group_name}"
db_subnet_group_ids = ["${element(data.terraform_remote_state.vpc_subnets_ids.vpc_private_subnets_ids,0)}","${element(data.terraform_remote_state.vpc_subnets_ids.vpc_private_subnets_ids,1)}"]
}
VPC代码:
resources.tf:
module "vpc" {
source = "modules/vpc"
aws_region = "${var.region}"
vpc_tag_name = "${var.vpc_name}"
vpc_cidr = "${var.vpccidr}"
private-subnet-mapping = ["${var.private_az_subnets_cidr}"]
public-subnet-mapping = ["${var.public_az_subnets_cidr}"]
}
resource "aws_subnet" "add_private_subnets" {
count = "${length(var.private-subnet-mapping)}"
cidr_block = "${lookup(var.private-subnet-mapping[count.index],"cidr")}"
vpc_id = "${module.vpc.vpc_id}"
availability_zone = "${lookup(var.private-subnet-mapping[count.index],"az")}"
tags = {
Name = "${lookup(var.private-subnet-mapping[count.index],"name")}"
BU = "${lookup(var.private-subnet-mapping[count.index],"BU")}"
}
}
outputs.tf
output "vpc_private_subnets_ids" {
value = ["${aws_subnet.add_private_subnets.*.id}"]
}
output "vpc_private_subnets_cidrs" {
value = ["${aws_subnet.add_private_subnets.*.cidr_block}"]
}
我提供MariaDB时,我想使用VPC代码中提供的专用子网。但是,它抱怨,
- module.csc_db_sbnet_group.var.db_subnet_group_ids:element:element()不得与以下列表中的空列表一起使用:
$ {element(data.terraform_remote_state.vpc_subnets_ids.vpc_private_subnets_ids,0)}
解决方法
我在下面为VPC添加了代码,并解决了问题。
data "aws_subnet_ids" "private_subnet_ids" {
vpc_id = "${module.vpc.vpc_id}"
}
data "aws_subnet" "private_subnet" {
count = "${length(data.aws_subnet_ids.private_subnet_ids.ids)}"
id = "${data.aws_subnet_ids.private_subnet_ids.ids[count.index]}"
}