-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Add batch job as a cloudwatch_event_target #4312
Changes from 1 commit
cde797a
b4f7635
47b08e8
74e3f73
ac458d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,34 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { | |
}, | ||
}, | ||
|
||
"batch_target": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"job_definition": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"job_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"array_size": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ValidateFunc: validation.IntBetween(2, 10000), | ||
}, | ||
"job_attempts": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ValidateFunc: validation.IntBetween(1, 10), | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"input_transformer": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
|
@@ -209,6 +237,12 @@ func resourceAwsCloudWatchEventTargetRead(d *schema.ResourceData, meta interface | |
} | ||
} | ||
|
||
if t.BatchParameters != nil { | ||
if err := d.Set("batch_target", flattenAwsCloudWatchEventTargetBatchParameters(t.BatchParameters)); err != nil { | ||
return fmt.Errorf("[DEBUG] Error setting batch_target error: %#v", err) | ||
} | ||
} | ||
|
||
if t.InputTransformer != nil { | ||
if err := d.Set("input_transformer", flattenAwsCloudWatchInputTransformer(t.InputTransformer)); err != nil { | ||
return fmt.Errorf("[DEBUG] Error setting input_transformer error: %#v", err) | ||
|
@@ -299,6 +333,9 @@ func buildPutTargetInputStruct(d *schema.ResourceData) *events.PutTargetsInput { | |
if v, ok := d.GetOk("ecs_target"); ok { | ||
e.EcsParameters = expandAwsCloudWatchEventTargetEcsParameters(v.([]interface{})) | ||
} | ||
if v, ok := d.GetOk("batch_target"); ok { | ||
e.BatchParameters = expandAwsCloudWatchEventTargetBatchParameters(v.([]interface{})) | ||
} | ||
|
||
if v, ok := d.GetOk("input_transformer"); ok { | ||
e.InputTransformer = expandAwsCloudWatchEventTransformerParameters(v.([]interface{})) | ||
|
@@ -344,6 +381,27 @@ func expandAwsCloudWatchEventTargetEcsParameters(config []interface{}) *events.E | |
return ecsParameters | ||
} | ||
|
||
func expandAwsCloudWatchEventTargetBatchParameters(config []interface{}) *events.BatchParameters { | ||
batchParameters := &events.BatchParameters{} | ||
for _, c := range config { | ||
param := c.(map[string]interface{}) | ||
batchParameters.JobDefinition = aws.String(param["job_definition"].(string)) | ||
batchParameters.JobName = aws.String(param["job_name"].(string)) | ||
if v := param["array_size"]; v != nil { | ||
arrayProperties := &events.BatchArrayProperties{} | ||
arrayProperties.Size = aws.Int64(int64(v.(int))) | ||
batchParameters.ArrayProperties = arrayProperties | ||
} | ||
if v := param["job_attempts"]; v != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably need to check against 0 value here:
if v := param["job_attempts"]; v != nil && v.(int) != 0 {
batchParameters.RetryStrategy = &events.BatchRetryStrategy{
Attempts: aws.Int64(int64(v.(int))),
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
retryStrategy := &events.BatchRetryStrategy{} | ||
retryStrategy.Attempts = aws.Int64(int64(v.(int))) | ||
batchParameters.RetryStrategy = retryStrategy | ||
} | ||
} | ||
|
||
return batchParameters | ||
} | ||
|
||
func expandAwsCloudWatchEventTransformerParameters(config []interface{}) *events.InputTransformer { | ||
transformerParameters := &events.InputTransformer{} | ||
|
||
|
@@ -385,6 +443,20 @@ func flattenAwsCloudWatchEventTargetEcsParameters(ecsParameters *events.EcsParam | |
return result | ||
} | ||
|
||
func flattenAwsCloudWatchEventTargetBatchParameters(batchParameters *events.BatchParameters) []map[string]interface{} { | ||
config := make(map[string]interface{}) | ||
config["job_definition"] = aws.StringValue(batchParameters.JobDefinition) | ||
config["job_name"] = aws.StringValue(batchParameters.JobName) | ||
if batchParameters.ArrayProperties != nil { | ||
config["array_size"] = int(aws.Int64Value(batchParameters.ArrayProperties.Size)) | ||
} | ||
if batchParameters.RetryStrategy != nil { | ||
config["job_attempts"] = int(aws.Int64Value(batchParameters.RetryStrategy.Attempts)) | ||
} | ||
result := []map[string]interface{}{config} | ||
return result | ||
} | ||
|
||
func flattenAwsCloudWatchInputTransformer(inputTransformer *events.InputTransformer) []map[string]interface{} { | ||
config := make(map[string]interface{}) | ||
inputPathsMap := make(map[string]string) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,6 +139,26 @@ func TestAccAWSCloudWatchEventTarget_ecs(t *testing.T) { | |
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSCloudWatchEventTarget_batch(t *testing.T) { | ||
var target events.Target | ||
rName := acctest.RandomWithPrefix("tf_batch_target") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSCloudWatchEventTargetConfigBatch(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSCloudWatchEventTarget_input_transformer(t *testing.T) { | ||
var target events.Target | ||
rName := acctest.RandomWithPrefix("tf_input_transformer") | ||
|
@@ -473,6 +493,155 @@ EOF | |
}`, rName, rName, rName, rName, rName) | ||
} | ||
|
||
func testAccAWSCloudWatchEventTargetConfigBatch(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_cloudwatch_event_rule" "cloudwatch_event_rule" { | ||
name = "%[1]s" | ||
description = "schedule_batch_test" | ||
schedule_expression = "rate(5 minutes)" | ||
} | ||
|
||
resource "aws_cloudwatch_event_target" "test" { | ||
arn = "${aws_batch_job_queue.batch_job_queue.arn}" | ||
rule = "${aws_cloudwatch_event_rule.cloudwatch_event_rule.id}" | ||
role_arn = "${aws_iam_role.event_iam_role.arn}" | ||
|
||
batch_target { | ||
job_definition = "${aws_batch_job_definition.batch_job_definition}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
job_name = "%[1]s" | ||
} | ||
} | ||
|
||
resource "aws_iam_role" "event_iam_role" { | ||
name = "event_%[1]s" | ||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "events.amazonaws.com" | ||
} | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
resource "aws_iam_role" "ecs_iam_role" { | ||
name = "ecs_%[1]s" | ||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "ec2.amazonaws.com" | ||
} | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "ecs_policy_attachment" { | ||
role = "${aws_iam_role.ecs_iam_role.name}" | ||
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role" | ||
} | ||
|
||
resource "aws_iam_instance_profile" "iam_instance_profile" { | ||
name = "ecs_%[1]s" | ||
role = "${aws_iam_role.ecs_iam_role.name}" | ||
} | ||
|
||
resource "aws_iam_role" "batch_iam_role" { | ||
name = "batch_%[1]s" | ||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "batch.amazonaws.com" | ||
} | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "batch_policy_attachment" { | ||
role = "${aws_iam_role.batch_iam_role.name}" | ||
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole" | ||
} | ||
|
||
resource "aws_security_group" "security_group" { | ||
name = "%[1]s" | ||
} | ||
|
||
resource "aws_vpc" "vpc" { | ||
cidr_block = "10.1.0.0/16" | ||
} | ||
|
||
resource "aws_subnet" "subnet" { | ||
vpc_id = "${aws_vpc.vpc.id}" | ||
cidr_block = "10.1.1.0/24" | ||
} | ||
|
||
resource "aws_batch_compute_environment" "batch_compute_environment" { | ||
compute_environment_name = "%[1]s" | ||
compute_resources { | ||
instance_role = "${aws_iam_instance_profile.iam_instance_profile.arn}" | ||
instance_type = [ | ||
"c4.large", | ||
] | ||
max_vcpus = 16 | ||
min_vcpus = 0 | ||
security_group_ids = [ | ||
"${aws_security_group.security_group.id}" | ||
] | ||
subnets = [ | ||
"${aws_subnet.subnet.id}" | ||
] | ||
type = "EC2" | ||
} | ||
service_role = "${aws_iam_role.batch_iam_role.arn}" | ||
type = "MANAGED" | ||
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"] | ||
} | ||
|
||
resource "aws_batch_job_queue" "batch_job_queue" { | ||
name = "%[1]s" | ||
state = "ENABLED" | ||
priority = 1 | ||
compute_environments = ["${aws_batch_compute_environment.batch_compute_environment}"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing attribute here as well. |
||
} | ||
|
||
resource "aws_batch_job_definition" "batch_job_definition" { | ||
name = "%[1]s" | ||
type = "container" | ||
container_properties = <<CONTAINER_PROPERTIES | ||
{ | ||
"command": ["ls", "-la"], | ||
"image": "busybox", | ||
"memory": 512, | ||
"vcpus": 1, | ||
"volumes": [ ], | ||
"environment": [ ], | ||
"mountPoints": [ ], | ||
"ulimits": [ ] | ||
} | ||
CONTAINER_PROPERTIES | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccAWSCloudWatchEventTargetConfigInputTransformer(rName string) string { | ||
return fmt.Sprintf(` | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this supposed to be optional or required? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dang! good spot!