Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apigateway): expose endpointconfiguration to include vpcEndpointIds #6078

Merged
merged 14 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-apigateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,42 @@ OPTIONS added to them.
See [#906](https://github.com/aws/aws-cdk/issues/906) for a list of CORS
features which are not yet supported.

### Endpoint Configuration
API gateway allows you to specify an
[API Endpoint Type](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-api-endpoint-types.html).
To define an endpoint type for the API gateway, use `endpointConfiguration` property:

```ts
const api = new apigw.RestApi(stack, 'api', {
endpointConfiguration: {
types: [ apigw.EndpointType.EDGE ]
}
});
```

You can also create an association between your Rest API and a VPC endpoint. By doing so,
API Gateway will generate a new
Route53 Alias DNS record which you can use to invoke your private APIs. More info can be found
[here](https://docs.aws.amazon.com/apigateway/latest/developerguide/associate-private-api-with-vpc-endpoint.html).

Here is an example:

```ts
const someEndpoint: IVpcEndpoint = /* Get or Create endpoint here */
const api = new apigw.RestApi(stack, 'api', {
endpointConfiguration: {
types: [ apigw.EndpointType.PRIVATE ],
vpcEndpoints: [ someEndpoint ]
}
});
```

By performing this association, we can invoke the API gateway using the following format:

```
https://{rest-api-id}-{vpce-id}.execute-api.{region}.amazonaws.com/{stage}
```

## APIGateway v2

APIGateway v2 APIs are now moved to its own package named `aws-apigatewayv2`. For backwards compatibility, existing
Expand Down
65 changes: 56 additions & 9 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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 { ApiKey, IApiKey } from './api-key';
Expand Down Expand Up @@ -92,6 +93,23 @@ export interface RestApiProps extends ResourceOptions {
*/
readonly description?: string;

/**
* The EndpointConfiguration property type specifies the endpoint types of a REST API
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-restapi-endpointconfiguration.html
*
* @default - No endpoint configuration
*/
readonly endpointConfiguration?: EndpointConfiguration;

/**
* A list of the endpoint types of the API. Use this property when creating
* an API.
*
* @default - No endpoint types.
* @deprecated this property is deprecated, use endpointConfiguration instead
*/
readonly endpointTypes?: EndpointType[];

/**
* The source of the API key for metering requests according to a usage
* plan.
Expand All @@ -108,14 +126,6 @@ export interface RestApiProps extends ResourceOptions {
*/
readonly binaryMediaTypes?: string[];

/**
* A list of the endpoint types of the API. Use this property when creating
* an API.
*
* @default - No endpoint types.
*/
readonly endpointTypes?: EndpointType[];

/**
* Indicates whether to roll back the resource if a warning occurs while API
* Gateway is creating the RestApi resource.
Expand Down Expand Up @@ -232,7 +242,7 @@ export class RestApi extends Resource implements IRestApi {
failOnWarnings: props.failOnWarnings,
minimumCompressionSize: props.minimumCompressionSize,
binaryMediaTypes: props.binaryMediaTypes,
endpointConfiguration: props.endpointTypes ? { types: props.endpointTypes } : undefined,
endpointConfiguration: this.configureEndpoints(props),
apiKeySourceType: props.apiKeySourceType,
cloneFrom: props.cloneFrom ? props.cloneFrom.restApiId : undefined,
parameters: props.parameters
Expand Down Expand Up @@ -430,6 +440,43 @@ export class RestApi extends Resource implements IRestApi {

resource.node.addDependency(apiResource);
}

private configureEndpoints(props: RestApiProps): CfnRestApi.EndpointConfigurationProperty | undefined {
if (props.endpointTypes && props.endpointConfiguration) {
throw new Error('Only one of the RestApi props, endpointTypes or endpointConfiguration, is allowed');
}
if (props.endpointConfiguration) {
return {
types: props.endpointConfiguration.types,
vpcEndpointIds: props.endpointConfiguration?.vpcEndpoints?.map(vpcEndpoint => vpcEndpoint.vpcEndpointId)
};
}
if (props.endpointTypes) {
return { types: props.endpointTypes };
}
return undefined;
}
}

/**
* The endpoint configuration of a REST API, including VPCs and endpoint types.
*
* EndpointConfiguration is a property of the AWS::ApiGateway::RestApi resource.
*/
export interface EndpointConfiguration {
/**
* A list of endpoint types of an API or its custom domain name.
*
* @default - no endpoint types.
*/
readonly types: EndpointType[];

/**
* A list of VPC Endpoints against which to create Route53 ALIASes
*
* @default - no ALIASes are created for the endpoint.
*/
readonly vpcEndpoints?: IVpcEndpoint[];
}

export enum ApiKeySourceType {
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-apigateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"license": "Apache-2.0",
"devDependencies": {
"@aws-cdk/assert": "1.24.0",
"@aws-cdk/aws-ec2": "1.24.0",
"@types/nodeunit": "^0.0.30",
"cdk-build-tools": "1.24.0",
"cdk-integ-tools": "1.24.0",
Expand All @@ -73,6 +72,7 @@
},
"dependencies": {
"@aws-cdk/aws-certificatemanager": "1.24.0",
"@aws-cdk/aws-ec2": "1.24.0",
"@aws-cdk/aws-elasticloadbalancingv2": "1.24.0",
"@aws-cdk/aws-iam": "1.24.0",
"@aws-cdk/aws-lambda": "1.24.0",
Expand All @@ -81,6 +81,7 @@
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-certificatemanager": "1.24.0",
"@aws-cdk/aws-ec2": "1.24.0",
"@aws-cdk/aws-elasticloadbalancingv2": "1.24.0",
"@aws-cdk/aws-iam": "1.24.0",
"@aws-cdk/aws-lambda": "1.24.0",
Expand Down
Loading