diff --git a/builtin/providers/aws/resource_aws_cloudwatch_event_target.go b/builtin/providers/aws/resource_aws_cloudwatch_event_target.go index 031090c5b..cb32596b3 100644 --- a/builtin/providers/aws/resource_aws_cloudwatch_event_target.go +++ b/builtin/providers/aws/resource_aws_cloudwatch_event_target.go @@ -5,6 +5,7 @@ import ( "log" "regexp" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "github.com/aws/aws-sdk-go/aws" @@ -29,7 +30,7 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { "target_id": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, ForceNew: true, ValidateFunc: validateCloudWatchEventTargetId, }, @@ -60,7 +61,13 @@ func resourceAwsCloudWatchEventTargetCreate(d *schema.ResourceData, meta interfa conn := meta.(*AWSClient).cloudwatcheventsconn rule := d.Get("rule").(string) - targetId := d.Get("target_id").(string) + + var targetId string + if v, ok := d.GetOk("target_id"); ok { + targetId = v.(string) + } else { + targetId = resource.UniqueId() + } input := buildPutTargetInputStruct(d) log.Printf("[DEBUG] Creating CloudWatch Event Target: %s", input) diff --git a/builtin/providers/aws/resource_aws_cloudwatch_event_target_test.go b/builtin/providers/aws/resource_aws_cloudwatch_event_target_test.go index 56fffdeab..fef35d94e 100644 --- a/builtin/providers/aws/resource_aws_cloudwatch_event_target_test.go +++ b/builtin/providers/aws/resource_aws_cloudwatch_event_target_test.go @@ -42,6 +42,27 @@ func TestAccAWSCloudWatchEventTarget_basic(t *testing.T) { }) } +func TestAccAWSCloudWatchEventTarget_missingTargetId(t *testing.T) { + var target events.Target + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSCloudWatchEventTargetConfigMissingTargetId, + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.moobar", &target), + resource.TestCheckResourceAttr("aws_cloudwatch_event_target.moobar", "rule", "tf-acc-cw-event-rule-basic"), + resource.TestMatchResourceAttr("aws_cloudwatch_event_target.moobar", "arn", regexp.MustCompile(":tf-acc-moon$")), + testAccCheckTargetIdExists("aws_cloudwatch_event_target.target_id"), + ), + }, + }, + }) +} + func TestAccAWSCloudWatchEventTarget_full(t *testing.T) { var target events.Target @@ -105,6 +126,17 @@ func testAccCheckAWSCloudWatchEventTargetDestroy(s *terraform.State) error { return nil } +func testAccCheckTargetIdExists(targetId string) resource.TestCheckFunc { + return func(s *terraform.State) error { + _, ok := s.RootModule().Resources[targetId] + if !ok { + return fmt.Errorf("Not found: %s", targetId) + } + + return nil + } +} + var testAccAWSCloudWatchEventTargetConfig = ` resource "aws_cloudwatch_event_rule" "foo" { name = "tf-acc-cw-event-rule-basic" @@ -122,6 +154,22 @@ resource "aws_sns_topic" "moon" { } ` +var testAccAWSCloudWatchEventTargetConfigMissingTargetId = ` +resource "aws_cloudwatch_event_rule" "foo" { + name = "tf-acc-cw-event-rule-basic" + schedule_expression = "rate(1 hour)" +} + +resource "aws_cloudwatch_event_target" "moobar" { + rule = "${aws_cloudwatch_event_rule.foo.name}" + arn = "${aws_sns_topic.moon.arn}" +} + +resource "aws_sns_topic" "moon" { + name = "tf-acc-moon" +} +` + var testAccAWSCloudWatchEventTargetConfigModified = ` resource "aws_cloudwatch_event_rule" "foo" { name = "tf-acc-cw-event-rule-basic"