Skip to content

Commit

Permalink
fix(apigateway): stack update fails to replace api key (#12745)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
Niranjan Jayakar committed Jan 28, 2021
1 parent dfc765a commit ffe7e42
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 31 deletions.
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-apigateway/lib/usage-plan.ts
Expand Up @@ -179,8 +179,10 @@ export class UsagePlan extends Resource {
* @param apiKey
*/
public addApiKey(apiKey: IApiKey): void {
const prefix = 'UsagePlanKeyResource';

// Postfixing apikey id only from the 2nd child, to keep physicalIds of UsagePlanKey for existing CDK apps unmodifed.
const id = `UsagePlanKeyResource:${Names.nodeUniqueId(apiKey.node)}`;
const id = this.node.tryFindChild(prefix) ? `${prefix}:${Names.nodeUniqueId(apiKey.node)}` : prefix;

new CfnUsagePlanKey(this, id, {
keyId: apiKey.keyId,
Expand Down
Expand Up @@ -602,7 +602,7 @@
"UsagePlanName": "Basic"
}
},
"myapiUsagePlanUsagePlanKeyResourcetestapigatewayrestapimyapiApiKeyC43601CB600D112D": {
"myapiUsagePlanUsagePlanKeyResource050D133F": {
"Type": "AWS::ApiGateway::UsagePlanKey",
"Properties": {
"KeyId": {
Expand Down
Expand Up @@ -3,7 +3,7 @@
"myusageplan4B391740": {
"Type": "AWS::ApiGateway::UsagePlan"
},
"myusageplanUsagePlanKeyResourcetestapigatewayusageplanmultikeymyapikey1DDABC389A2809A73": {
"myusageplanUsagePlanKeyResource095B4EA9": {
"Type": "AWS::ApiGateway::UsagePlanKey",
"Properties": {
"KeyId": {
Expand Down
28 changes: 0 additions & 28 deletions packages/@aws-cdk/aws-apigateway/test/usage-plan.test.ts
Expand Up @@ -205,32 +205,4 @@ describe('usage plan', () => {
},
}, ResourcePart.Properties);
});

test('UsagePlanKeys have unique logical ids', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'my-stack');
const usagePlan = new apigateway.UsagePlan(stack, 'my-usage-plan');
const apiKey1 = new apigateway.ApiKey(stack, 'my-api-key-1', {
apiKeyName: 'my-api-key-1',
});
const apiKey2 = new apigateway.ApiKey(stack, 'my-api-key-2', {
apiKeyName: 'my-api-key-2',
});

// WHEN
usagePlan.addApiKey(apiKey1);
usagePlan.addApiKey(apiKey2);

// THEN
const template = app.synth().getStackByName(stack.stackName).template;
const logicalIds = Object.entries(template.Resources)
.filter(([_, v]) => (v as any).Type === 'AWS::ApiGateway::UsagePlanKey')
.map(([k, _]) => k);

expect(logicalIds).toEqual([
'myusageplanUsagePlanKeyResourcemystackmyapikey1EE9AA1B359121274',
'myusageplanUsagePlanKeyResourcemystackmyapikey2B4E8EB1456DC88E9',
]);
});
});

0 comments on commit ffe7e42

Please sign in to comment.