diff --git a/packages/@aws-cdk/aws-apigateway/README.md b/packages/@aws-cdk/aws-apigateway/README.md index 89d2d721dc849..a2553cea63fac 100644 --- a/packages/@aws-cdk/aws-apigateway/README.md +++ b/packages/@aws-cdk/aws-apigateway/README.md @@ -200,6 +200,12 @@ const key = api.addApiKey('ApiKey', { }); ``` +Existing API keys can also be imported into a CDK app using its id. + +```ts +const importedKey = ApiKey.fromApiKeyId(this, 'imported-key', ''); +``` + In scenarios where you need to create a single api key and configure rate limiting for it, you can use `RateLimitedApiKey`. This construct lets you specify rate limiting properties which should be applied only to the api key being created. The API key created has the specified rate limits, such as quota and throttles, applied. diff --git a/packages/@aws-cdk/aws-apigateway/lib/api-key.ts b/packages/@aws-cdk/aws-apigateway/lib/api-key.ts index 172b77aa40309..dc705e73c939e 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/api-key.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/api-key.ts @@ -81,6 +81,18 @@ export interface ApiKeyProps extends ApiKeyOptions { * for Method resources that require an Api Key. */ export class ApiKey extends Resource implements IApiKey { + + /** + * Import an ApiKey by its Id + */ + public static fromApiKeyId(scope: Construct, id: string, apiKeyId: string): IApiKey { + class Import extends Resource implements IApiKey { + public keyId = apiKeyId; + } + + return new Import(scope, id); + } + public readonly keyId: string; constructor(scope: Construct, id: string, props: ApiKeyProps = { }) { diff --git a/packages/@aws-cdk/aws-apigateway/package.json b/packages/@aws-cdk/aws-apigateway/package.json index 6816f6cc02ab7..668185ab89589 100644 --- a/packages/@aws-cdk/aws-apigateway/package.json +++ b/packages/@aws-cdk/aws-apigateway/package.json @@ -115,7 +115,6 @@ "from-method:@aws-cdk/aws-apigateway.Resource", "duration-prop-type:@aws-cdk/aws-apigateway.QuotaSettings.period", "duration-prop-type:@aws-cdk/aws-apigateway.ResponseType.INTEGRATION_TIMEOUT", - "from-method:@aws-cdk/aws-apigateway.ApiKey", "ref-via-interface:@aws-cdk/aws-apigateway.ApiKeyProps.resources", "props-physical-name:@aws-cdk/aws-apigateway.DeploymentProps", "props-physical-name:@aws-cdk/aws-apigateway.MethodProps", diff --git a/packages/@aws-cdk/aws-apigateway/test/test.api-key.ts b/packages/@aws-cdk/aws-apigateway/test/test.api-key.ts index e32f7be5149d5..e7db715036bc6 100644 --- a/packages/@aws-cdk/aws-apigateway/test/test.api-key.ts +++ b/packages/@aws-cdk/aws-apigateway/test/test.api-key.ts @@ -1,4 +1,4 @@ -import { expect, haveResource, ResourcePart } from '@aws-cdk/assert'; +import { expect, haveResource, haveResourceLike, ResourcePart } from '@aws-cdk/assert'; import * as cdk from '@aws-cdk/core'; import { Test } from 'nodeunit'; import * as apigateway from '../lib'; @@ -43,4 +43,27 @@ export = { test.done(); }, + + 'use an imported api key'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } }); + api.root.addMethod('GET'); // api must have atleast one method. + + // WHEN + const importedKey = apigateway.ApiKey.fromApiKeyId(stack, 'imported', 'KeyIdabc'); + api.addUsagePlan('plan', { + apiKey: importedKey, + }); + + // THEN + expect(stack).to(haveResourceLike('AWS::ApiGateway::UsagePlanKey', { + KeyId: 'KeyIdabc', + KeyType: 'API_KEY', + UsagePlanId: { + Ref: 'testapiplan1B111AFF', + }, + })); + test.done(); + }, };