Skip to content

feat: Configurable event patterns for NTH EventBridge rules for all ec2 events#456

Merged
askulkarni2 merged 1 commit intoaws-ia:mainfrom
JaseKoonce:feat/configurable-event-pattern-for-all-events
Jul 24, 2025
Merged

feat: Configurable event patterns for NTH EventBridge rules for all ec2 events#456
askulkarni2 merged 1 commit intoaws-ia:mainfrom
JaseKoonce:feat/configurable-event-pattern-for-all-events

Conversation

@JaseKoonce
Copy link
Contributor

Expands on work done in #454

What does this PR do?

In PR #454 a variable was added to allow the end user to create a pattern on their EC2 Instance-terminate Lifecycle Action eventbridge rule that is scoped to only their desired ASG. Without this, your event rules will receive and send events for every ASG in your account. Expanding on this work, I am re-using the ASG name variable to add patterns to the rest of the ec2 events.

Motivation

  • Seeing errors on our NTH pods about nodes and ASGs it never should have seen events from.

More

  • Yes, I have tested the PR using my local account setup (Provide any test evidence report under Additional Notes)
  • Yes, I ran pre-commit run -a with this PR

For Moderators

  • E2E Test successfully complete before merge?

Additional Notes

plan output:
image
image
image
image
image

@JaseKoonce JaseKoonce requested a review from a team as a code owner July 22, 2025 16:24
Copy link
Contributor

@askulkarni2 askulkarni2 left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

@askulkarni2 askulkarni2 merged commit 2e7f1d3 into aws-ia:main Jul 24, 2025
6 checks passed
@VolodymyrSmahliuk
Copy link

@JaseKoonce , thanks for your work here! I created the custom fix to solve that issue before you've provided these and previous PRs.

Just a suggestion: should we once define the fix in the aws_cloudwatch_event_rule.aws_node_termination_handler instead of propagating it for each ec2_event?

before

resource "aws_cloudwatch_event_rule" "aws_node_termination_handler" {
  for_each = { for k, v in local.aws_node_termination_handler_events : k => v if var.enable_aws_node_termination_handler }

  name_prefix   = "NTH-${each.value.name}-"
  description   = each.value.description
  event_pattern = jsonencode(each.value.event_pattern)

  tags = merge(
    { "ClusterName" : var.cluster_name },
    var.tags,
  )
}

After

resource "aws_cloudwatch_event_rule" "aws_node_termination_handler" {
  for_each = { for k, v in local.aws_node_termination_handler_events : k => v if var.enable_aws_node_termination_handler }

  name_prefix = "NTH-${each.value.name}-"
  description = each.value.description
  event_pattern = jsonencode(merge(each.value.event_pattern,
    try(length(var.aws_node_termination_handler_asg_names)) > 0 ? {
      detail = {
        AutoScalingGroupName = var.aws_node_termination_handler_asg_names
      }
  } : {}))

  tags = merge(
    { "ClusterName" : var.cluster_name },
    var.tags,
  )
}

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.

3 participants