Skip to content

Commit 96cbe32

Browse files
author
Niranjan Jayakar
authored
fix(apigateway): cannot remove first api key from usage plan (#12505)
This is caused by a [commit introduced] back in Nov 2019. The original change tried to be overly ambitious in not triggering a resource replacement on customers who already had a single key usage plan configured. This results in stack update failures for a usage plan with multiple keys, when the **first** key is removed. Removal of the first key re-adjusts the logical ids of all the keys in a way that makes the update look like the first key is changing and a subsequent key is being removed. The fix is to simply not special case the first key. Resource replacement of Usage Plan Key resource should not create any impact on running applications. fixes #11876 [commit introduced]: 142bd0e ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 638b995 commit 96cbe32

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

packages/@aws-cdk/aws-apigateway/lib/usage-plan.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ export class UsagePlan extends Resource {
179179
* @param apiKey
180180
*/
181181
public addApiKey(apiKey: IApiKey): void {
182-
const prefix = 'UsagePlanKeyResource';
183-
184182
// Postfixing apikey id only from the 2nd child, to keep physicalIds of UsagePlanKey for existing CDK apps unmodifed.
185-
const id = this.node.tryFindChild(prefix) ? `${prefix}:${Names.nodeUniqueId(apiKey.node)}` : prefix;
183+
const id = `UsagePlanKeyResource:${Names.nodeUniqueId(apiKey.node)}`;
186184

187185
new CfnUsagePlanKey(this, id, {
188186
keyId: apiKey.keyId,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@
602602
"UsagePlanName": "Basic"
603603
}
604604
},
605-
"myapiUsagePlanUsagePlanKeyResource050D133F": {
605+
"myapiUsagePlanUsagePlanKeyResourcetestapigatewayrestapimyapiApiKeyC43601CB600D112D": {
606606
"Type": "AWS::ApiGateway::UsagePlanKey",
607607
"Properties": {
608608
"KeyId": {

packages/@aws-cdk/aws-apigateway/test/integ.usage-plan.multikey.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"myusageplan4B391740": {
44
"Type": "AWS::ApiGateway::UsagePlan"
55
},
6-
"myusageplanUsagePlanKeyResource095B4EA9": {
6+
"myusageplanUsagePlanKeyResourcetestapigatewayusageplanmultikeymyapikey1DDABC389A2809A73": {
77
"Type": "AWS::ApiGateway::UsagePlanKey",
88
"Properties": {
99
"KeyId": {

packages/@aws-cdk/aws-apigateway/test/usage-plan.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,32 @@ describe('usage plan', () => {
205205
},
206206
}, ResourcePart.Properties);
207207
});
208+
209+
test('UsagePlanKeys have unique logical ids', () => {
210+
// GIVEN
211+
const app = new cdk.App();
212+
const stack = new cdk.Stack(app, 'my-stack');
213+
const usagePlan = new apigateway.UsagePlan(stack, 'my-usage-plan');
214+
const apiKey1 = new apigateway.ApiKey(stack, 'my-api-key-1', {
215+
apiKeyName: 'my-api-key-1',
216+
});
217+
const apiKey2 = new apigateway.ApiKey(stack, 'my-api-key-2', {
218+
apiKeyName: 'my-api-key-2',
219+
});
220+
221+
// WHEN
222+
usagePlan.addApiKey(apiKey1);
223+
usagePlan.addApiKey(apiKey2);
224+
225+
// THEN
226+
const template = app.synth().getStackByName(stack.stackName).template;
227+
const logicalIds = Object.entries(template.Resources)
228+
.filter(([_, v]) => (v as any).Type === 'AWS::ApiGateway::UsagePlanKey')
229+
.map(([k, _]) => k);
230+
231+
expect(logicalIds).toEqual([
232+
'myusageplanUsagePlanKeyResourcemystackmyapikey1EE9AA1B359121274',
233+
'myusageplanUsagePlanKeyResourcemystackmyapikey2B4E8EB1456DC88E9',
234+
]);
235+
});
208236
});

0 commit comments

Comments
 (0)