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

cloudwatch_event_target: Add ecs_target #977

Merged
merged 1 commit into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions aws/resource_aws_cloudwatch_event_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"math"
"regexp"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -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),
},
},
},
},
},
}
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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)

Expand All @@ -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
}
101 changes: 101 additions & 0 deletions aws/resource_aws_cloudwatch_event_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "events.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

resource "aws_iam_role_policy" "test_policy" {
name = "%s"
role = "${aws_iam_role.test_role.id}"

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecs:RunTask"
],
"Resource": [
"*"
]
}
]
}
EOF
}

resource "aws_ecs_cluster" "test" {
name = "%s"
}

resource "aws_ecs_task_definition" "task" {
family = "%s"
container_definitions = <<EOF
[
{
"name": "first",
"image": "service-first",
"cpu": 10,
"memory": 512,
"essential": true
}
]
EOF
}`, rName, rName, rName, rName, rName)
}
6 changes: 6 additions & 0 deletions website/docs/r/cloudwatch_event_target.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ The following arguments are supported:
that is used for extracting part of the matched event when passing it to the target.
* `role_arn` - (Optional) The Amazon Resource Name (ARN) of the IAM role to be used for this target when the rule is triggered.
* `run_command_targets` - (Optional) Parameters used when you are using the rule to invoke Amazon EC2 Run Command. Documented below. A maximum of 5 are allowed.
* `ecs_target` - (Optional) Parameters used when you are using the rule to invoke Amazon ECS Task. Documented below. A maximum of 1 are allowed.

`run_command_parameters` support the following:

* `key` - (Required) Can be either `tag:tag-key` or `InstanceIds`.
* `values` - (Required) If Key is `tag:tag-key`, Values is a list of tag values. If Key is `InstanceIds`, Values is a list of Amazon EC2 instance IDs.

`ecs_target` support the following:

* `task_count` - (Optional) The number of tasks to create based on the TaskDefinition. The default is 1.
* `task_definition_arn` - (Required) The ARN of the task definition to use if the event target is an Amazon ECS cluster.