Skip to content

Conversation

@RoseSecurity
Copy link

@RoseSecurity RoseSecurity commented Dec 1, 2025

Description

  • This resolves issue subnet_ids not optional #374 where subnet_ids was declared as optional but caused an error when not provided. The problem occurred when create_security_group was enabled (the default) with network_mode = "awsvpc" but subnet_ids was empty. The data source attempted to query element(var.subnet_ids, 0) on an empty list, causing a runtime error. The fix adds conditional logic to only create the subnet data source when subnet_ids is 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 the one() function to safely handle the optional data source, preferring an explicit vpc_id when provided. The count expressions avoid null checks to prevent Terraform planning errors when vpc_id is a computed value from another resource. Users can now omit subnet_ids when they don't need the module to create a security group, or provide only subnet_ids to 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?

  • Existing examples validate this functionality
  • I have tested and validated these changes using one or more of the provided examples/* projects
  • I have executed pre-commit run -a on my pull request

Update conditional logic for creating security groups and subnet data sources
to handle cases where vpc_id is null and subnet_ids are provided. Use the
Terraform `one()` function for safer vpc_id selection. This ensures correct
resource creation and avoids potential errors in edge cases.
The conditional for the aws_subnet data resource count no longer checks
if var.vpc_id is null. This simplifies the logic and ensures the subnet
data resource is created whenever a security group is needed and
subnet_ids are provided.
The conditional for the aws_subnet data resource count no longer checks
if var.vpc_id is null. This simplifies the logic and ensures the subnet
data resource is created whenever a security group is needed and
subnet_ids are provided.
@RoseSecurity RoseSecurity changed the title fix(service): improve security group creation logic Fix: improve security group creation logic Dec 1, 2025

data "aws_subnet" "this" {
count = local.create_security_group ? 1 : 0
count = local.create_security_group && length(var.subnet_ids) > 0 ? 1 : 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't work when passing computed values

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very true, should we update the default variable declaration to require the subnet_ids to be provided then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats now how we manage variables - we provide default values so that we can disable all resource creation by simply setting create = true. Terraform doesn't allow us to simply disable resource creation and ignore required values (they aren't required if the resource is not going to be created/executed), which results in us providing a value, any value, to ensure we can disable without forcing users to provide values

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fair and sounds more like the user didn't properly enter the parameters they needed with the configuration they were attempting to construct. Closing this PR now

@RoseSecurity RoseSecurity changed the title Fix: improve security group creation logic fix: improve security group creation logic Dec 1, 2025
@RoseSecurity RoseSecurity changed the title fix: improve security group creation logic fix: Improve security group creation logic Dec 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

subnet_ids not optional

2 participants