Skip to content

Commit ffe7e42

Browse files
author
Niranjan Jayakar
authored
fix(apigateway): stack update fails to replace api key (#12745)
This reverts commit 96cbe32. The above commit changed the logical id layout of API keys. It turns out that ApiKey resource types cannot be replaced without explicitly specifying, and changing, the API key name. See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-name fixes #12698 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent dfc765a commit ffe7e42

File tree

4 files changed

+5
-31
lines changed

4 files changed

+5
-31
lines changed

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

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

185187
new CfnUsagePlanKey(this, id, {
186188
keyId: apiKey.keyId,

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

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

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

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

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -205,32 +205,4 @@ 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-
});
236208
});

0 commit comments

Comments
 (0)