Skip to content

Commit

Permalink
feat(apigateway): specify API key name and value in addApiKey() (aw…
Browse files Browse the repository at this point in the history
  • Loading branch information
njlynch authored May 13, 2020
1 parent 0105efd commit e93da2c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-apigateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ plan.addApiStage({
});
```

The name and value of the API Key can be specified at creation; if not
provided, a name and value will be automatically generated by API Gateway.

```ts
const key = api.addApiKey('ApiKey', {
apiKeyName: 'myApiKey1',
value: 'MyApiKeyThatIsAtLeast20Characters',
});
```

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
29 changes: 21 additions & 8 deletions packages/@aws-cdk/aws-apigateway/lib/api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,29 @@ export interface IApiKey extends IResourceBase {
readonly keyId: string;
}

/**
* The options for creating an API Key.
*/
export interface ApiKeyOptions extends ResourceOptions {
/**
* A name for the API key. If you don't specify a name, AWS CloudFormation generates a unique physical ID and uses that ID for the API key name.
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-name
* @default automically generated name
*/
readonly apiKeyName?: string;

/**
* The value of the API key. Must be at least 20 characters long.
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-value
* @default none
*/
readonly value?: string;
}

/**
* ApiKey Properties.
*/
export interface ApiKeyProps extends ResourceOptions {
export interface ApiKeyProps extends ApiKeyOptions {
/**
* [disable-awslint:ref-via-interface]
* A list of resources this api key is associated with.
Expand Down Expand Up @@ -53,13 +72,6 @@ export interface ApiKeyProps extends ResourceOptions {
* @default false
*/
readonly generateDistinctId?: boolean;

/**
* A name for the API key. If you don't specify a name, AWS CloudFormation generates a unique physical ID and uses that ID for the API key name.
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-apikey.html#cfn-apigateway-apikey-name
* @default automically generated name
*/
readonly apiKeyName?: string;
}

/**
Expand All @@ -83,6 +95,7 @@ export class ApiKey extends Resource implements IApiKey {
generateDistinctId: props.generateDistinctId,
name: this.physicalName,
stageKeys: this.renderStageKeys(props.resources),
value: props.value,
});

this.keyId = resource.ref;
Expand Down
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IVpcEndpoint } from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import { CfnOutput, Construct, IResource as IResourceBase, Resource, Stack } from '@aws-cdk/core';
import { ApiDefinition } from './api-definition';
import { ApiKey, IApiKey } from './api-key';
import { ApiKey, ApiKeyOptions, IApiKey } from './api-key';
import { CfnAccount, CfnRestApi } from './apigateway.generated';
import { CorsOptions } from './cors';
import { Deployment } from './deployment';
Expand Down Expand Up @@ -496,9 +496,10 @@ export class RestApi extends RestApiBase implements IRestApi {
/**
* Add an ApiKey
*/
public addApiKey(id: string): IApiKey {
public addApiKey(id: string, options?: ApiKeyOptions): IApiKey {
return new ApiKey(this, id, {
resources: [this],
...options,
});
}

Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/test.restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,34 @@ export = {
test.done();
},

'addApiKey is supported'(test: Test) {
// GIVEN
const stack = new Stack();
const api = new apigw.RestApi(stack, 'myapi');
api.root.addMethod('OPTIONS');

// WHEN
api.addApiKey('myapikey', {
apiKeyName: 'myApiKey1',
value: '01234567890ABCDEFabcdef',
});

// THEN
expect(stack).to(haveResource('AWS::ApiGateway::ApiKey', {
Enabled: true,
Name: 'myApiKey1',
StageKeys: [
{
RestApiId: { Ref: 'myapi162F20B8' },
StageName: { Ref: 'myapiDeploymentStageprod329F21FF' },
},
],
Value: '01234567890ABCDEFabcdef',
}));

test.done();
},

'addModel is supported'(test: Test) {
// GIVEN
const stack = new Stack();
Expand Down

0 comments on commit e93da2c

Please sign in to comment.