Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventBridge: aws_cloudwatch_event_target being duplicated #16394

Closed
abonawit opened this issue Nov 23, 2020 · 9 comments · Fixed by #16484
Closed

EventBridge: aws_cloudwatch_event_target being duplicated #16394

abonawit opened this issue Nov 23, 2020 · 9 comments · Fixed by #16484
Assignees
Labels
bug Addresses a defect in current functionality. service/cloudwatch Issues and PRs that pertain to the cloudwatch service.
Milestone

Comments

@abonawit
Copy link

abonawit commented Nov 23, 2020

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform CLI and Terraform AWS Provider Version

Terraform v0.12.18
provider.aws v3.16.0

Affected Resource(s)

  • aws_cloudwatch_event_target

Terraform Configuration Files

resource "aws_cloudwatch_event_target" "my-test-target" {
  target_id = "my-test-target"
    rule = aws_cloudwatch_event_rule.my-test-rule.name
    arn = "arn:aws:lambda:xx-xxxx-x:xxxxxxxxxxxxxx:function:my-test-lambda"

    event_bus_name = "my-custom-bus"
}

Debug Output

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.instances.module.my-test-rules.aws_cloudwatch_event_target.my-test-target will be created
  + resource "aws_cloudwatch_event_target" "my-test-target" {
      + arn            = "arn:aws:lambda:xx-xxxx-x:xxxxxxxxxxxxxx:function:my-test-lambda"
      + event_bus_name = "my-custom-bus"
      + id             = (known after apply)
      + rule           = "my-test-rule"
      + target_id      = "my-test-target"
    }

Expected Behavior

No new targets would be created since they were already created in the rule on a previous terraform apply.

Actual Behavior

A new target was created in the rule even though no new changes were made to the target in the terraform.

Steps to Reproduce

  1. Create eventbridge rule via terraform
  2. Add target to rule via terraform
  3. Plan and apply rule and target
  4. Run "terraform init" again.
  5. Run "terraform plan" without making any changes to the eventbridge rule target
  6. Output will show a new target resource to create
  7. Terraform apply adds new duplicate target while leaving existing target untouched.

Important Factoids

This behavior occurred in both scenarios where target_id was populated, and where it was omitted.

@ghost ghost added service/cloudwatchevents service/cloudwatch Issues and PRs that pertain to the cloudwatch service. labels Nov 23, 2020
@github-actions github-actions bot added the needs-triage Waiting for first response or review from a maintainer. label Nov 23, 2020
@bflad
Copy link
Contributor

bflad commented Nov 25, 2020

Hi @abonawit 👋 Thank you for opening this and sorry you ran into trouble here.

In your reproduction steps you mention running terraform init again. Was the aws_cloudwatch_event_target resource originally applied with version 3.14.0 of the Terraform AWS Provider from November 06, 2020?

@bflad bflad added waiting-response Maintainers are waiting on response from community or contributor. and removed needs-triage Waiting for first response or review from a maintainer. labels Nov 25, 2020
@abonawit
Copy link
Author

Hi @bflad yes the original issue was found in the 3.14.0 version. I had changed the version before posting the issue and forgot to change it back.

@ghost ghost removed waiting-response Maintainers are waiting on response from community or contributor. labels Nov 26, 2020
@bflad
Copy link
Contributor

bflad commented Nov 30, 2020

Thank you for that additional information. We should be able to add a patch on top of the resource logic that is causing this issue. In the meantime, if you need to plan/apply your configuration on the newer version, you should be able to use terraform state rm and terraform import to workaround the issue. If you need assistance with that process, please reach out.

@bflad bflad added the bug Addresses a defect in current functionality. label Nov 30, 2020
@abonawit
Copy link
Author

Thanks @bflad. Any estimation on time frame for the patch?

bflad added a commit that referenced this issue Nov 30, 2020
…sthrough custom event_bus_name in v0 state upgrade

Reference: #16394

If a configuration was applied with version 3.14.0, the `event_bus_name` attribute could have a custom value and the state upgrader function would previously not pass it through.

Previous output from new unit test:

```
--- FAIL: TestResourceAwsCloudWatchEventTargetStateUpgradeV0EventBusName (0.00s)
    resource_aws_cloudwatch_event_target_migrate_test.go:66:

        expected:

        map[string]interface {}{"arn":"arn:aws:test:us-east-1:123456789012:test", "event_bus_name":"testbus", "rule":"testrule", "target_id":"testtargetid"}

        got:

        map[string]interface {}{"arn":"arn:aws:test:us-east-1:123456789012:test", "event_bus_name":"default", "rule":"testrule", "target_id":"testtargetid"}
```

Given this configuration:

```terraform
terraform {
  required_providers {
    aws = "3.14.0"
  }
  required_version = "0.12.29"
}

provider "aws" {
  region = "us-east-2"
}

resource "aws_cloudwatch_event_bus" "test" {
  name = "16394-test"
}

resource "aws_cloudwatch_event_rule" "test" {
  event_bus_name = aws_cloudwatch_event_bus.test.name
  event_pattern = jsonencode({
    source = ["aws.ec2"]
  })
  name = "16394-test"
}

resource "aws_cloudwatch_event_target" "test" {
  arn            = aws_sns_topic.test.arn
  event_bus_name = aws_cloudwatch_event_bus.test.name
  rule           = aws_cloudwatch_event_rule.test.name
  target_id      = "16394-test"
}

resource "aws_sns_topic" "test" {
  name = "16394-test"
}
```

Output from console:

```console
$ terraform init
...
$ terraform apply
...
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
# edit provider version to 3.18.0
$ terraform init
...
$ terraform apply
...
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_cloudwatch_event_target.test will be created
  + resource "aws_cloudwatch_event_target" "test" {
      + arn            = "arn:aws:sns:us-east-2:--OMITTED--:16394-test"
      + event_bus_name = "16394-test"
      + id             = (known after apply)
      + rule           = "16394-test"
      + target_id      = "16394-test"
    }

Plan: 1 to add, 0 to change, 0 to destroy.
# swap in built Terraform AWS Provider binary
$ terraform apply
...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
```

Output from acceptance testing:

```
--- PASS: TestAccAWSCloudWatchEventTarget_basic (35.74s)
--- PASS: TestAccAWSCloudWatchEventTarget_batch (140.33s)
--- PASS: TestAccAWSCloudWatchEventTarget_disappears (17.51s)
--- PASS: TestAccAWSCloudWatchEventTarget_ecs (28.63s)
--- PASS: TestAccAWSCloudWatchEventTarget_ecsWithBlankTaskCount (31.25s)
--- PASS: TestAccAWSCloudWatchEventTarget_EventBusName (32.01s)
--- PASS: TestAccAWSCloudWatchEventTarget_full (59.07s)
--- PASS: TestAccAWSCloudWatchEventTarget_GeneratedTargetId (16.62s)
--- PASS: TestAccAWSCloudWatchEventTarget_input_transformer (47.34s)
--- PASS: TestAccAWSCloudWatchEventTarget_inputTransformerJsonString (38.99s)
--- PASS: TestAccAWSCloudWatchEventTarget_kinesis (59.09s)
--- PASS: TestAccAWSCloudWatchEventTarget_sqs (16.71s)
--- PASS: TestAccAWSCloudWatchEventTarget_ssmDocument (18.77s)
```
@bflad
Copy link
Contributor

bflad commented Nov 30, 2020

Fix submitted: #16484

bflad added a commit that referenced this issue Dec 3, 2020
…sthrough custom event_bus_name in v0 state upgrade (#16484)

Reference: #16394

If a configuration was applied with version 3.14.0, the `event_bus_name` attribute could have a custom value and the state upgrader function would previously not pass it through.

Previous output from new unit test:

```
--- FAIL: TestResourceAwsCloudWatchEventTargetStateUpgradeV0EventBusName (0.00s)
    resource_aws_cloudwatch_event_target_migrate_test.go:66:

        expected:

        map[string]interface {}{"arn":"arn:aws:test:us-east-1:123456789012:test", "event_bus_name":"testbus", "rule":"testrule", "target_id":"testtargetid"}

        got:

        map[string]interface {}{"arn":"arn:aws:test:us-east-1:123456789012:test", "event_bus_name":"default", "rule":"testrule", "target_id":"testtargetid"}
```

Given this configuration:

```terraform
terraform {
  required_providers {
    aws = "3.14.0"
  }
  required_version = "0.12.29"
}

provider "aws" {
  region = "us-east-2"
}

resource "aws_cloudwatch_event_bus" "test" {
  name = "16394-test"
}

resource "aws_cloudwatch_event_rule" "test" {
  event_bus_name = aws_cloudwatch_event_bus.test.name
  event_pattern = jsonencode({
    source = ["aws.ec2"]
  })
  name = "16394-test"
}

resource "aws_cloudwatch_event_target" "test" {
  arn            = aws_sns_topic.test.arn
  event_bus_name = aws_cloudwatch_event_bus.test.name
  rule           = aws_cloudwatch_event_rule.test.name
  target_id      = "16394-test"
}

resource "aws_sns_topic" "test" {
  name = "16394-test"
}
```

Output from console:

```console
$ terraform init
...
$ terraform apply
...
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
# edit provider version to 3.18.0
$ terraform init
...
$ terraform apply
...
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_cloudwatch_event_target.test will be created
  + resource "aws_cloudwatch_event_target" "test" {
      + arn            = "arn:aws:sns:us-east-2:--OMITTED--:16394-test"
      + event_bus_name = "16394-test"
      + id             = (known after apply)
      + rule           = "16394-test"
      + target_id      = "16394-test"
    }

Plan: 1 to add, 0 to change, 0 to destroy.
# swap in built Terraform AWS Provider binary
$ terraform apply
...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
```

Output from acceptance testing:

```
--- PASS: TestAccAWSCloudWatchEventTarget_basic (35.74s)
--- PASS: TestAccAWSCloudWatchEventTarget_batch (140.33s)
--- PASS: TestAccAWSCloudWatchEventTarget_disappears (17.51s)
--- PASS: TestAccAWSCloudWatchEventTarget_ecs (28.63s)
--- PASS: TestAccAWSCloudWatchEventTarget_ecsWithBlankTaskCount (31.25s)
--- PASS: TestAccAWSCloudWatchEventTarget_EventBusName (32.01s)
--- PASS: TestAccAWSCloudWatchEventTarget_full (59.07s)
--- PASS: TestAccAWSCloudWatchEventTarget_GeneratedTargetId (16.62s)
--- PASS: TestAccAWSCloudWatchEventTarget_input_transformer (47.34s)
--- PASS: TestAccAWSCloudWatchEventTarget_inputTransformerJsonString (38.99s)
--- PASS: TestAccAWSCloudWatchEventTarget_kinesis (59.09s)
--- PASS: TestAccAWSCloudWatchEventTarget_sqs (16.71s)
--- PASS: TestAccAWSCloudWatchEventTarget_ssmDocument (18.77s)
```
@bflad bflad self-assigned this Dec 3, 2020
@bflad bflad added this to the v3.20.0 milestone Dec 3, 2020
@bflad
Copy link
Contributor

bflad commented Dec 3, 2020

The fix for the state upgrade has been merged and will release with version 3.20.0 of the Terraform AWS Provider. 👍

@abonawit
Copy link
Author

abonawit commented Dec 3, 2020

Thank you @bflad for your quick work on this! Very much appreciated.

@ghost
Copy link

ghost commented Dec 3, 2020

This has been released in version 3.20.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

@ghost
Copy link

ghost commented Jan 2, 2021

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Jan 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/cloudwatch Issues and PRs that pertain to the cloudwatch service.
Projects
None yet
2 participants