diff --git a/aws/resource_aws_cloudwatch_event_target.go b/aws/resource_aws_cloudwatch_event_target.go index afa5e84fdcf..1678bc645ca 100644 --- a/aws/resource_aws_cloudwatch_event_target.go +++ b/aws/resource_aws_cloudwatch_event_target.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "math" "regexp" "github.com/hashicorp/terraform/helper/resource" @@ -80,6 +81,26 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { }, }, }, + + "ecs_target": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "task_count": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, math.MaxInt32), + }, + "task_definition_arn": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(1, 1600), + }, + }, + }, + }, }, } } @@ -163,6 +184,12 @@ func resourceAwsCloudWatchEventTargetRead(d *schema.ResourceData, meta interface } } + if t.EcsParameters != nil { + if err := d.Set("ecs_target", flattenAwsCloudWatchEventTargetEcsParameters(t.EcsParameters)); err != nil { + return fmt.Errorf("[DEBUG] Error setting ecs_target error: %#v", err) + } + } + return nil } @@ -245,6 +272,9 @@ func buildPutTargetInputStruct(d *schema.ResourceData) *events.PutTargetsInput { if v, ok := d.GetOk("run_command_targets"); ok { e.RunCommandParameters = expandAwsCloudWatchEventTargetRunParameters(v.([]interface{})) } + if v, ok := d.GetOk("ecs_target"); ok { + e.EcsParameters = expandAwsCloudWatchEventTargetEcsParameters(v.([]interface{})) + } input := events.PutTargetsInput{ Rule: aws.String(d.Get("rule").(string)), @@ -275,6 +305,17 @@ func expandAwsCloudWatchEventTargetRunParameters(config []interface{}) *events.R return command } +func expandAwsCloudWatchEventTargetEcsParameters(config []interface{}) *events.EcsParameters { + ecsParameters := &events.EcsParameters{} + for _, c := range config { + param := c.(map[string]interface{}) + ecsParameters.TaskCount = aws.Int64(int64(param["task_count"].(int))) + ecsParameters.TaskDefinitionArn = aws.String(param["task_definition_arn"].(string)) + } + + return ecsParameters +} + func flattenAwsCloudWatchEventTargetRunParameters(runCommand *events.RunCommandParameters) []map[string]interface{} { result := make([]map[string]interface{}, 0) @@ -289,3 +330,10 @@ func flattenAwsCloudWatchEventTargetRunParameters(runCommand *events.RunCommandP return result } +func flattenAwsCloudWatchEventTargetEcsParameters(ecsParameters *events.EcsParameters) []map[string]interface{} { + config := make(map[string]interface{}) + config["task_count"] = *ecsParameters.TaskCount + config["task_definition_arn"] = *ecsParameters.TaskDefinitionArn + result := []map[string]interface{}{config} + return result +} diff --git a/aws/resource_aws_cloudwatch_event_target_test.go b/aws/resource_aws_cloudwatch_event_target_test.go index 044a32e6794..3f8c6e370c5 100644 --- a/aws/resource_aws_cloudwatch_event_target_test.go +++ b/aws/resource_aws_cloudwatch_event_target_test.go @@ -108,6 +108,25 @@ func TestAccAWSCloudWatchEventTarget_ssmDocument(t *testing.T) { }) } +func TestAccAWSCloudWatchEventTarget_ecs(t *testing.T) { + var target events.Target + rName := acctest.RandomWithPrefix("tf_ecs_target") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudWatchEventTargetConfigEcs(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target), + ), + }, + }, + }) +} + func testAccCheckCloudWatchEventTargetExists(n string, rule *events.Target) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -353,3 +372,85 @@ resource "aws_iam_role_policy" "test_policy" { EOF }`, rName, rName, rName, rName) } + +func testAccAWSCloudWatchEventTargetConfigEcs(rName string) string { + return fmt.Sprintf(` +resource "aws_cloudwatch_event_rule" "schedule" { + name = "%s" + description = "schedule_ecs_test" + + schedule_expression = "rate(5 minutes)" +} + +resource "aws_cloudwatch_event_target" "test" { + arn = "${aws_ecs_cluster.test.id}" + rule = "${aws_cloudwatch_event_rule.schedule.id}" + role_arn = "${aws_iam_role.test_role.arn}" + + ecs_target { + task_count = 1 + task_definition_arn = "${aws_ecs_task_definition.task.arn}" + } +} + +resource "aws_iam_role" "test_role" { + name = "%s" + + assume_role_policy = <