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): configure endpoint types on SpecRestApi #9068

Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
dd9f704
feat(apigateway) adding the ability to set the endpoint configuration…
IsmaelMartinez Jul 14, 2020
00c3ce4
feat (apigateway): adding the ability for the endpoint configuration …
IsmaelMartinez Jul 14, 2020
8cd4b85
pr adding initial integration test. WIP still need to add the actual …
IsmaelMartinez Jul 14, 2020
97326bc
pr adding integration test
IsmaelMartinez Jul 14, 2020
7e6a4a8
minor documentation renaming to follow the pattern from the previous …
IsmaelMartinez Jul 14, 2020
88233ac
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 14, 2020
4c4caf6
changing the name for the private api gateway integration test
IsmaelMartinez Jul 15, 2020
b63a0e2
Merge branch 'ismaelmartinez/add-endpointconfiguration-for-openapi3' …
IsmaelMartinez Jul 15, 2020
62c6832
updating integration tests after doing a test run in local
IsmaelMartinez Jul 16, 2020
85ac49c
Trying removing the 'expected' integration test as I can't see what i…
IsmaelMartinez Jul 16, 2020
7c2bbc0
WIP reading the integration test file
IsmaelMartinez Jul 16, 2020
2756fee
wip: trying to workout the deployment ids for integration tests from …
IsmaelMartinez Jul 16, 2020
f327d67
removing the metatadata information from the integration test
IsmaelMartinez Jul 16, 2020
51adf34
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 16, 2020
43ff752
PR changes.
IsmaelMartinez Jul 16, 2020
7a097a1
Merge branch 'ismaelmartinez/add-endpointconfiguration-for-openapi3' …
IsmaelMartinez Jul 16, 2020
5fe131c
adding an starting underscore to the new internal function
IsmaelMartinez Jul 16, 2020
dc6f5ec
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 16, 2020
0bdf82e
Adding missing trailing comma in integration tests
IsmaelMartinez Jul 16, 2020
2727182
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 16, 2020
85092ad
removing tailing spaces
IsmaelMartinez Jul 16, 2020
6d3d066
Merge branch 'ismaelmartinez/add-endpointconfiguration-for-openapi3' …
IsmaelMartinez Jul 16, 2020
dfd0be0
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 16, 2020
b5755a8
wip: removing package-lock.json file.
IsmaelMartinez Jul 16, 2020
c54fd16
Merge branch 'ismaelmartinez/add-endpointconfiguration-for-openapi3' …
IsmaelMartinez Jul 16, 2020
ee7dda7
reverting yarn and package unintended changes
IsmaelMartinez Jul 16, 2020
7c03d7d
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 16, 2020
c0e65ab
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 16, 2020
44618d9
pr: documentation and testing changes on the back of @nija-at review
IsmaelMartinez Jul 17, 2020
a0a273e
fixing eslint issues
IsmaelMartinez Jul 17, 2020
18e98ab
eslint fixes
IsmaelMartinez Jul 17, 2020
7fe8f6b
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 17, 2020
d667b6a
move eslint fixes
IsmaelMartinez Jul 17, 2020
71b97ab
hopefully last eslint fixes
IsmaelMartinez Jul 17, 2020
670056c
fix integration test. Id changed
IsmaelMartinez Jul 17, 2020
4a6d1d8
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 19, 2020
d0bcfd9
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 20, 2020
688b3b2
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 20, 2020
62f8fe7
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 21, 2020
279df8c
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 21, 2020
99608de
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 21, 2020
e1c638e
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 23, 2020
ea34bd3
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 24, 2020
e7b18f9
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 27, 2020
10cedac
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 28, 2020
4ebc527
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 28, 2020
f03fe62
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Jul 30, 2020
671dd0a
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Aug 3, 2020
6337b1a
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
IsmaelMartinez Aug 3, 2020
640b142
modiry readme not to have a long line description
IsmaelMartinez Aug 4, 2020
4480b57
Update packages/@aws-cdk/aws-apigateway/README.md
IsmaelMartinez Aug 4, 2020
ea29348
Adding endpoint configuration to the table of contents
IsmaelMartinez Aug 4, 2020
efa3b94
removing integration tests
IsmaelMartinez Aug 4, 2020
80876ec
Update packages/@aws-cdk/aws-apigateway/lib/restapi.ts
IsmaelMartinez Aug 4, 2020
19ea67d
Update packages/@aws-cdk/aws-apigateway/lib/restapi.ts
IsmaelMartinez Aug 4, 2020
0af5923
Update packages/@aws-cdk/aws-apigateway/lib/restapi.ts
IsmaelMartinez Aug 4, 2020
d224d73
Update packages/@aws-cdk/aws-apigateway/README.md
Aug 4, 2020
d165bba
Merge branch 'master' into ismaelmartinez/add-endpointconfiguration-f…
mergify[bot] Aug 4, 2020
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
20 changes: 19 additions & 1 deletion packages/@aws-cdk/aws-apigateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -986,11 +986,29 @@ to configure these.
There are a number of limitations in using OpenAPI definitions in API Gateway. Read the [Amazon API Gateway important
notes for REST APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html#api-gateway-known-issues-rest-apis)
for more details.

**Note:** When starting off with an OpenAPI definition using `SpecRestApi`, it is not possible to configure some
properties that can be configured directly in the OpenAPI specification file. This is to prevent people duplication
of these properties and potential confusion.

### Endpoint configuration

By default, SpecRestApi will create an endpoint with a edge optimized endpoint configuration.

It is possible to modify this by providing the endpointConfiguration as shown here:

```ts
const api = new apigateway.SpecRestApi(this, 'ExampleRestApi', {
apiDefinition: apigateway.ApiDefinition.fromInline(replacedSwagger),
endpointConfiguration: {
types: [apigateway.EndpointType.PRIVATE],
},
});
```

**Note:** For private endpoints you will still need to provide the `x-amazon-apigateway-policy` in your openApi file.


## APIGateway v2

APIGateway v2 APIs are now moved to its own package named `aws-apigatewayv2`. For backwards compatibility, existing
Expand Down
51 changes: 26 additions & 25 deletions packages/@aws-cdk/aws-apigateway/lib/restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ export interface RestApiOptions extends ResourceOptions {
* @default - when no export name is given, output will be created without export
*/
readonly endpointExportName?: 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;
}

/**
Expand Down Expand Up @@ -207,20 +215,12 @@ export interface RestApiProps extends RestApiOptions {
*/
readonly apiKeySourceType?: ApiKeySourceType;

/**
* 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
* @deprecated this property is deprecated, use endpointConfiguration in the RestApiOptions instead
*/
readonly endpointTypes?: EndpointType[];
}
Expand Down Expand Up @@ -416,6 +416,22 @@ export abstract class RestApiBase extends Resource implements IRestApi {
}
}
}

protected 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;
}
}

/**
Expand Down Expand Up @@ -456,6 +472,7 @@ export class SpecRestApi extends RestApiBase {
failOnWarnings: props.failOnWarnings,
body: apiDefConfig.inlineDefinition ? apiDefConfig.inlineDefinition : undefined,
bodyS3Location: apiDefConfig.inlineDefinition ? undefined : apiDefConfig.s3Location,
endpointConfiguration: this.configureEndpoints(props),
parameters: props.parameters,
});
this.node.defaultChild = resource;
Expand Down Expand Up @@ -632,22 +649,6 @@ export class RestApi extends RestApiBase {

return [];
}

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;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
{
"Resources": {
"myprivateapiD2D027EE": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"openapi": "3.0.2",
"info": {
"version": "1.0.0",
"title": "Test API for CDK"
},
"paths": {
"/pets": {
"get": {
"summary": "Test Method",
"operationId": "testMethod",
"responses": {
"200": {
"description": "A paged array of pets",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"responses": {
"default": {
"statusCode": "200"
}
},
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match",
"type": "mock"
}
}
}
},
"components": {
"schemas": {
"Empty": {
"title": "Empty Schema",
"type": "object"
}
}
}
},
"EndpointConfiguration": {
"Types": [
"PRIVATE"
]
},
"Name": "my-private-api"
}
},
"myprivateapiDeployment16B560CC678219fa18beb0906b49cad02ed224e8": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "myprivateapiD2D027EE"
},
"Description": "Automatically created by the RestApi construct"
}
},
"myprivateapiDeploymentStageprodC271AE68": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"RestApiId": {
"Ref": "myprivateapiD2D027EE"
},
"DeploymentId": {
"Ref": "myprivateapiDeployment16B560CC678219fa18beb0906b49cad02ed224e8"
},
"StageName": "prod"
}
},
"myprivateapiCloudWatchRole82F3E469": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
]
]
}
]
}
},
"myprivateapiAccount8564096B": {
"Type": "AWS::ApiGateway::Account",
"Properties": {
"CloudWatchRoleArn": {
"Fn::GetAtt": [
"myprivateapiCloudWatchRole82F3E469",
"Arn"
]
}
},
"DependsOn": [
"myprivateapiD2D027EE"
]
}
},
"Outputs": {
"myprivateapiEndpoint40EE1421": {
"Value": {
"Fn::Join": [
"",
[
"https://",
{
"Ref": "myprivateapiD2D027EE"
},
".execute-api.",
{
"Ref": "AWS::Region"
},
".",
{
"Ref": "AWS::URLSuffix"
},
"/",
{
"Ref": "myprivateapiDeploymentStageprodC271AE68"
},
"/"
]
]
}
},
"PetsURL": {
"Value": {
"Fn::Join": [
"",
[
"https://",
{
"Ref": "myprivateapiD2D027EE"
},
".execute-api.",
{
"Ref": "AWS::Region"
},
".",
{
"Ref": "AWS::URLSuffix"
},
"/",
{
"Ref": "myprivateapiDeploymentStageprodC271AE68"
},
"/pets"
]
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as cdk from '@aws-cdk/core';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of an integration test, add a unit test here instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a unit test. Waiting for the build to confirm it works... my machine (and GitPot) unit test keep failing in jest timeouts so using this slower approach.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop this integration test.

import * as apigateway from '../lib';

/*
* Stack verification steps:
* * `curl -i <CFN output PetsURL>` should return HTTP code 200
*/

const app = new cdk.App();
const stack = new cdk.Stack(app, 'integtest-restapi-fromdefinition-inline-private');

const api = new apigateway.SpecRestApi(stack, 'my-private-api', {
apiDefinition: apigateway.ApiDefinition.fromInline({
openapi: '3.0.2',
info: {
version: '1.0.0',
title: 'Test API for CDK',
},
paths: {
'/pets': {
get: {
'summary': 'Test Method',
'operationId': 'testMethod',
'responses': {
200: {
description: 'A paged array of pets',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Empty',
},
},
},
},
},
'x-amazon-apigateway-integration': {
responses: {
default: {
statusCode: '200',
},
},
requestTemplates: {
'application/json': '{"statusCode": 200}',
},
passthroughBehavior: 'when_no_match',
type: 'mock',
},
},
},
},
components: {
schemas: {
Empty: {
title: 'Empty Schema',
type: 'object',
},
},
},
}),
endpointConfiguration: {
types: [apigateway.EndpointType.PRIVATE],
},
});

new cdk.CfnOutput(stack, 'PetsURL', {
value: api.urlForPath('/pets'),
});

app.synth();