Skip to content

Commit e3f6ae3

Browse files
authored
feat(apigateway): import API keys (#9155)
add fromApiKeyId import method to the ApiKey construct closes #8367 *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent e3ae645 commit e3f6ae3

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

packages/@aws-cdk/aws-apigateway/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ const key = api.addApiKey('ApiKey', {
200200
});
201201
```
202202

203+
Existing API keys can also be imported into a CDK app using its id.
204+
205+
```ts
206+
const importedKey = ApiKey.fromApiKeyId(this, 'imported-key', '<api-key-id>');
207+
```
208+
203209
In scenarios where you need to create a single api key and configure rate limiting for it, you can use `RateLimitedApiKey`.
204210
This construct lets you specify rate limiting properties which should be applied only to the api key being created.
205211
The API key created has the specified rate limits, such as quota and throttles, applied.

packages/@aws-cdk/aws-apigateway/lib/api-key.ts

+12
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ export interface ApiKeyProps extends ApiKeyOptions {
8181
* for Method resources that require an Api Key.
8282
*/
8383
export class ApiKey extends Resource implements IApiKey {
84+
85+
/**
86+
* Import an ApiKey by its Id
87+
*/
88+
public static fromApiKeyId(scope: Construct, id: string, apiKeyId: string): IApiKey {
89+
class Import extends Resource implements IApiKey {
90+
public keyId = apiKeyId;
91+
}
92+
93+
return new Import(scope, id);
94+
}
95+
8496
public readonly keyId: string;
8597

8698
constructor(scope: Construct, id: string, props: ApiKeyProps = { }) {

packages/@aws-cdk/aws-apigateway/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@
115115
"from-method:@aws-cdk/aws-apigateway.Resource",
116116
"duration-prop-type:@aws-cdk/aws-apigateway.QuotaSettings.period",
117117
"duration-prop-type:@aws-cdk/aws-apigateway.ResponseType.INTEGRATION_TIMEOUT",
118-
"from-method:@aws-cdk/aws-apigateway.ApiKey",
119118
"ref-via-interface:@aws-cdk/aws-apigateway.ApiKeyProps.resources",
120119
"props-physical-name:@aws-cdk/aws-apigateway.DeploymentProps",
121120
"props-physical-name:@aws-cdk/aws-apigateway.MethodProps",

packages/@aws-cdk/aws-apigateway/test/test.api-key.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, haveResource, ResourcePart } from '@aws-cdk/assert';
1+
import { expect, haveResource, haveResourceLike, ResourcePart } from '@aws-cdk/assert';
22
import * as cdk from '@aws-cdk/core';
33
import { Test } from 'nodeunit';
44
import * as apigateway from '../lib';
@@ -43,4 +43,27 @@ export = {
4343

4444
test.done();
4545
},
46+
47+
'use an imported api key'(test: Test) {
48+
// GIVEN
49+
const stack = new cdk.Stack();
50+
const api = new apigateway.RestApi(stack, 'test-api', { cloudWatchRole: false, deploy: true, deployOptions: { stageName: 'test' } });
51+
api.root.addMethod('GET'); // api must have atleast one method.
52+
53+
// WHEN
54+
const importedKey = apigateway.ApiKey.fromApiKeyId(stack, 'imported', 'KeyIdabc');
55+
api.addUsagePlan('plan', {
56+
apiKey: importedKey,
57+
});
58+
59+
// THEN
60+
expect(stack).to(haveResourceLike('AWS::ApiGateway::UsagePlanKey', {
61+
KeyId: 'KeyIdabc',
62+
KeyType: 'API_KEY',
63+
UsagePlanId: {
64+
Ref: 'testapiplan1B111AFF',
65+
},
66+
}));
67+
test.done();
68+
},
4669
};

0 commit comments

Comments
 (0)