Hello all, this is my first question here. Apologize if something goes wrong, I’m also a newbie in Terraform,
Anyway, I have two modules in my AWS Terraform script, and it should be as simple as possible, just to help me to understand how it works.
In the vpc module, I create the VPC itself, subnets (let’s say 6 of them, 3 public, 3 private), internet gateway, route tables, and NAT gateway.
########################################
# starting private subnet - API
resource "aws_subnet" "api-subnets" {
count = var.api_subnets_size
vpc_id = aws_vpc.main-pub-vpc.id
cidr_block = "10.0.${count.index+10}.0/24"
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = false
tags = {
"Name" = "${var.prefix}-subnet-api-${count.index+1}",
"cliente" = var.client,
"ambiente" = "dev"
}
}
exposing them to other modules (I think they are exported as a list)
output "api_subnet_ids" {
value = aws_subnet.api-subnets[*].id
}
In the rds module, I create a Postgres instance in the private subnets, so I have to get them from the vpc module to create the subnet group. Easy, right?
First I have to declare rds module in my main.tf and get that output from the vpc module.
module "vpc" {
source = "./modules/vpc"
prefix = var.prefix
client = var.client
api_subnets_size = var.api_subnets_size
api_subnet_ids = module.vpc.api_subnet_ids <== HERE
}
Then the variable is used to set up the subnet ids list in the rds module:
module "rds" {
source = "./modules/rds"
prefix = var.prefix
client = var.client
api_subnet_ids = module.vpc.api_subnet_ids <== AND HERE
}
Finally, I use this variable to set the subnets in the rds subnets group:
resource "aws_db_subnet_group" "api-subnet-grp" {
name = "${var.prefix}-api-subnet-grp"
subnet_ids = api_subnet_ids # -> why doesn't work here? It is a list in both source and destination parameters
description = "subnet group to allow Multi AZ on this instance"
tags = {
"Name" = "${var.prefix}-api-subnet-grp",
"cliente" = var.client,
"ambiente" = "dev"
}
}
Apparently, the subnet_ids
property type of resource aws_db_subnet_group is different from subnets
property type of resource aws_subnet list of subnets…
I am completely lost here. I really don’t know what I did wrong or how to proceed to move forward. It was supposed to be easy.
Thanks,
Renato