diff --git a/packages/@aws-cdk/aws-apigateway/README.md b/packages/@aws-cdk/aws-apigateway/README.md index f83adfa9818f1..803ed51d906f6 100644 --- a/packages/@aws-cdk/aws-apigateway/README.md +++ b/packages/@aws-cdk/aws-apigateway/README.md @@ -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. diff --git a/packages/@aws-cdk/aws-apigateway/lib/api-key.ts b/packages/@aws-cdk/aws-apigateway/lib/api-key.ts index c94a67ead901c..172b77aa40309 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/api-key.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/api-key.ts @@ -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. @@ -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; } /** @@ -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; diff --git a/packages/@aws-cdk/aws-apigateway/lib/restapi.ts b/packages/@aws-cdk/aws-apigateway/lib/restapi.ts index 4e372f91c996c..5a43b562ff279 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/restapi.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/restapi.ts @@ -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'; @@ -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, }); } diff --git a/packages/@aws-cdk/aws-apigateway/test/test.restapi.ts b/packages/@aws-cdk/aws-apigateway/test/test.restapi.ts index 2be05b8e8c1f1..d512b924cfe98 100644 --- a/packages/@aws-cdk/aws-apigateway/test/test.restapi.ts +++ b/packages/@aws-cdk/aws-apigateway/test/test.restapi.ts @@ -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();