Place resources correctly when subnet ids are only known at apply time - #230
Open
patrickchugh wants to merge 1 commit into
Open
patrickchugh wants to merge 1 commit into
patrickchugh wants to merge 1 commit into
Conversation
TerraVision plans against empty local state, so no subnet has an id yet. Three places mishandled that: * expand_autoscaling_groups_to_subnets matched subnets by id and treated an unknown id as a wildcard, so an autoscaling group set to aws_subnet.private[*].id was copied into the public and data subnets too. It now expands into the subnets the expression names, and falls back to id matching only when no subnet resource is named. * _disambiguate_instances kept every instance for a splat but dropped a for expression over the whole resource ([for s in aws_subnet.this : s.id], the only way to list for_each instances) as ambiguous. Both are one-to-many and are now treated alike. * move_to_vpc_parent runs before availability zone boxes are inserted, so it looked for an AZ between subnet and VPC, found none, and left DB subnet groups and Lambda functions unlinked from both. It now accepts a VPC that is the subnet's direct parent. The secretsmanager-rds snapshot changes accordingly: its DB subnet group is now inside the VPC. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type of Change
What and why
TerraVision plans Terraform against empty local state, so no subnet has an id at plan time. Three places mishandled that, and together they made any environment whose subnets come from
countorfor_eachdraw wrongly: an autoscaling group was copied into every subnet of the VPC, and a database behind a DB subnet group was drawn outside the VPC.expand_autoscaling_groups_to_subnetsinmodules/resource_handlers_aws.pymatched subnets by id and treated an unknown id as a wildcard. It now expands into the subnets thevpc_zone_identifierexpression names (aws_subnet.private[*].id,[for s in aws_subnet.private : s.id], or an explicit list), and falls back to the old id matching only when no subnet resource is named, for example a data source._disambiguate_instancesinmodules/graphmaker.pyalready kept every instance for a splat, but[for s in aws_subnet.this : s.id], the only way to listfor_eachinstances, was rejected with "ambiguous for_each reference, connection omitted". Both are one-to-many and are now treated alike. A loop over one instance (aws_subnet.this[each.key]) is still not treated as one-to-many.move_to_vpc_parentinmodules/resource_transformers.pyruns before availability zone boxes are inserted, so it looked for an AZ between subnet and VPC, found none, and had already cut the subnet link. It now accepts a VPC that is the subnet's direct parent. This also affects Lambda functions in a VPC, which use the same transformation.The
expected-secretsmanager-rds.jsonsnapshot changes in one line: its DB subnet group is now a child of the VPC rather than floating outside it.Verified on two minimal repros (count with splat, for_each with for expression) replayed from plan files, and on a larger dev/prod example: the autoscaling group now sits only in the private subnets, the ElastiCache cluster and DB subnet group are inside the VPC, and the warnings are gone.
Known remaining issue, not addressed here: an RDS instance whose security group is the only thing linking it to the VPC can still end up outside it.
redirect_to_security_groupcorrectly swaps the DB subnet group for the security group inside the VPC, butaws_handle_sg, which runs afterwards, deletes that security group without creating a per-member copy for the RDS instance. The old snapshot already showed this. It needs a separate change to the security group handler.Tests:
tests/test_subnet_placement.py(new): autoscaling expansion for splat, for expression and explicit list; fallback when no subnet is named; module-prefixed nodes;move_to_vpc_parentwith and without an AZ node.tests/test_foreach_matching.py: for expression cases added to the one-to-many parametrisation, including a loop over one instance that must stay ambiguous.pytest tests, slow tests included): 2,294 passed, 1 failed. The failure istest_live_sourcefor the wordpress_fargate example, which looks up an existing Route 53 zone with a data source that does not exist in the account the test ran against. It fails identically without this change.Checklist
All Submissions:
AI Assistance Declaration
Checklist for Changes to Core Features:
🤖 Generated with Claude Code