Skip to content

Commit

Permalink
fix(aws-events-targets): ApiGateway events target should accept IRestApi
Browse files Browse the repository at this point in the history
issue: #16423
  • Loading branch information
Neel Krishna committed Mar 7, 2024
1 parent 9c82bca commit 0cef0c6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/aws-cdk-lib/aws-events-targets/lib/api-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ export interface ApiGatewayProps extends TargetBaseProps {
*/
export class ApiGateway implements events.IRuleTarget {

constructor(public readonly restApi: api.RestApi, private readonly props?: ApiGatewayProps) {
/**
* @param iRestApi - IRestApi implementation to use as event target
* @param props - Properties to configure the APIGatway target
*/
constructor(public readonly iRestApi: api.IRestApi, private readonly props?: ApiGatewayProps) {
}

/**
Expand All @@ -93,13 +97,13 @@ 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,
);

const role = this.props?.eventRole || singletonEventRole(this.restApi);
const role = this.props?.eventRole || singletonEventRole(this.iRestApi);
role.addToPrincipalPolicy(new iam.PolicyStatement({
resources: [restApiArn],
actions: [
Expand All @@ -114,7 +118,7 @@ export class ApiGateway implements events.IRuleTarget {
role,
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,62 @@ import * as sqs from '../../../aws-sqs';
import * as cdk from '../../../core';
import * as targets from '../../lib';

test('use a SpecRestApi APIGateway event rule target', () => {
// GIVEN
const stack = new cdk.Stack();
const specRestApi = newTestSpecRestApi(stack);
const rule = new events.Rule(stack, 'Rule', {
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
});

// WHEN
rule.addTarget(new targets.ApiGateway(specRestApi));

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Events::Rule', {
Targets: [
{
Arn: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':execute-api:',
{
Ref: 'AWS::Region',
},
':',
{
Ref: 'AWS::AccountId',
},
':',
{
Ref: 'MySpecRestApiFB7DB2AB',
},
'/',
{
Ref: 'MySpecRestApiDeploymentStageprod8522A503',
},
'/*/',
],
],
},
HttpParameters: {},
Id: 'Target0',
RoleArn: {
'Fn::GetAtt': [
'MySpecRestApiEventsRole25C1D10F',
'Arn',
],
},
},
],
});
});

test('use api gateway rest api as an event rule target', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -261,3 +317,12 @@ function newTestRestApi(scope: constructs.Construct, suffix = '') {
handler: lambdaFunctin,
} );
}

function newTestSpecRestApi(scope: constructs.Construct, suffix = '') {
return new api.SpecRestApi( scope, `MySpecRestApi${suffix}`, {
apiDefinition: api.ApiDefinition.fromInline({
openapi: '3.0.2',
paths: {},
}),
} );
}

0 comments on commit 0cef0c6

Please sign in to comment.