Skip to content

Commit 2e67c2d

Browse files
jogoldmergify[bot]
authored andcommitted
fix(events): remove policy statement from CF template when using AwsApi (#4037)
* fix(events): remove policy statement from CF template when using AwsApi All props of the `AwsApi` target were passed as event input, unnecessarily polluting the CloudFormation template. * refactor with AwsApiInput interface * JSDoc
1 parent cad0b15 commit 2e67c2d

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed

packages/@aws-cdk/aws-events-targets/lib/aws-api-handler/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// tslint:disable:no-console
22
import AWS = require('aws-sdk');
3-
import { AwsApiProps } from '../aws-api';
3+
import { AwsApiInput } from '../aws-api';
44

5-
export async function handler(event: AwsApiProps) {
5+
export async function handler(event: AwsApiInput) {
66
console.log('Event: %j', event);
77
console.log('AWS SDK VERSION: ' + (AWS as any).VERSION);
88

packages/@aws-cdk/aws-events-targets/lib/aws-api.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ export type AwsSdkMetadata = {[key: string]: any};
1212

1313
const awsSdkMetadata: AwsSdkMetadata = metadata;
1414

15-
export interface AwsApiProps {
15+
/**
16+
* Rule target input for an AwsApi target.
17+
*/
18+
export interface AwsApiInput {
1619
/**
1720
* The service to call
1821
*
@@ -52,7 +55,12 @@ export interface AwsApiProps {
5255
* @default - use latest available API version
5356
*/
5457
readonly apiVersion?: string;
58+
}
5559

60+
/**
61+
* Properties for an AwsApi target.
62+
*/
63+
export interface AwsApiProps extends AwsApiInput {
5664
/**
5765
* The IAM policy statement to allow the API call. Use only if
5866
* resource restriction is needed.
@@ -93,10 +101,18 @@ export class AwsApi implements events.IRuleTarget {
93101
// Allow handler to be called from rule
94102
addLambdaPermission(rule, handler);
95103

104+
const input: AwsApiInput = {
105+
service: this.props.service,
106+
action: this.props.action,
107+
parameters: this.props.parameters,
108+
catchErrorPattern: this.props.catchErrorPattern,
109+
apiVersion: this.props.apiVersion,
110+
};
111+
96112
return {
97113
id: '',
98114
arn: handler.functionArn,
99-
input: events.RuleTargetInput.fromObject(this.props),
115+
input: events.RuleTargetInput.fromObject(input),
100116
targetResource: handler,
101117
};
102118
}

packages/@aws-cdk/aws-events-targets/test/aws-api/aws-api.test.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { countResources, expect, haveResource } from '@aws-cdk/assert';
22
import events = require('@aws-cdk/aws-events');
3+
import iam = require('@aws-cdk/aws-iam');
34
import { Stack } from '@aws-cdk/core';
45
import targets = require('../../lib');
56

@@ -18,8 +19,8 @@ test('use AwsApi as an event rule target', () => {
1819
service: 'cool-service',
1920
forceNewDeployment: true
2021
} as AWS.ECS.UpdateServiceRequest,
22+
catchErrorPattern: 'error',
2123
apiVersion: '2019-01-01',
22-
catchErrorPattern: 'error'
2324
}));
2425

2526
rule.addTarget(new targets.AwsApi({
@@ -48,8 +49,8 @@ test('use AwsApi as an event rule target', () => {
4849
service: 'cool-service',
4950
forceNewDeployment: true
5051
},
52+
catchErrorPattern: 'error',
5153
apiVersion: '2019-01-01',
52-
catchErrorPattern: 'error'
5354
})
5455
},
5556
{
@@ -92,3 +93,53 @@ test('use AwsApi as an event rule target', () => {
9293
}
9394
}));
9495
});
96+
97+
test('with policy statement', () => {
98+
// GIVEN
99+
const stack = new Stack();
100+
const rule = new events.Rule(stack, 'Rule', {
101+
schedule: events.Schedule.expression('rate(15 minutes)')
102+
});
103+
104+
// WHEN
105+
rule.addTarget(new targets.AwsApi({
106+
service: 'service',
107+
action: 'action',
108+
policyStatement: new iam.PolicyStatement({
109+
actions: ['s3:GetObject'],
110+
resources: ['resource'],
111+
})
112+
}));
113+
114+
// THEN
115+
expect(stack).to(haveResource('AWS::Events::Rule', {
116+
Targets: [
117+
{
118+
Arn: {
119+
"Fn::GetAtt": [
120+
"AWSb4cf1abd4e4f4bc699441af7ccd9ec371511E620",
121+
"Arn"
122+
]
123+
},
124+
Id: "Target0",
125+
Input: JSON.stringify({ // No `policyStatement`
126+
service: 'service',
127+
action: 'action',
128+
})
129+
},
130+
]
131+
}));
132+
133+
expect(stack).to(haveResource('AWS::IAM::Policy', {
134+
PolicyDocument: {
135+
Statement: [
136+
{
137+
Action: "s3:GetObject",
138+
Effect: "Allow",
139+
Resource: "resource"
140+
},
141+
],
142+
Version: "2012-10-17"
143+
}
144+
}));
145+
});

0 commit comments

Comments
 (0)