I’m creating terraform modules and working fine. However, I try to replicate using terragrunt as a learning process. Can someone point out the terragrunt dependency mock_outputs please for a multiple indexes.
Terraform
data “aws_availability_zones” “avail” {}
resource “aws_subnet” “subnets” {
for_each = {
for v, subnet in var.subnet_cidr : v => subnet
}
vpc_id = var.vpc_id
cidr_block = each.value
map_public_ip_on_launch = var.map_public_ip_on_launch
availability_zone = element(data.aws_availability_zones.avail.names, each.key)
tags = merge(
{
“Name” = format(“%s%d”, var.subnet_name, each.key + 1)
},
var.additional_tags,
var.eks_additional_tags,
)
}
output “subnet_id” {
value = values(aws_subnet.subnets).*.id
}
output “subnet_cidr_block” {
value = values(aws_subnet.subnets).*.cidr_block
}
resource “aws_route” “route” {
route_table_id = var.route_table_id
destination_cidr_block = var.destination_cidr_block
gateway_id = var.gateway_id
}
output “aws_route_id” {
value = aws_route.route.id
}
module “public-subnet” {
depends_on = [module.internet-gw]
source = “../../modules/subnets”
count = var.config.development_public_subnet.enabled ? 1 : 0
vpc_id = module.vpc[0].vpc_id
subnet_name = var.config.development_public_subnet.subnet_name
subnet_cidr = var.config.development_public_subnet.subnet_cidr
additional_tags = var.additional_tags
eks_additional_tags = var.eks_public_additional_tags
}
module “public-route-table-association” {
depends_on = [module.public-subnet-route]
source = “../../modules/route_table_assoc”
count = var.config.development_public_route_table_assoc.enabled ? 1 : 0
subnet_id = module.public-subnet[0].subnet_id[*]
route_table_id = module.public-subnet-route-table[0].route_table_id
}
Terragrunt
include “root” {
path = find_in_parent_folders(“root.hcl”)
}
terraform {
source = “../../../modules/route_table_assoc”
}
locals {
global_vars = read_terragrunt_config(find_in_parent_folders(“global.hcl”))
project_name = local.global_vars.locals.project_name
}
dependency “vpc” {
config_path = “../vpc”
mock_outputs = {
vpc_id = “mock-vpc-id”
}
}
dependency “internet_gw” {
config_path = “../internet_gw”
mock_outputs = {
internet_gw_id = “mock-internet-gw-id”
}
}
dependency “public_subnets” {
config_path = “../public_subnets”
mock_outputs = {
subnet_id = “mock-subnet-id”
}
}
dependency “public_route_table” {
config_path = “../public_route_table”
mock_outputs = {
route_table_id = “mock-route-table-id”
}
}
dependency “public_route” {
config_path = “../public_route”
mock_outputs = {
aws_route_id = “mock-aws-route-id”
}
}
inputs = {
route_table_id = dependency.public_route_table.outputs.route_table_id
subnet_id = dependency.public_subnets.outputs.subnet_id[*]
}
terragrunt doc example
dependency "vpc" {
config_path = "../vpc"
# Configure mock outputs for the `validate` command that are returned when there are no outputs available (e.g the
# module hasn't been applied yet.
mock_outputs_allowed_terraform_commands = ["validate"]
mock_outputs = {
vpc_id = "fake-vpc-id"
}
}
Can I map each subnet_id index i.e
subnet_cidr = [dependency.public_subnets.outputs.subnet_cidr[0], dependency.public_subnets.outputs.subnet_cidr[1]]
Can I amend and add as follows
dependency “public_subnets” {
config_path = “../public_subnets”
mock_outputs = {
subnet_id = “mock-subnet-id”
subnet_cidr = “mock-subnet-cidr”
}
}
If I map as follow, then I will get an error
subnet_id = dependency.public_subnets.outputs.subnet_id[0]
How do I overcome this. Using local to call the terraform output or what is a workaround this issue?
Thanks,