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

feat(events): AWS Batch event target #6570

Merged
merged 29 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
87ca2c2
add initial tests and constructure for batch job target
Mar 3, 2020
0d5853f
add comments
Mar 3, 2020
f6113a9
roll other pr into this one:
Mar 5, 2020
b716342
Add experimental and remove yarn.lcok
Mar 5, 2020
d24c529
Merge branch 'master' into batch-event-target
Mar 5, 2020
39c33d6
fix tests and update event target
Mar 6, 2020
2dd28cb
reset yarn.lock
Mar 6, 2020
52dcd1f
remove changes to yarn.lock
Mar 6, 2020
b6d29ff
revert yarn.lock
Mar 6, 2020
30bc4e6
Merge branch 'master' into batch-event-target
Mar 6, 2020
033b484
fix yarn.lock
Mar 6, 2020
4125d2f
add another readme update
Mar 6, 2020
36bb464
Merge branch 'master' into batch-event-target
Mar 6, 2020
6ba8ca5
Merge branch 'master' into batch-event-target
Mar 6, 2020
fa30f69
Merge branch 'master' into batch-event-target
Mar 9, 2020
9543f5b
Merge branch 'master' into batch-event-target
Mar 9, 2020
7836343
fix value setters
Mar 9, 2020
18b072e
Merge branch 'master' into batch-event-target
Mar 9, 2020
6b4ce5f
Update batch.ts
rix0rrr Mar 9, 2020
3303838
fix test
Mar 9, 2020
01823a0
Merge branch 'batch-event-target' of github.com:wulfmann/aws-cdk into…
Mar 9, 2020
69c8104
Merge branch 'batch-event-target' of github.com:wulfmann/aws-cdk into…
wulfmann Mar 9, 2020
9d783aa
Merge branch 'batch-event-target' of github.com:wulfmann/aws-cdk into…
wulfmann Mar 9, 2020
7579527
fix integ test
wulfmann Mar 9, 2020
96c99e9
Merge branch 'master' into batch-event-target
wulfmann Mar 17, 2020
5867443
update tests
wulfmann Mar 23, 2020
a3db722
Merge branch 'master' into batch-event-target
wulfmann Mar 23, 2020
f6bdfc6
Update package.json
wulfmann Mar 23, 2020
cb7a5e4
Merge branch 'master' into batch-event-target
mergify[bot] Mar 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-events-targets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Currently supported are:
* Publish a message to an SNS topic
* Send a message to an SQS queue
* Start a StepFunctions state machine
* Queue a Batch job
* Make an AWS API call

See the README of the `@aws-cdk/aws-events` library for more information on
Expand Down
86 changes: 86 additions & 0 deletions packages/@aws-cdk/aws-events-targets/lib/batch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import * as batch from '@aws-cdk/aws-batch';
import * as events from '@aws-cdk/aws-events';
import * as iam from '@aws-cdk/aws-iam';
import { singletonEventRole } from './util';

/**
* Customize the Batch Job Event Target
* @experimental
*/
wulfmann marked this conversation as resolved.
Show resolved Hide resolved
export interface BatchJobProps {
/**
* The event to send to the Lambda
*
* This will be the payload sent to the Lambda Function.
*
* @default the entire CloudWatch event
*/
readonly event?: events.RuleTargetInput;

/**
* The size of the array, if this is an array batch job.
*
* Valid values are integers between 2 and 10,000.
*
* @default no arrayProperties are set
*/
readonly size?: number;

/**
* The number of times to attempt to retry, if the job fails. Valid values are 1–10.
*
* @default no retryStrategy is set
*/
readonly attempts?: number;
}

/**
* Use an AWS Batch Job / Queue as an event rule target.
* @experimental
*/
wulfmann marked this conversation as resolved.
Show resolved Hide resolved
export class BatchJob implements events.IRuleTarget {
constructor(
private readonly jobQueue: batch.IJobQueue,
private readonly jobDefinition: batch.IJobDefinition,
private readonly props: BatchJobProps = {}
) { }

/**
* Returns a RuleTarget that can be used to trigger queue this batch job as a
* result from a CloudWatch event.
*/
public bind(_rule: events.IRule, _id?: string): events.RuleTargetConfig {
const baseBatchParameters: any = {
jobDefinition: this.jobDefinition.jobDefinitionArn,
jobName: this.jobDefinition.jobDefinitionName
};

if (this.props.size) {
baseBatchParameters.arrayProperties = {
size: this.props.size
};
}

if (this.props.attempts) {
baseBatchParameters.retryStrategy = {
attempts: this.props.attempts
};
}

const batchParameters: events.CfnRule.BatchParametersProperty = baseBatchParameters;

return {
id: '',
arn: this.jobQueue.jobQueueArn,
role: singletonEventRole(this.jobDefinition, [
new iam.PolicyStatement({
actions: ['batch:SubmitJob'],
resources: [this.jobDefinition.jobDefinitionArn]
})
]),
input: this.props.event,
targetResource: this.jobQueue,
batchParameters
};
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-events-targets/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './batch';
export * from './codepipeline';
export * from './sns';
export * from './sqs';
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-events-targets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"@aws-cdk/aws-sns-subscriptions": "0.0.0",
"@aws-cdk/aws-sqs": "0.0.0",
"@aws-cdk/aws-stepfunctions": "0.0.0",
"@aws-cdk/aws-batch": "0.0.0",
"@aws-cdk/core": "0.0.0",
"constructs": "^2.0.0"
},
Expand All @@ -123,6 +124,7 @@
"@aws-cdk/aws-sns-subscriptions": "0.0.0",
"@aws-cdk/aws-sqs": "0.0.0",
"@aws-cdk/aws-stepfunctions": "0.0.0",
"@aws-cdk/aws-batch": "0.0.0",
"@aws-cdk/core": "0.0.0",
"constructs": "^2.0.0"
},
Expand Down
101 changes: 101 additions & 0 deletions packages/@aws-cdk/aws-events-targets/test/batch/batch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { expect, haveResource } from '@aws-cdk/assert';
import * as batch from '@aws-cdk/aws-batch';
import { ContainerImage } from '@aws-cdk/aws-ecs';
import * as events from '@aws-cdk/aws-events';
import { Stack } from '@aws-cdk/core';
import * as targets from '../../lib';

test('use aws batch job as an eventrule target', () => {
// GIVEN
const stack = new Stack();
const jobQueue = new batch.JobQueue(stack, 'MyQueue', {
computeEnvironments: [
{
computeEnvironment: new batch.ComputeEnvironment(stack, 'ComputeEnvironment', {
managed: false
}),
order: 1
}
]
});
const jobDefinition = new batch.JobDefinition(stack, 'MyJob', {
container: {
image: ContainerImage.fromRegistry('test-repo')
}
});
const rule = new events.Rule(stack, 'Rule', {
schedule: events.Schedule.expression('rate(1 min)')
});

// WHEN
rule.addTarget(new targets.BatchJob(jobQueue, jobDefinition));

// THEN
expect(stack).to(haveResource('AWS::Events::Rule', {
ScheduleExpression: "rate(1 min)",
State: "ENABLED",
Targets: [
{
Arn: {
Ref: "MyQueueE6CA6235"
},
Id: "Target0",
RoleArn: {
"Fn::GetAtt": [
"MyJobEventsRoleCF43C336",
"Arn"
]
}
}
]
}));
expect(stack).to(haveResource('AWS::IAM::Role', {
AssumeRolePolicyDocument: {
Statement: [
{
Action: "sts:AssumeRole",
Effect: "Allow",
Principal: {
Service: "batch.amazonaws.com"
}
}
],
Version: "2012-10-17"
},
ManagedPolicyArns: [
{
"Fn::Join": [
"",
[
"arn:",
{
Ref: "AWS::Partition"
},
":iam::aws:policy/service-role/AWSBatchServiceRole"
]
]
}
]
}));

expect(stack).to(haveResource('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: "batch:SubmitJob",
Effect: "Allow",
Resource: {
Ref: "MyJob8719E923"
}
}
],
Version: "2012-10-17"
},
PolicyName: "MyJobEventsRoleDefaultPolicy7266D3A7",
Roles: [
{
Ref: "MyJobEventsRoleCF43C336"
}
]
}));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"Resources": {
"MyJob8719E923": {
"Type": "AWS::Batch::JobDefinition",
"Properties": {
"ContainerProperties": {
"Image": "test-repo",
"Memory": 4,
"Privileged": false,
"ReadonlyRootFilesystem": false,
"Vcpus": 1
},
"RetryStrategy": {
"Attempts": 1
},
"Timeout": {},
"Type": "container"
}
},
"MyQueueE6CA6235": {
"Type": "AWS::Batch::JobQueue",
"Properties": {
"ComputeEnvironmentOrder": [
{
"ComputeEnvironment": {
"Ref": "ComputeEnvironmentC570994D"
},
"Order": 1
}
],
"Priority": 1,
"State": "ENABLED"
}
},
"ComputeEnvironmentC570994D": {
"Type": "AWS::Batch::ComputeEnvironment",
"Properties": {
"ServiceRole": {
"Fn::GetAtt": [
"ComputeEnvironmentResourceServiceInstanceRoleDC6D4445",
"Arn"
]
},
"State": "ENABLED",
"Type": "UNMANAGED"
}
},
"MyJobEventsRoleCF43C336": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
}
}
],
"Version": "2012-10-17"
}
}
},
"ComputeEnvironmentResourceServiceInstanceRoleDC6D4445": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "batch.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSBatchServiceRole"
]
]
}
]
}
},
"MyJobEventsRoleDefaultPolicy7266D3A7": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": "batch:SubmitJob",
"Effect": "Allow",
"Resource": {
"Ref": "MyJob8719E923"
}
}
],
"Version": "2012-10-17"
},
"PolicyName": "MyJobEventsRoleDefaultPolicy7266D3A7",
"Roles": [
{
"Ref": "MyJobEventsRoleCF43C336"
}
]
}
},
"TimerBF6F831F": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "rate(1 minute)",
"State": "ENABLED",
"Targets": [
{
"Arn": {
"Ref": "MyQueueE6CA6235"
},
"Id": "Target0",
"RoleArn": {
"Fn::GetAtt": [
"MyJobEventsRoleCF43C336",
"Arn"
]
}
}
]
}
},
"Timer2B6F162E9": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "rate(2 minutes)",
"State": "ENABLED",
"Targets": [
{
"Arn": {
"Ref": "MyQueueE6CA6235"
},
"Id": "Target0",
"RoleArn": {
"Fn::GetAtt": [
"MyJobEventsRoleCF43C336",
"Arn"
]
}
}
]
}
}
}
}