Skip to content

Commit 6e6440a

Browse files
hoegertnmergify[bot]
authored andcommitted
fix(apigateway): allow reusing lambda integration for multiple apis (#3532)
* fix: allow multiple uses of Lambdas inAPI GW * fix: more existing permissions changed * fix: decdk tests
1 parent 581f537 commit 6e6440a

File tree

7 files changed

+717
-121
lines changed

7 files changed

+717
-121
lines changed

packages/@aws-cdk/aws-apigateway/lib/integrations/lambda.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class LambdaIntegration extends AwsIntegration {
5555
super.bind(method);
5656
const principal = new iam.ServicePrincipal('apigateway.amazonaws.com');
5757

58-
const desc = `${method.httpMethod}.${method.resource.path.replace(/\//g, '.')}`;
58+
const desc = `${method.restApi.node.uniqueId}.${method.httpMethod}.${method.resource.path.replace(/\//g, '.')}`;
5959

6060
this.handler.addPermission(`ApiPermission.${desc}`, {
6161
principal,

packages/@aws-cdk/aws-apigateway/test/integ.restapi.books.expected.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"BooksHandlerServiceRole5B6A8847"
6161
]
6262
},
63-
"BooksHandlerApiPermissionGETbooksAB573150": {
63+
"BooksHandlerApiPermissionrestapibooksexamplebooksapi4538F335GETbooks727D645E": {
6464
"Type": "AWS::Lambda::Permission",
6565
"Properties": {
6666
"Action": "lambda:InvokeFunction",
@@ -101,7 +101,7 @@
101101
}
102102
}
103103
},
104-
"BooksHandlerApiPermissionTestGETbooksE0682829": {
104+
"BooksHandlerApiPermissionTestrestapibooksexamplebooksapi4538F335GETbooksCC375808": {
105105
"Type": "AWS::Lambda::Permission",
106106
"Properties": {
107107
"Action": "lambda:InvokeFunction",
@@ -138,7 +138,7 @@
138138
}
139139
}
140140
},
141-
"BooksHandlerApiPermissionPOSTbooksC38F97D8": {
141+
"BooksHandlerApiPermissionrestapibooksexamplebooksapi4538F335POSTbooksFDED8A87": {
142142
"Type": "AWS::Lambda::Permission",
143143
"Properties": {
144144
"Action": "lambda:InvokeFunction",
@@ -179,7 +179,7 @@
179179
}
180180
}
181181
},
182-
"BooksHandlerApiPermissionTestPOSTbooksCEEC4EF7": {
182+
"BooksHandlerApiPermissionTestrestapibooksexamplebooksapi4538F335POSTbooks4667899F": {
183183
"Type": "AWS::Lambda::Permission",
184184
"Properties": {
185185
"Action": "lambda:InvokeFunction",
@@ -276,7 +276,7 @@
276276
"BookHandlerServiceRole894768AD"
277277
]
278278
},
279-
"BookHandlerApiPermissionGETbooksbookid8BEBC7A6": {
279+
"BookHandlerApiPermissionrestapibooksexamplebooksapi4538F335GETbooksbookidA10D3CE2": {
280280
"Type": "AWS::Lambda::Permission",
281281
"Properties": {
282282
"Action": "lambda:InvokeFunction",
@@ -317,7 +317,7 @@
317317
}
318318
}
319319
},
320-
"BookHandlerApiPermissionTestGETbooksbookid7E089259": {
320+
"BookHandlerApiPermissionTestrestapibooksexamplebooksapi4538F335GETbooksbookidAB5191B6": {
321321
"Type": "AWS::Lambda::Permission",
322322
"Properties": {
323323
"Action": "lambda:InvokeFunction",
@@ -354,7 +354,7 @@
354354
}
355355
}
356356
},
357-
"BookHandlerApiPermissionDELETEbooksbookid56D0DC9D": {
357+
"BookHandlerApiPermissionrestapibooksexamplebooksapi4538F335DELETEbooksbookidB3A85313": {
358358
"Type": "AWS::Lambda::Permission",
359359
"Properties": {
360360
"Action": "lambda:InvokeFunction",
@@ -395,7 +395,7 @@
395395
}
396396
}
397397
},
398-
"BookHandlerApiPermissionTestDELETEbooksbookid3E3975F4": {
398+
"BookHandlerApiPermissionTestrestapibooksexamplebooksapi4538F335DELETEbooksbookid9308C830": {
399399
"Type": "AWS::Lambda::Permission",
400400
"Properties": {
401401
"Action": "lambda:InvokeFunction",
@@ -492,7 +492,7 @@
492492
"HelloServiceRole1E55EA16"
493493
]
494494
},
495-
"HelloApiPermissionANY16B11477": {
495+
"HelloApiPermissionrestapibooksexamplebooksapi4538F335ANYE385693C": {
496496
"Type": "AWS::Lambda::Permission",
497497
"Properties": {
498498
"Action": "lambda:InvokeFunction",
@@ -533,7 +533,7 @@
533533
}
534534
}
535535
},
536-
"HelloApiPermissionTestANY9A757A77": {
536+
"HelloApiPermissionTestrestapibooksexamplebooksapi4538F335ANY46B0DA7B": {
537537
"Type": "AWS::Lambda::Permission",
538538
"Properties": {
539539
"Action": "lambda:InvokeFunction",
@@ -920,4 +920,4 @@
920920
}
921921
}
922922
}
923-
}
923+
}

packages/@aws-cdk/aws-apigateway/test/integ.restapi.expected.json

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"myapiv1toysPUT59AFBBC2",
2727
"myapiv1toysA55FCBC4"
2828
],
29-
"DeletionPolicy": "Retain",
30-
"UpdateReplacePolicy": "Retain"
29+
"UpdateReplacePolicy": "Retain",
30+
"DeletionPolicy": "Retain"
3131
},
3232
"myapiDeploymentStagebeta96434BEB": {
3333
"Type": "AWS::ApiGateway::Stage",
@@ -332,6 +332,64 @@
332332
}
333333
}
334334
},
335+
"myapiApiKey43446CCF": {
336+
"Type": "AWS::ApiGateway::ApiKey",
337+
"Properties": {
338+
"Enabled": true,
339+
"StageKeys": [
340+
{
341+
"RestApiId": {
342+
"Ref": "myapi4C7BF186"
343+
},
344+
"StageName": {
345+
"Ref": "myapiDeploymentStagebeta96434BEB"
346+
}
347+
}
348+
]
349+
}
350+
},
351+
"myapiUsagePlan56F9C4F2": {
352+
"Type": "AWS::ApiGateway::UsagePlan",
353+
"Properties": {
354+
"ApiStages": [
355+
{
356+
"ApiId": {
357+
"Ref": "myapi4C7BF186"
358+
},
359+
"Stage": {
360+
"Ref": "myapiDeploymentStagebeta96434BEB"
361+
},
362+
"Throttle": {
363+
"/v1/toys/GET": {
364+
"BurstLimit": 2,
365+
"RateLimit": 10
366+
}
367+
}
368+
}
369+
],
370+
"Description": "Free tier monthly usage plan",
371+
"Quota": {
372+
"Limit": 10000,
373+
"Period": "MONTH"
374+
},
375+
"Throttle": {
376+
"RateLimit": 5
377+
},
378+
"UsagePlanName": "Basic"
379+
}
380+
},
381+
"myapiUsagePlanUsagePlanKeyResource050D133F": {
382+
"Type": "AWS::ApiGateway::UsagePlanKey",
383+
"Properties": {
384+
"KeyId": {
385+
"Ref": "myapiApiKey43446CCF"
386+
},
387+
"KeyType": "API_KEY",
388+
"UsagePlanId": {
389+
"Ref": "myapiUsagePlan56F9C4F2"
390+
}
391+
}
392+
},
335393
"MyHandlerServiceRoleFFA06653": {
336394
"Type": "AWS::IAM::Role",
337395
"Properties": {
@@ -392,7 +450,7 @@
392450
"MyHandlerServiceRoleFFA06653"
393451
]
394452
},
395-
"MyHandlerApiPermissionGETv1toys8E10C024": {
453+
"MyHandlerApiPermissiontestapigatewayrestapimyapi1AE401C4GETv1toys00F704BC": {
396454
"Type": "AWS::Lambda::Permission",
397455
"Properties": {
398456
"Action": "lambda:InvokeFunction",
@@ -433,7 +491,7 @@
433491
}
434492
}
435493
},
436-
"MyHandlerApiPermissionTestGETv1toys499738A6": {
494+
"MyHandlerApiPermissionTesttestapigatewayrestapimyapi1AE401C4GETv1toysDBCC8082": {
437495
"Type": "AWS::Lambda::Permission",
438496
"Properties": {
439497
"Action": "lambda:InvokeFunction",
@@ -470,7 +528,7 @@
470528
}
471529
}
472530
},
473-
"MyHandlerApiPermissionGETv1books376A9081": {
531+
"MyHandlerApiPermissiontestapigatewayrestapimyapi1AE401C4GETv1books96EB3DB8": {
474532
"Type": "AWS::Lambda::Permission",
475533
"Properties": {
476534
"Action": "lambda:InvokeFunction",
@@ -511,7 +569,7 @@
511569
}
512570
}
513571
},
514-
"MyHandlerApiPermissionTestGETv1booksB64C41EB": {
572+
"MyHandlerApiPermissionTesttestapigatewayrestapimyapi1AE401C4GETv1books906B3BB6": {
515573
"Type": "AWS::Lambda::Permission",
516574
"Properties": {
517575
"Action": "lambda:InvokeFunction",
@@ -548,7 +606,7 @@
548606
}
549607
}
550608
},
551-
"MyHandlerApiPermissionPOSTv1booksAC487705": {
609+
"MyHandlerApiPermissiontestapigatewayrestapimyapi1AE401C4POSTv1booksA48C273B": {
552610
"Type": "AWS::Lambda::Permission",
553611
"Properties": {
554612
"Action": "lambda:InvokeFunction",
@@ -589,7 +647,7 @@
589647
}
590648
}
591649
},
592-
"MyHandlerApiPermissionTestPOSTv1books6E15773F": {
650+
"MyHandlerApiPermissionTesttestapigatewayrestapimyapi1AE401C4POSTv1booksA566985D": {
593651
"Type": "AWS::Lambda::Permission",
594652
"Properties": {
595653
"Action": "lambda:InvokeFunction",
@@ -625,62 +683,6 @@
625683
]
626684
}
627685
}
628-
},
629-
"myapiUsagePlan56F9C4F2": {
630-
"Type": "AWS::ApiGateway::UsagePlan",
631-
"Properties": {
632-
"ApiStages": [
633-
{
634-
"ApiId": {
635-
"Ref": "myapi4C7BF186"
636-
},
637-
"Stage": {
638-
"Ref": "myapiDeploymentStagebeta96434BEB"
639-
},
640-
"Throttle": {
641-
"/v1/toys/GET": {
642-
"BurstLimit": 2,
643-
"RateLimit": 10
644-
}
645-
}
646-
}
647-
],
648-
"Throttle": { "RateLimit": 5 },
649-
"Description": "Free tier monthly usage plan",
650-
"Quota": {
651-
"Limit": 10000,
652-
"Period": "MONTH"
653-
},
654-
"UsagePlanName": "Basic"
655-
}
656-
},
657-
"myapiUsagePlanUsagePlanKeyResource050D133F": {
658-
"Type": "AWS::ApiGateway::UsagePlanKey",
659-
"Properties": {
660-
"KeyId": {
661-
"Ref": "myapiApiKey43446CCF"
662-
},
663-
"KeyType": "API_KEY",
664-
"UsagePlanId": {
665-
"Ref": "myapiUsagePlan56F9C4F2"
666-
}
667-
}
668-
},
669-
"myapiApiKey43446CCF": {
670-
"Type": "AWS::ApiGateway::ApiKey",
671-
"Properties": {
672-
"Enabled": true,
673-
"StageKeys": [
674-
{
675-
"RestApiId": {
676-
"Ref": "myapi4C7BF186"
677-
},
678-
"StageName": {
679-
"Ref": "myapiDeploymentStagebeta96434BEB"
680-
}
681-
}
682-
]
683-
}
684686
}
685687
},
686688
"Outputs": {
@@ -711,4 +713,4 @@
711713
}
712714
}
713715
}
714-
}
716+
}

0 commit comments

Comments
 (0)