fix: Improve security group creation logic #375
Closed
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.
Description
subnet_idswas declared as optional but caused an error when not provided. The problem occurred whencreate_security_groupwas enabled (the default) withnetwork_mode = "awsvpc"butsubnet_idswas empty. The data source attempted to queryelement(var.subnet_ids, 0)on an empty list, causing a runtime error. The fix adds conditional logic to only create the subnet data source whensubnet_idsis non-empty, and only creates the security group when subnet information is available to derive or use the VPC ID. The VPC ID reference now uses theone()function to safely handle the optional data source, preferring an explicitvpc_idwhen provided. The count expressions avoid null checks to prevent Terraform planning errors whenvpc_idis a computed value from another resource. Users can now omitsubnet_idswhen they don't need the module to create a security group, or provide onlysubnet_idsto have the VPC ID automatically derived, making the module's behavior consistent with the variable's optional declaration.Motivation and Context
How Has This Been Tested?
examples/*projectspre-commit run -aon my pull request