Skip to content

Commit

Permalink
feat(apigateway): import API keys (aws#9155)
Browse files Browse the repository at this point in the history
add fromApiKeyId import method to the ApiKey construct

closes aws#8367

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
corymhall authored Aug 4, 2020
1 parent e3ae645 commit e3f6ae3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-apigateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<api-key-id>');
```

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.
Expand Down
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-apigateway/lib/api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = { }) {
Expand Down
1 change: 0 additions & 1 deletion packages/@aws-cdk/aws-apigateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 24 additions & 1 deletion packages/@aws-cdk/aws-apigateway/test/test.api-key.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
},
};

0 comments on commit e3f6ae3

Please sign in to comment.