Skip to content

Commit 23003f3

Browse files
IsmaelMartinezCurtis Eppel
authored and
Curtis Eppel
committed
feat(apigateway): configure endpoint types on SpecRestApi (aws#9068)
feat(apigateway): adding the ability to set the endpoint configuration for the OpenAPI 3.0 With this change, it will be possible to modify this by providing the endpointTypes as shown here: ``` const api = new apigateway.SpecRestApi(this, 'ExampleRestApi', { apiDefinition: apigateway.ApiDefinition.fromInline(replacedSwagger), endpointTypes: [apigateway.EndpointType.PRIVATE], }); ``` Note: For private endpoints you will still need to provide the `x-amazon-apigateway-endpoint-configuration` and `x-amazon-apigateway-policy` in your openApi file. The following is an example with both settings: ```json { "openapi": "3.0.2", "servers" : [ { "x-amazon-apigateway-endpoint-configuration": { "vpcEndpointIds": [ "vpce-00111a1111a1aa011" ] } } ], "paths": { ... }, "x-amazon-apigateway-policy": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": "*", "Action": [ "execute-api:Invoke", "execute-api:GET" ], "Resource": "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:*", "Condition": { "StringEquals": { "aws:sourceVpce": "vpce-00111a1111a1aa011" } } } ] } } ``` Checklist for this PR: 🧪 Testing: adding integration testing for private API gateway. 📄 Docs: Add example in the README documentation about how to create a private API gateway with swagger Fixes aws#9060 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 09e1fdf commit 23003f3

File tree

3 files changed

+76
-30
lines changed

3 files changed

+76
-30
lines changed

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ running on AWS Lambda, or any web application.
3838
- [Private Integrations](#private-integrations)
3939
- [Gateway Response](#gateway-response)
4040
- [OpenAPI Definition](#openapi-definition)
41+
- [Endpoint configuration](#endpoint-configuration)
4142
- [APIGateway v2](#apigateway-v2)
4243

4344
## Defining APIs
@@ -992,11 +993,29 @@ to configure these.
992993
There are a number of limitations in using OpenAPI definitions in API Gateway. Read the [Amazon API Gateway important
993994
notes for REST APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html#api-gateway-known-issues-rest-apis)
994995
for more details.
995-
996+
996997
**Note:** When starting off with an OpenAPI definition using `SpecRestApi`, it is not possible to configure some
997998
properties that can be configured directly in the OpenAPI specification file. This is to prevent people duplication
998999
of these properties and potential confusion.
9991000
1001+
### Endpoint configuration
1002+
1003+
By default, `SpecRestApi` will create an edge optimized endpoint.
1004+
1005+
This can be modified as shown below:
1006+
1007+
```ts
1008+
const api = new apigateway.SpecRestApi(this, 'ExampleRestApi', {
1009+
// ...
1010+
endpointTypes: [apigateway.EndpointType.PRIVATE]
1011+
});
1012+
```
1013+
1014+
**Note:** For private endpoints you will still need to provide the
1015+
[`x-amazon-apigateway-policy`](https://docs.aws.amazon.com/apigateway/latest/developerguide/openapi-extensions-policy.html) and
1016+
[`x-amazon-apigateway-endpoint-configuration`](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-endpoint-configuration.html)
1017+
in your openApi file.
1018+
10001019
## APIGateway v2
10011020
10021021
APIGateway v2 APIs are now moved to its own package named `aws-apigatewayv2`. For backwards compatibility, existing

packages/@aws-cdk/aws-apigateway/lib/restapi.ts

+32-29
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ export interface RestApiBaseProps {
159159
* @default - when no export name is given, output will be created without export
160160
*/
161161
readonly endpointExportName?: string;
162+
163+
/**
164+
* A list of the endpoint types of the API. Use this property when creating
165+
* an API.
166+
*
167+
* @default EndpointType.EDGE
168+
*/
169+
readonly endpointTypes?: EndpointType[];
162170
}
163171

164172
/**
@@ -218,18 +226,9 @@ export interface RestApiProps extends RestApiOptions {
218226
* The EndpointConfiguration property type specifies the endpoint types of a REST API
219227
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-restapi-endpointconfiguration.html
220228
*
221-
* @default - No endpoint configuration
229+
* @default EndpointType.EDGE
222230
*/
223231
readonly endpointConfiguration?: EndpointConfiguration;
224-
225-
/**
226-
* A list of the endpoint types of the API. Use this property when creating
227-
* an API.
228-
*
229-
* @default - No endpoint types.
230-
* @deprecated this property is deprecated, use endpointConfiguration instead
231-
*/
232-
readonly endpointTypes?: EndpointType[];
233232
}
234233

235234
/**
@@ -423,6 +422,25 @@ export abstract class RestApiBase extends Resource implements IRestApi {
423422
}
424423
}
425424
}
425+
426+
/**
427+
* @internal
428+
*/
429+
protected _configureEndpoints(props: RestApiProps): CfnRestApi.EndpointConfigurationProperty | undefined {
430+
if (props.endpointTypes && props.endpointConfiguration) {
431+
throw new Error('Only one of the RestApi props, endpointTypes or endpointConfiguration, is allowed');
432+
}
433+
if (props.endpointConfiguration) {
434+
return {
435+
types: props.endpointConfiguration.types,
436+
vpcEndpointIds: props.endpointConfiguration?.vpcEndpoints?.map(vpcEndpoint => vpcEndpoint.vpcEndpointId),
437+
};
438+
}
439+
if (props.endpointTypes) {
440+
return { types: props.endpointTypes };
441+
}
442+
return undefined;
443+
}
426444
}
427445

428446
/**
@@ -463,6 +481,7 @@ export class SpecRestApi extends RestApiBase {
463481
failOnWarnings: props.failOnWarnings,
464482
body: apiDefConfig.inlineDefinition ? apiDefConfig.inlineDefinition : undefined,
465483
bodyS3Location: apiDefConfig.inlineDefinition ? undefined : apiDefConfig.s3Location,
484+
endpointConfiguration: this._configureEndpoints(props),
466485
parameters: props.parameters,
467486
});
468487
this.node.defaultChild = resource;
@@ -560,7 +579,7 @@ export class RestApi extends RestApiBase {
560579
failOnWarnings: props.failOnWarnings,
561580
minimumCompressionSize: props.minimumCompressionSize,
562581
binaryMediaTypes: props.binaryMediaTypes,
563-
endpointConfiguration: this.configureEndpoints(props),
582+
endpointConfiguration: this._configureEndpoints(props),
564583
apiKeySourceType: props.apiKeySourceType,
565584
cloneFrom: props.cloneFrom ? props.cloneFrom.restApiId : undefined,
566585
parameters: props.parameters,
@@ -639,22 +658,6 @@ export class RestApi extends RestApiBase {
639658

640659
return [];
641660
}
642-
643-
private configureEndpoints(props: RestApiProps): CfnRestApi.EndpointConfigurationProperty | undefined {
644-
if (props.endpointTypes && props.endpointConfiguration) {
645-
throw new Error('Only one of the RestApi props, endpointTypes or endpointConfiguration, is allowed');
646-
}
647-
if (props.endpointConfiguration) {
648-
return {
649-
types: props.endpointConfiguration.types,
650-
vpcEndpointIds: props.endpointConfiguration?.vpcEndpoints?.map(vpcEndpoint => vpcEndpoint.vpcEndpointId),
651-
};
652-
}
653-
if (props.endpointTypes) {
654-
return { types: props.endpointTypes };
655-
}
656-
return undefined;
657-
}
658661
}
659662

660663
/**
@@ -666,7 +669,7 @@ export interface EndpointConfiguration {
666669
/**
667670
* A list of endpoint types of an API or its custom domain name.
668671
*
669-
* @default - no endpoint types.
672+
* @default EndpointType.EDGE
670673
*/
671674
readonly types: EndpointType[];
672675

@@ -752,4 +755,4 @@ class RootResource extends ResourceBase {
752755

753756
function ignore(_x: any) {
754757
return;
755-
}
758+
}

packages/@aws-cdk/aws-apigateway/test/test.restapi.ts

+24
Original file line numberDiff line numberDiff line change
@@ -1018,5 +1018,29 @@ export = {
10181018
}));
10191019
test.done();
10201020
},
1021+
1022+
'"endpointTypes" can be used to specify endpoint configuration for SpecRestApi'(test: Test) {
1023+
// GIVEN
1024+
const stack = new Stack();
1025+
1026+
// WHEN
1027+
const api = new apigw.SpecRestApi(stack, 'api', {
1028+
apiDefinition: apigw.ApiDefinition.fromInline({ foo: 'bar' }),
1029+
endpointTypes: [ apigw.EndpointType.EDGE, apigw.EndpointType.PRIVATE ],
1030+
});
1031+
1032+
api.root.addMethod('GET');
1033+
1034+
// THEN
1035+
expect(stack).to(haveResource('AWS::ApiGateway::RestApi', {
1036+
EndpointConfiguration: {
1037+
Types: [
1038+
'EDGE',
1039+
'PRIVATE',
1040+
],
1041+
},
1042+
}));
1043+
test.done();
1044+
},
10211045
},
10221046
};

0 commit comments

Comments
 (0)