diff --git a/builtin/providers/aws/resource_aws_cloudwatch_event_target.go b/builtin/providers/aws/resource_aws_cloudwatch_event_target.go index 727a17192e59..031090c5bf28 100644 --- a/builtin/providers/aws/resource_aws_cloudwatch_event_target.go +++ b/builtin/providers/aws/resource_aws_cloudwatch_event_target.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" events "github.com/aws/aws-sdk-go/service/cloudwatchevents" ) @@ -61,9 +62,6 @@ func resourceAwsCloudWatchEventTargetCreate(d *schema.ResourceData, meta interfa rule := d.Get("rule").(string) targetId := d.Get("target_id").(string) - id := rule + "-" + targetId - d.SetId(id) - input := buildPutTargetInputStruct(d) log.Printf("[DEBUG] Creating CloudWatch Event Target: %s", input) out, err := conn.PutTargets(input) @@ -76,6 +74,9 @@ func resourceAwsCloudWatchEventTargetCreate(d *schema.ResourceData, meta interfa out.FailedEntries) } + id := rule + "-" + targetId + d.SetId(id) + log.Printf("[INFO] CloudWatch Event Target %q created", d.Id()) return resourceAwsCloudWatchEventTargetRead(d, meta) @@ -94,6 +95,15 @@ func resourceAwsCloudWatchEventTargetRead(d *schema.ResourceData, meta interface d.SetId("") return nil } + if awsErr, ok := err.(awserr.Error); ok { + // This should never happen, but it's useful + // for recovering from https://github.com/hashicorp/terraform/issues/5389 + if awsErr.Code() == "ValidationException" { + log.Printf("[WARN] Removing CloudWatch Event Target %q because it never existed.", d.Id()) + d.SetId("") + return nil + } + } return err } log.Printf("[DEBUG] Found Event Target: %s", t) diff --git a/website/source/docs/providers/aws/r/cloudwatch_event_target.html.markdown b/website/source/docs/providers/aws/r/cloudwatch_event_target.html.markdown index e7555da9b7b6..20a94b31e932 100644 --- a/website/source/docs/providers/aws/r/cloudwatch_event_target.html.markdown +++ b/website/source/docs/providers/aws/r/cloudwatch_event_target.html.markdown @@ -15,7 +15,7 @@ Provides a CloudWatch Event Target resource. ``` resource "aws_cloudwatch_event_target" "yada" { target_id = "Yada" - rule = "${aws_cloudwatch_event_rule.console.arn}" + rule = "${aws_cloudwatch_event_rule.console.name}" arn = "${aws_kinesis_stream.test_stream.arn}" }