diff --git a/packages/@aws-cdk/aws-apigatewayv2-authorizers/test/http/integ.user-pool.expected.json b/packages/@aws-cdk/aws-apigatewayv2-authorizers/test/http/integ.user-pool.expected.json index cdfdf727a21f3..b53be9c1b9c27 100644 --- a/packages/@aws-cdk/aws-apigatewayv2-authorizers/test/http/integ.user-pool.expected.json +++ b/packages/@aws-cdk/aws-apigatewayv2-authorizers/test/http/integ.user-pool.expected.json @@ -72,14 +72,14 @@ [ "integrations/", { - "Ref": "MyHttpApiHttpIntegration6f095b8469365f72e33fa33d9711b140C45F3B26" + "Ref": "MyHttpApiGETHttpIntegration6f095b8469365f72e33fa33d9711b140516EBE31" } ] ] } } }, - "MyHttpApiHttpIntegration6f095b8469365f72e33fa33d9711b140C45F3B26": { + "MyHttpApiGETHttpIntegration6f095b8469365f72e33fa33d9711b140516EBE31": { "Type": "AWS::ApiGatewayV2::Integration", "Properties": { "ApiId": { diff --git a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.alb.expected.json b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.alb.expected.json index 891c7ab616b10..e0ffbc32a3349 100644 --- a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.alb.expected.json +++ b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.alb.expected.json @@ -622,7 +622,7 @@ [ "integrations/", { - "Ref": "HttpProxyPrivateApiHttpIntegration1a580b19954e4317026ffbce1f7d5ade82925DF0" + "Ref": "HttpProxyPrivateApiDefaultRouteHttpIntegration1a580b19954e4317026ffbce1f7d5ade7A32685B" } ] ] @@ -647,7 +647,7 @@ "SecurityGroupIds": [] } }, - "HttpProxyPrivateApiHttpIntegration1a580b19954e4317026ffbce1f7d5ade82925DF0": { + "HttpProxyPrivateApiDefaultRouteHttpIntegration1a580b19954e4317026ffbce1f7d5ade7A32685B": { "Type": "AWS::ApiGatewayV2::Integration", "Properties": { "ApiId": { diff --git a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.http-proxy.expected.json b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.http-proxy.expected.json index 71c6674df6b90..e5220fe2959b7 100644 --- a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.http-proxy.expected.json +++ b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.http-proxy.expected.json @@ -108,14 +108,14 @@ [ "integrations/", { - "Ref": "LambdaProxyApiHttpIntegration70df0ec52c3e3b6bbc96e64ce3a05f24467606A4" + "Ref": "LambdaProxyApiDefaultRouteHttpIntegration70df0ec52c3e3b6bbc96e64ce3a05f24EE575CBA" } ] ] } } }, - "LambdaProxyApiHttpIntegration70df0ec52c3e3b6bbc96e64ce3a05f24467606A4": { + "LambdaProxyApiDefaultRouteHttpIntegration70df0ec52c3e3b6bbc96e64ce3a05f24EE575CBA": { "Type": "AWS::ApiGatewayV2::Integration", "Properties": { "ApiId": { @@ -162,14 +162,14 @@ [ "integrations/", { - "Ref": "HttpProxyApiHttpIntegration8eeecf9ecdb91f31bebf6bd54fb711a4F32A389A" + "Ref": "HttpProxyApiDefaultRouteHttpIntegration8eeecf9ecdb91f31bebf6bd54fb711a41921AB82" } ] ] } } }, - "HttpProxyApiHttpIntegration8eeecf9ecdb91f31bebf6bd54fb711a4F32A389A": { + "HttpProxyApiDefaultRouteHttpIntegration8eeecf9ecdb91f31bebf6bd54fb711a41921AB82": { "Type": "AWS::ApiGatewayV2::Integration", "Properties": { "ApiId": { diff --git a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.lambda-proxy.expected.json b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.lambda-proxy.expected.json index 573cd3bcb8bb3..f5bc444d929c3 100644 --- a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.lambda-proxy.expected.json +++ b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.lambda-proxy.expected.json @@ -108,14 +108,14 @@ [ "integrations/", { - "Ref": "LambdaProxyApiHttpIntegration70df0ec52c3e3b6bbc96e64ce3a05f24467606A4" + "Ref": "LambdaProxyApiDefaultRouteHttpIntegration70df0ec52c3e3b6bbc96e64ce3a05f24EE575CBA" } ] ] } } }, - "LambdaProxyApiHttpIntegration70df0ec52c3e3b6bbc96e64ce3a05f24467606A4": { + "LambdaProxyApiDefaultRouteHttpIntegration70df0ec52c3e3b6bbc96e64ce3a05f24EE575CBA": { "Type": "AWS::ApiGatewayV2::Integration", "Properties": { "ApiId": { diff --git a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.nlb.expected.json b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.nlb.expected.json index 6c9fa0633f138..0446031c61a19 100644 --- a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.nlb.expected.json +++ b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.nlb.expected.json @@ -587,7 +587,7 @@ [ "integrations/", { - "Ref": "HttpProxyPrivateApiHttpIntegration1a580b19954e4317026ffbce1f7d5ade82925DF0" + "Ref": "HttpProxyPrivateApiDefaultRouteHttpIntegration1a580b19954e4317026ffbce1f7d5ade7A32685B" } ] ] @@ -612,7 +612,7 @@ "SecurityGroupIds": [] } }, - "HttpProxyPrivateApiHttpIntegration1a580b19954e4317026ffbce1f7d5ade82925DF0": { + "HttpProxyPrivateApiDefaultRouteHttpIntegration1a580b19954e4317026ffbce1f7d5ade7A32685B": { "Type": "AWS::ApiGatewayV2::Integration", "Properties": { "ApiId": { diff --git a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.service-discovery.expected.json b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.service-discovery.expected.json index ada88fe7be66f..28b636ef54808 100644 --- a/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.service-discovery.expected.json +++ b/packages/@aws-cdk/aws-apigatewayv2-integrations/test/http/integ.service-discovery.expected.json @@ -588,14 +588,14 @@ [ "integrations/", { - "Ref": "HttpProxyPrivateApiHttpIntegrationa5ec5390ca688d567e9449daf58afc6fB75CE02B" + "Ref": "HttpProxyPrivateApiDefaultRouteHttpIntegrationa5ec5390ca688d567e9449daf58afc6f6DEAA8A8" } ] ] } } }, - "HttpProxyPrivateApiHttpIntegrationa5ec5390ca688d567e9449daf58afc6fB75CE02B": { + "HttpProxyPrivateApiDefaultRouteHttpIntegrationa5ec5390ca688d567e9449daf58afc6f6DEAA8A8": { "Type": "AWS::ApiGatewayV2::Integration", "Properties": { "ApiId": { diff --git a/packages/@aws-cdk/aws-apigatewayv2/lib/http/api.ts b/packages/@aws-cdk/aws-apigatewayv2/lib/http/api.ts index 2a481925c0a8b..52e5f1fccbe07 100644 --- a/packages/@aws-cdk/aws-apigatewayv2/lib/http/api.ts +++ b/packages/@aws-cdk/aws-apigatewayv2/lib/http/api.ts @@ -92,7 +92,7 @@ export interface IHttpApi extends IResource { * Add a http integration * @internal */ - _addIntegration(config: HttpRouteIntegrationConfig): HttpIntegration; + _addIntegration(scope: Construct, config: HttpRouteIntegrationConfig): HttpIntegration; } /** @@ -274,15 +274,15 @@ abstract class HttpApiBase extends Resource implements IHttpApi { // note that t /** * @internal */ - public _addIntegration(config: HttpRouteIntegrationConfig): HttpIntegration { - const stringifiedConfig = JSON.stringify(Stack.of(this).resolve(config)); + public _addIntegration(scope: Construct, config: HttpRouteIntegrationConfig): HttpIntegration { + const stringifiedConfig = JSON.stringify(Stack.of(scope).resolve(config)); const configHash = crypto.createHash('md5').update(stringifiedConfig).digest('hex'); if (configHash in this.httpIntegrations) { return this.httpIntegrations[configHash]; } - const integration = new HttpIntegration(this, `HttpIntegration-${configHash}`, { + const integration = new HttpIntegration(scope, `HttpIntegration-${configHash}`, { httpApi: this, integrationType: config.type, integrationUri: config.uri, diff --git a/packages/@aws-cdk/aws-apigatewayv2/lib/http/route.ts b/packages/@aws-cdk/aws-apigatewayv2/lib/http/route.ts index dac962f7bfee4..4510d13ed6f2b 100644 --- a/packages/@aws-cdk/aws-apigatewayv2/lib/http/route.ts +++ b/packages/@aws-cdk/aws-apigatewayv2/lib/http/route.ts @@ -140,7 +140,7 @@ export class HttpRoute extends Resource implements IHttpRoute { scope: this, }); - const integration = props.httpApi._addIntegration(config); + const integration = props.httpApi._addIntegration(this, config); const authBindResult = props.authorizer ? props.authorizer.bind({ route: this, diff --git a/packages/@aws-cdk/aws-apigatewayv2/test/http/route.test.ts b/packages/@aws-cdk/aws-apigatewayv2/test/http/route.test.ts index bfeb036a3fa72..a5ce1b7b64b64 100644 --- a/packages/@aws-cdk/aws-apigatewayv2/test/http/route.test.ts +++ b/packages/@aws-cdk/aws-apigatewayv2/test/http/route.test.ts @@ -1,5 +1,5 @@ import '@aws-cdk/assert/jest'; -import { Stack } from '@aws-cdk/core'; +import { Stack, App } from '@aws-cdk/core'; import { HttpApi, HttpAuthorizer, HttpAuthorizerType, HttpConnectionType, HttpIntegrationType, HttpMethod, HttpRoute, HttpRouteAuthorizerBindOptions, HttpRouteAuthorizerConfig, HttpRouteIntegrationConfig, HttpRouteKey, IHttpRouteAuthorizer, IHttpRouteIntegration, PayloadFormatVersion, @@ -25,7 +25,7 @@ describe('HttpRoute', () => { [ 'integrations/', { - Ref: 'HttpApiHttpIntegrationcff2618c192d3bd8581dd2a4093464f6CDB667B8', + Ref: 'HttpRouteHttpIntegrationcff2618c192d3bd8581dd2a4093464f6FB1097D0', }, ], ], @@ -115,6 +115,27 @@ describe('HttpRoute', () => { expect(stack2).toCountResources('AWS::ApiGatewayV2::Integration', 1); }); + test('route defined in a separate stack does not create cycles', () => { + // GIVEN + const integration = new DummyIntegration(); + + // WHEN + const app = new App(); + const stack1 = new Stack(app, 'ApiStack'); + const httpApi = new HttpApi(stack1, 'HttpApi'); + + const stack2 = new Stack(app, 'RouteStack'); + new HttpRoute(stack2, 'HttpRoute1', { + httpApi, + integration, + routeKey: HttpRouteKey.with('/books', HttpMethod.GET), + }); + + // THEN + expect(stack1).toCountResources('AWS::ApiGatewayV2::Integration', 0); + expect(stack2).toCountResources('AWS::ApiGatewayV2::Integration', 1); + }); + test('throws when path not start with /', () => { const stack = new Stack(); const httpApi = new HttpApi(stack, 'HttpApi'); @@ -253,4 +274,4 @@ class DummyAuthorizer implements IHttpRouteAuthorizer { authorizationType: HttpAuthorizerType.JWT, }; } -} \ No newline at end of file +}