-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathapi-destination.ts
101 lines (91 loc) · 2.97 KB
/
api-destination.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { addToDeadLetterQueueResourcePolicy, bindBaseTargetConfig, singletonEventRole, TargetBaseProps } from './util';
import * as events from '../../aws-events';
import * as iam from '../../aws-iam';
/**
* Customize the EventBridge Api Destinations Target
*/
export interface ApiDestinationProps extends TargetBaseProps {
/**
* The event to send
*
* @default - the entire EventBridge event
*/
readonly event?: events.RuleTargetInput;
/**
* The role to assume before invoking the target
*
* @default - a new role will be created
*/
readonly eventRole?: iam.IRole;
/**
* Additional headers sent to the API Destination
*
* These are merged with headers specified on the Connection, with
* the headers on the Connection taking precedence.
*
* You can only specify secret values on the Connection.
*
* @default - none
*/
readonly headerParameters?: Record<string, string>;
/**
* Path parameters to insert in place of path wildcards (`*`).
*
* If the API destination has a wilcard in the path, these path parts
* will be inserted in that place.
*
* @default - none
*/
readonly pathParameterValues?: string[];
/**
* Additional query string parameters sent to the API Destination
*
* These are merged with headers specified on the Connection, with
* the headers on the Connection taking precedence.
*
* You can only specify secret values on the Connection.
*
* @default - none
*/
readonly queryStringParameters?: Record<string, string>;
}
/**
* Use an API Destination rule target.
*/
export class ApiDestination implements events.IRuleTarget {
constructor(
private readonly apiDestination: events.IApiDestination,
private readonly props: ApiDestinationProps = {},
) { }
/**
* Returns a RuleTarget that can be used to trigger API destinations
* from an EventBridge event.
*/
public bind(_rule: events.IRule, _id?: string): events.RuleTargetConfig {
const httpParameters: events.CfnRule.HttpParametersProperty | undefined =
this.props.headerParameters ??
this.props.pathParameterValues ??
this.props.queryStringParameters
? {
headerParameters: this.props.headerParameters,
pathParameterValues: this.props.pathParameterValues,
queryStringParameters: this.props.queryStringParameters,
} : undefined;
if (this.props?.deadLetterQueue) {
addToDeadLetterQueueResourcePolicy(_rule, this.props.deadLetterQueue);
}
const role = this.props?.eventRole ?? singletonEventRole(this.apiDestination);
role.addToPrincipalPolicy(new iam.PolicyStatement({
resources: [this.apiDestination.apiDestinationArn],
actions: ['events:InvokeApiDestination'],
}));
return {
...(this.props ? bindBaseTargetConfig(this.props) : {}),
arn: this.apiDestination.apiDestinationArn,
role,
input: this.props.event,
targetResource: this.apiDestination,
httpParameters,
};
}
}