Skip to content

Commit da32176

Browse files
jogoldrix0rrr
authored andcommitted
feat(stepfunctions): add grantStartExecution() (#2793)
Grant the given identity permissions to start an execution of a state machine (`states:StartExecution`).
1 parent acf015d commit da32176

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

Diff for: packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,39 @@ export interface StateMachineProps {
3737
}
3838

3939
/**
40-
* Define a StepFunctions State Machine
40+
* A new or imported state machine.
4141
*/
42-
export class StateMachine extends Resource implements IStateMachine {
42+
abstract class StateMachineBase extends Resource implements IStateMachine {
4343
/**
4444
* Import a state machine
4545
*/
4646
public static fromStateMachineArn(scope: Construct, id: string, stateMachineArn: string): IStateMachine {
47-
class Import extends Resource implements IStateMachine {
47+
class Import extends StateMachineBase {
4848
public readonly stateMachineArn = stateMachineArn;
4949
}
5050

5151
return new Import(scope, id);
5252
}
5353

54+
public abstract readonly stateMachineArn: string;
55+
56+
/**
57+
* Grant the given identity permissions to start an execution of this state
58+
* machine.
59+
*/
60+
public grantStartExecution(identity: iam.IGrantable): iam.Grant {
61+
return iam.Grant.addToPrincipal({
62+
grantee: identity,
63+
actions: ['states:StartExecution'],
64+
resourceArns: [this.stateMachineArn]
65+
});
66+
}
67+
}
68+
69+
/**
70+
* Define a StepFunctions State Machine
71+
*/
72+
export class StateMachine extends StateMachineBase {
5473
/**
5574
* Execution role of this state machine
5675
*/

Diff for: packages/@aws-cdk/aws-stepfunctions/test/test.state-machine-resources.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,47 @@ export = {
129129
test.done();
130130
},
131131

132-
};
132+
'Can grant start execution to a role'(test: Test) {
133+
// GIVEN
134+
const stack = new cdk.Stack();
135+
const task = new stepfunctions.Task(stack, 'Task', {
136+
task: {
137+
bind: () => ({ resourceArn: 'resource' })
138+
}
139+
});
140+
const stateMachine = new stepfunctions.StateMachine(stack, 'StateMachine', {
141+
definition: task
142+
});
143+
const role = new iam.Role(stack, 'Role', {
144+
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com')
145+
});
146+
147+
// WHEN
148+
stateMachine.grantStartExecution(role);
149+
150+
// THEN
151+
expect(stack).to(haveResource('AWS::IAM::Policy', {
152+
PolicyDocument: {
153+
Statement: [
154+
{
155+
Action: 'states:StartExecution',
156+
Effect: 'Allow',
157+
Resource: {
158+
Ref: 'StateMachine2E01A3A5'
159+
}
160+
}
161+
],
162+
Version: '2012-10-17',
163+
},
164+
PolicyName: 'RoleDefaultPolicy5FFB7DAB',
165+
Roles: [
166+
{
167+
Ref: 'Role1ABCC5F0'
168+
}
169+
]
170+
}));
171+
172+
test.done();
173+
}
174+
175+
};

0 commit comments

Comments
 (0)