hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.84k stars 9.19k forks source link

Use one aws_cloudwatch_event_target for multiple aws_cloudwatch_event_rule - same like "aws events put-targets" #23503

Closed youwalther65 closed 2 years ago

youwalther65 commented 2 years ago

Community Note

Terraform CLI and Terraform AWS Provider Version

$ terraform -v Terraform v1.1.4 on linux_amd64

Affected Resource(s)

AWS proivder resource aws_cloudwatch_event_target resource aws_cloudwatch_event_rule

Following: https://github.com/aws/aws-node-termination-handler#4-create-amazon-eventbridge-rules

I want to create multiple EventBridge rules which use the same SQS as a target target. Using AWS CLI "aws events pu-targets" I can use the same target ID and SQS ARN fo rmultiple event rules. Using Terraform I get an error

Terraform Configuration Files

Please include all Terraform configurations required to reproduce the bug. Bug reports without a functional reproduction may be closed without investigation.

$ cat nth-eventbridge.tf
resource "aws_cloudwatch_event_rule" "karpenter_nth_SpotTermRule" {
  count = var.enable_karpenter ? 1 : 0

  name        = "${local.name}-SpotTermRule"
  description = "Capture EC2 Spot Instance Interruption Warnings"

  event_pattern = <<-EOF
  {
    "source": ["aws.ec2"],
    "detail-type": ["EC2 Spot Instance Interruption Warning"]
  }
EOF
}

resource "aws_cloudwatch_event_target" "karpenter_nth_target1" {
  count = var.enable_karpenter ? 1 : 0

  rule      = aws_cloudwatch_event_rule.karpenter_nth_SpotTermRule[0].name
  target_id = "SendToSQS1"
  arn       = aws_sqs_queue.karpenter_nth_queue[0].arn
}

resource "aws_cloudwatch_event_rule" "karpenter_nth_RebalanceRule" {
  count = var.enable_karpenter ? 1 : 0

  name        = "${local.name}-SpotTermRule"
  description = "Capture EC2 Instance Rebalance Recommendations"

  event_pattern = <<-EOF
  {
    "source": ["aws.ec2"],
    "detail-type": ["EC2 Instance Rebalance Recommendation"]
  }
EOF
}

resource "aws_cloudwatch_event_target" "karpenter_nth_target2" {
  count = var.enable_karpenter ? 1 : 0

  rule      = aws_cloudwatch_event_rule.karpenter_nth_RebalanceRule[0].name
  target_id = "SendToSQS2"
  arn       = aws_sqs_queue.karpenter_nth_queue[0].arn
}

resource "aws_cloudwatch_event_rule" "karpenter_nth_InstanceStateChangeRule" {
  count = var.enable_karpenter ? 1 : 0

  name        = "${local.name}-SpotTermRule"
  description = "Capture EC2 Instance State-change Notifications"

  event_pattern = <<-EOF
  {
    "source": ["aws.ec2"],
    "detail-type": ["EC2 Instance State-change Notification"]
  }
EOF
}

resource "aws_cloudwatch_event_target" "karpenter_nth_target3" {
  count = var.enable_karpenter ? 1 : 0

  rule      = aws_cloudwatch_event_rule.karpenter_nth_InstanceStateChangeRule[0].name
  target_id = "SendToSQS3"
  arn       = aws_sqs_queue.karpenter_nth_queue[0].arn
}

Expected Behavior

Using same target for multiple EventBridge rules

Actual Behavior

error message ā•· ā”‚ Error: error creating EventBridge Rule (git-eks-demo-ipv4-SpotTermRule): ConcurrentModificationException: com.amazon.aws.platform.tagris.client.exception.TagrisClientException: A resource with the same resourceName but a different internalId already exists: 86bbb8d2-b6a5-39d6-5813-5474a616804d ā”‚ ā”‚ with aws_cloudwatch_event_rule.karpenter_nth_RebalanceRule[0], ā”‚ on nth-eventbridge.tf line 23, in resource "aws_cloudwatch_event_rule" "karpenter_nth_RebalanceRule": ā”‚ 23: resource "aws_cloudwatch_event_rule" "karpenter_nth_RebalanceRule" { ā”‚

justinretzolk commented 2 years ago

Hey @youwalther65 šŸ‘‹ Thank you for taking the time to raise this! Based on the error logged, I suspect this is due to your having multiple aws_cloudwatch_event_rule resources that have the same value for the name argument. Can you try setting the name argument to a unique value and test to verify that resolves the issue?

youwalther65 commented 2 years ago

Hey @justinretzolk thank you very much, appreciate your fast response AND resolution. Sorry for that - never codify in a call and have someone look at your code if you are not sure ;-) Issue solved, worked great , thx again

github-actions[bot] commented 2 years ago

I'm going to lock this issue 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 similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.