Skip to content

[apigateway] Cannot call UsagePlan.addApiKey multiple times. #4860

@igilham

Description

@igilham

It appears to be impossible to add multiple API keys to a usage plan in the API Gateway module. Adding a single key works fine, but there is a construct naming conflict when adding multiple keys.

Reproduction Steps

// const api: apigateway.RestApi = new RestApi()....;

const plan = api.addUsagePlan('DefaultUsagePlan', {
    description: `Default usage plan for ${api.node.id}`,
    name: 'DefaultUsagePlan'
});
plan.addApiKey(api.addApiKey('ClientKey'));
plan.addApiKey(api.addApiKey('TestingKey'));

Error Log

When adding a second API key, I get an error complaining about the reuse of a construct name in CDK.

There is already a Construct with name 'UsagePlanKeyResource' in UsagePlan [DefaultUsagePlan]

.../infrastructure/node_modules/@aws-cdk/core/lib/construct.ts:48
        throw new Error(`Validation failed with the following errors:\n  ${errorList}`);
              ^
Error: Validation failed with the following errors:
  [LambdaStack/BotAudioVideoApi] The REST API doesn't contain any methods
    at Function.synth (.../infrastructure/node_modules/@aws-cdk/core/lib/construct.ts:48:15)
    at App.synth (.../infrastructure/node_modules/@aws-cdk/core/lib/app.ts:142:36)
    at process.App.process.once (.../infrastructure/node_modules/@aws-cdk/core/lib/app.ts:121:45)
    at Object.onceWrapper (events.js:286:20)
    at process.emit (events.js:198:13)
    at process.EventEmitter.emit (domain.js:448:20)
    at process.emit (.../infrastructure/node_modules/source-map-support/source-map-support.js:465:21)
    at process.topLevelDomainCallback (domain.js:126:23)

Environment

  • CLI Version : 1.12.0, 1.15.0
  • Framework Version: 1.12.0, 1.15.0
  • OS : MacOS
  • Language : Typescript

Other

I initially tried this using CDK v1.12.0. I have reproduced it in CDK v1.15.0.

There is a work-around using CfnUsagePlanKey:

// const api: apigateway.RestApi = new RestApi()....;

const clientKey = api.addApiKey('ClientKey');
const testingKey = api.addApiKey('TestingKey')
const clientUsagePlanKey = new apiGateway.CfnUsagePlanKey(this, 'ClientUsagePlanKey', {
    keyId: clientKey.keyId,
    keyType: 'API_KEY',
    usagePlanId: plan.usagePlanId
});
const testingUsagePlanKey = new apiGateway.CfnUsagePlanKey(this, 'TestingUsagePlanKey', {
    keyId: testingKey.keyId,
    keyType: 'API_KEY',
    usagePlanId: plan.usagePlanId
});

This is 🐛 Bug Report

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions