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-targets): option to provide an existing role to use with the StepFunctions State Machine target #10551

Merged
merged 2 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 16 additions & 4 deletions packages/@aws-cdk/aws-events-targets/lib/state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@ export interface SfnStateMachineProps {
* @default the entire EventBridge event
*/
readonly input?: events.RuleTargetInput;

/**
* The IAM role to be assumed to execute the State Machine
*
* @default - a new role will be created
*/
readonly role?: iam.IRole;
}

/**
* Use a StepFunctions state machine as a target for Amazon EventBridge rules.
*/
export class SfnStateMachine implements events.IRuleTarget {
private readonly role: iam.IRole;
shivlaks marked this conversation as resolved.
Show resolved Hide resolved

constructor(public readonly machine: sfn.IStateMachine, private readonly props: SfnStateMachineProps = {}) {
if (props.role) {
props.role.grant(new iam.ServicePrincipal('events.amazonaws.com'));
}
// no statements are passed because we are configuring permissions by using grant* helper below
this.role = props.role ?? singletonEventRole(machine, []);
machine.grantStartExecution(this.role);
}

/**
Expand All @@ -31,10 +46,7 @@ export class SfnStateMachine implements events.IRuleTarget {
return {
id: '',
arn: this.machine.stateMachineArn,
role: singletonEventRole(this.machine, [new iam.PolicyStatement({
actions: ['states:StartExecution'],
resources: [this.machine.stateMachineArn],
})]),
role: this.role,
input: this.props.input,
targetResource: this.machine,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '@aws-cdk/assert/jest';
import * as events from '@aws-cdk/aws-events';
import * as iam from '@aws-cdk/aws-iam';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as cdk from '@aws-cdk/core';
import * as targets from '../../lib';
Expand Down Expand Up @@ -27,4 +28,85 @@ test('State machine can be used as Event Rule target', () => {
},
],
});
expect(stack).toHaveResourceLike('AWS::IAM::Role', {
AssumeRolePolicyDocument: {
Statement: [
{
Action: 'sts:AssumeRole',
Effect: 'Allow',
Principal: {
Service: 'events.amazonaws.com',
},
},
],
},
});
expect(stack).toHaveResourceLike('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: 'states:StartExecution',
Effect: 'Allow',
Resource: {
Ref: 'SM934E715A',
},
},
],
},
});
});

test('Existing role can be used for State machine Rule target', () => {
// GIVEN
const stack = new cdk.Stack();
const rule = new events.Rule(stack, 'Rule', {
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
});
const role = new iam.Role(stack, 'Role', {
assumedBy: new iam.ServicePrincipal('events.amazonaws.com'),
});
const stateMachine = new sfn.StateMachine(stack, 'SM', {
definition: new sfn.Wait(stack, 'Hello', { time: sfn.WaitTime.duration(cdk.Duration.seconds(10)) }),
role,
});

// WHEN
rule.addTarget(new targets.SfnStateMachine(stateMachine, {
input: events.RuleTargetInput.fromObject({ SomeParam: 'SomeValue' }),
}));

// THEN
expect(stack).toHaveResourceLike('AWS::Events::Rule', {
Targets: [
{
Input: '{"SomeParam":"SomeValue"}',
},
],
});
ayush987goyal marked this conversation as resolved.
Show resolved Hide resolved
expect(stack).toHaveResourceLike('AWS::IAM::Role', {
AssumeRolePolicyDocument: {
Statement: [
{
Action: 'sts:AssumeRole',
Effect: 'Allow',
Principal: {
Service: 'events.amazonaws.com',
},
},
],
},
});
expect(stack).toHaveResourceLike('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: 'states:StartExecution',
Effect: 'Allow',
Resource: {
Ref: 'SM934E715A',
},
},
],
},
});
});