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