-
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cde797a
Add batch job as a cloudwatch_event_target
b4f7635
Mark job_attempts optional
47b08e8
Test fixes: add missing ARNs, fix depends_on's
74e3f73
Merge remote-tracking branch 'upstream/master' into batch_event_target
ac458d8
Check that array_size and job_attempts values are within range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We probably need to check against 0 value here:
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.
Fixed