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(stepfunctions-tasks): additional IAM statements for AWS SDK service integration #22070

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-stepfunctions-tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@ const listBuckets = new tasks.CallAwsService(this, 'ListBuckets', {
});
```

Use the `additionalIamStatements` prop to pass additional IAM statements that will be added to the
state machine role's policy. Use it in the case where the call requires more than a single statement
to be executed:

```ts
const detectLabels = new tasks.CallAwsService(stack, 'DetectLabels', {
service: 'rekognition',
action: 'detectLabels',
iamResources: ['*'],
additionalIamStatements: [
new iam.PolicyStatement({
actions: ['s3:getObject'],
resources: ['arn:aws:s3:::my-bucket/*'],
}),
],
});
```

## Athena

Step Functions supports [Athena](https://docs.aws.amazon.com/step-functions/latest/dg/connect-athena.html) through the service integration pattern.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ export interface CallAwsServiceProps extends sfn.TaskStateBaseProps {
* @default - service:action
*/
readonly iamAction?: string;

/**
* Additional IAM statements that will be added to the state machine
* role's policy.
*
* Use in the case where the call requires more than a single statement to
* be executed, e.g. `rekognition:detectLabels` requires also S3 permissions
* to read the object on which it must act.
*
* @default - no additional statements are added
*/
readonly additionalIamStatements?: iam.PolicyStatement[];
}

/**
Expand All @@ -75,6 +87,7 @@ export class CallAwsService extends sfn.TaskStateBase {
// https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_action.html
actions: [props.iamAction ?? `${props.service}:${props.action}`],
}),
...props.additionalIamStatements ?? [],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Template } from '@aws-cdk/assertions';
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 tasks from '../../lib';
Expand Down Expand Up @@ -159,3 +160,41 @@ test('throws with invalid integration pattern', () => {
iamResources: ['*'],
})).toThrow(/The RUN_JOB integration pattern is not supported for CallAwsService/);
});

test('can pass additional IAM statements', () => {
// WHEN
const task = new tasks.CallAwsService(stack, 'DetectLabels', {
service: 'rekognition',
action: 'detectLabels',
iamResources: ['*'],
additionalIamStatements: [
new iam.PolicyStatement({
actions: ['s3:getObject'],
resources: ['arn:aws:s3:::my-bucket/*'],
}),
],
});

new sfn.StateMachine(stack, 'StateMachine', {
definition: task,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: 'rekognition:detectLabels',
Effect: 'Allow',
Resource: '*',
},
{
Action: 's3:getObject',
Effect: 'Allow',
Resource: 'arn:aws:s3:::my-bucket/*',
},
],
Version: '2012-10-17',
},
});
});