Closed oponomarov-tu closed 4 months ago
The error is because the key in some of the for_each
blocks is the az of the vpc_az_map
, and that value is computed with data.aws_subnet.subnet[index].availability_zone
when constructing the map. If you are able to hardcode the AZ in the map it should work. e.g.
locals {
azs = ["us-east-1a", "us-east-1b"]
vpc_az_maps = [
for index, az in local.azs
: {
az = az
route_table_ids = [module.vpc.private_route_table_ids[index]]
public_subnet_id = module.vpc.public_subnets[index]
private_subnet_ids = [module.vpc.private_subnets[index]]
}
]
}
Thank you, @bwhaley! I've got it working eventually with the following:
```hcl variable "az_count" { description = "Number of AZs to use." type = number default = 3 } data "aws_availability_zones" "current" { filter { name = "opt-in-status" values = ["opt-in-not-required"] } } locals { # Searches region for # of AZs to use and takes a slice based on count. # Assume slice is sorted a-z. azs = slice(data.aws_availability_zones.current.names, 0, var.az_count) vpc_name = "example-vpc" vpc_cidr = "172.16.44.0/22" subnets = { private = { netmask = 26 connect_to_public_natgw = false } public = { netmask = 26 nat_gateway_configuration = "none" } } } module "vpc_from_cidr" { count = 1 source = "aws-ia/vpc/aws" version = "~> 4.4.2" name = local.vpc_name az_count = var.az_count cidr_block = local.vpc_cidr subnets = local.subnets } locals { vpc_id = module.vpc_from_cidr[0].vpc_attributes.id private_subnet_attributes = module.vpc_from_cidr[0].private_subnet_attributes_by_az public_subnet_attributes = module.vpc_from_cidr[0].public_subnet_attributes_by_az private_route_table_attributes = module.vpc_from_cidr[0].rt_attributes_by_type_by_az.private private_subnet_ids = [ for subnet_name, subnet_attributes in local.private_subnet_attributes : subnet_attributes.id if startswith(subnet_name, "private") ] } locals { # Construct a map of private/public subnets, private route tables & availability zones. # More: https://github.com/chime/terraform-aws-alternat/blob/91abdd8aedb701659e47d11bed0c15d049bde38d/variables.tf#L194-L202 # This relies on `data "aws_availability_zones"` to ensure Terraform knows # the size of the list during plan phase, otherwise it would have failed. # See: https://github.com/chime/terraform-aws-alternat/issues/104 vpc_config_for_alternat = [ for az in local.azs : { az = az public_subnet_id = local.public_subnet_attributes[az].id private_subnet_ids = [ for k, v in local.private_subnet_attributes : v.id if v.availability_zone == az ] route_table_ids = [ for k, v in local.private_route_table_attributes : v.id if startswith(k, "private/${az}") ] } ] } module "alternat_instances" { count = 1 source = "chime/alternat/aws" version = "0.6.0" nat_instance_type = "t4g.medium" lambda_package_type = "Zip" vpc_id = local.vpc_id vpc_az_maps = local.vpc_config_for_alternat } ```
If you'll find it useful then I can create a new example in the examples
folder. ;)
Sure, an example showing how to use Alternat with aws-ia/vpc/aws
would be excellent! Let's reorganize the examples/
directory to put the current example in a sub dir - let's call it "basic" - and another new dir for your example. Also need to update the path in the integration test.
I'm going to close this issue, but feel free to open a PR with the example if you want. Thanks for sharing your solution here.
Long time no see!
Since recently I've started incorporating AWS-maintained Terraform module for provisioning VPCs mainly due to its better support of AWS IPAM. Trying to construct the
vpc_az_maps
from VPC module's outputs and pass it down to AlterNAT module, I've discovered that Terraform can't plan the changes. Note: using targeted applies and applying VPC module first & AlterNAT module afterwards works w/o issues.I'm looking forward to any ideas how this could be solved in painless manner. Would love to contribute the solution into
examples
if we manage to find one.π‘ CloudPosse had written a nice article about values that can't be determined until apply, leaving it here for those who might come across.
The following Terraform (slightly simplified for demonstration purposes):
.. results in: