Skip to content

Commit

Permalink
fix(aws-events-targets): Consume IRestApi as target (aws#16423)
Browse files Browse the repository at this point in the history
Fixes: aws#16423

When creating an Event Bridge target for an API Gateway, the
superinterface IRestApi should be consumed instead of the
concrete class RestApi.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Matt Berry committed Sep 22, 2021
1 parent f3c7dc3 commit 126f627
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions packages/@aws-cdk/aws-events-targets/lib/api-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ export interface ApiGatewayProps extends TargetBaseProps {
}

/**
* Use an API Gateway REST APIs as a target for Amazon EventBridge rules.
* Use an API Gateway REST API as a target for Amazon EventBridge rules.
*/
export class ApiGateway implements events.IRuleTarget {
export class ApiGatewayTarget implements events.IRuleTarget {

constructor(public readonly restApi: api.RestApi, private readonly props?: ApiGatewayProps) {
/**
* @param iRestApi - An implementation of a Rest API to send events to
*/
constructor(public readonly iRestApi: api.IRestApi, protected readonly props?: ApiGatewayProps) {
}

/**
Expand All @@ -93,15 +96,15 @@ export class ApiGateway implements events.IRuleTarget {
throw new Error('The number of wildcards in the path does not match the number of path pathParameterValues.');
}

const restApiArn = this.restApi.arnForExecuteApi(
const restApiArn = this.iRestApi.arnForExecuteApi(
this.props?.method,
this.props?.path || '/',
this.props?.stage || this.restApi.deploymentStage.stageName,
this.props?.stage || this.iRestApi.deploymentStage.stageName,
);
return {
...(this.props ? bindBaseTargetConfig(this.props) : {}),
arn: restApiArn,
role: this.props?.eventRole || singletonEventRole(this.restApi, [new iam.PolicyStatement({
role: this.props?.eventRole || singletonEventRole(this.iRestApi, [new iam.PolicyStatement({
resources: [restApiArn],
actions: [
'execute-api:Invoke',
Expand All @@ -110,7 +113,7 @@ export class ApiGateway implements events.IRuleTarget {
})]),
deadLetterConfig: this.props?.deadLetterQueue && { arn: this.props.deadLetterQueue?.queueArn },
input: this.props?.postBody,
targetResource: this.restApi,
targetResource: this.iRestApi,
httpParameters: {
headerParameters: this.props?.headerParameters,
queryStringParameters: this.props?.queryStringParameters,
Expand All @@ -121,3 +124,19 @@ export class ApiGateway implements events.IRuleTarget {

}

/**
* Use an API Gateway REST APIs as a target for Amazon EventBridge rules.
*
* @deprecated - Use ApiGatewayTarget
*/
export class ApiGateway extends ApiGatewayTarget {

/**
* @param restApi - A Rest API to send events to
*/
constructor(public readonly restApi: api.RestApi, protected readonly props?: ApiGatewayProps) {
super(restApi, props);
}

}

0 comments on commit 126f627

Please sign in to comment.