terraform-aws-modules / terraform-aws-security-group

Terraform module to create AWS Security Group resources 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/security-group/aws
Other
562 stars 1.08k forks source link

chore!: default using optional keywork, merge rule map - v5.0.0 refactor #282

Closed Remy-Mollandin-SK5 closed 1 year ago

Remy-Mollandin-SK5 commented 1 year ago

Description

For now, module have been using a resource statement for each rule type (source_security_group, cidr_blocks, ipv6_cidr_blocks and self).

Using terraform 1.3 optional keywork with default, we should be able to simplify this logic and merge resources in a single map. It should also provide a more clear and concise source code.

  type = list(object({
    description              = optional(string, "Computed Ingress Rule")
    from_port                = optional(number)
    rule                     = optional(string, "_")
    to_port                  = optional(number)
    protocol                 = optional(string)
    cidr_blocks              = optional(list(string))
    ipv6_cidr_blocks         = optional(list(string))
    prefix_list_ids          = optional(list(string))
    self                     = optional(bool)
    source_security_group_id = optional(string)
  }))

Validation are enabled at the variable level to ensure at least one required field is set:

  validation {
    condition = alltrue([
      for rule in var.computed_ingress_with_custom_blocks : (
        rule.cidr_blocks != null ||
        rule.ipv6_cidr_blocks != null ||
        rule.prefix_list_ids != null ||
        rule.self != null ||
        rule.source_security_group_id != null
      )
    ])
    error_message = "Computed ingress rule must have at least one of cidr_blocks, ipv6_cidr_blocks, prefix_list_ids, self or source_security_group_id defined."
  }

It moves the logic of rule default values from:

lookup(
    var.ingress_with_self[count.index],
    "from_port",
    var.rules[lookup(var.ingress_with_self[count.index], "rule", "_")][0],
  )

to:

  from_port = coalesce(
    var.ingress_with_custom_blocks[count.index].from_port,
    var.rules[var.ingress_with_custom_blocks[count.index].rule][0],
  )

It also gives a solution to #281 as prefix_list_ids are now handled at the rule level.

Motivation and Context

Making the module easier to use and less error prone by merging variables that are closely related. It also increase the maintainability as most of the default logic is now handled directly by Terraform.

Breaking Changes

This is breaking the current usage as the rule logic is now handled by a new variable. All the old ones is going to be removed and there is no possibility to handle the changes using moved statements.

It would require a new major release and Terraform >= 1.3.

How Has This Been Tested?

pre-commit is installed and hooks enabled. However the pre-commit run -a command hang on terraform validate. Hooks during commit are clear.

github-actions[bot] commented 1 year ago

This PR has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this PR will be closed in 10 days

github-actions[bot] commented 1 year ago

This PR was automatically closed because of stale in 10 days

github-actions[bot] commented 1 year ago

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.