Skip to content

Commit 7101755

Browse files
authored
Merge pull request #524 from raphaeldeem-acorns/rd/add-endpoint-configuration
add vpcEndpointIds to EndpointConfiguration
2 parents dba800b + bf9e804 commit 7101755

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

lib/deploy/events/apiGateway/restApi.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
this.apiGatewayRestApiLogicalId = this.provider.naming.getRestApiLogicalId();
1414

1515
let endpointType = 'EDGE';
16+
let vpcEndpointIds;
1617

1718
if (this.serverless.service.provider.endpointType) {
1819
const validEndpointTypes = ['REGIONAL', 'EDGE', 'PRIVATE'];
@@ -22,6 +23,16 @@ module.exports = {
2223
throw new this.serverless.classes.Error('endpointType must be a string');
2324
}
2425

26+
if (this.serverless.service.provider.vpcEndpointIds) {
27+
vpcEndpointIds = this.serverless.service.provider.vpcEndpointIds;
28+
29+
if (endpointType !== 'PRIVATE') {
30+
throw new Error(
31+
'VPC endpoint IDs are only available for private APIs',
32+
'API_GATEWAY_INVALID_VPC_ENDPOINT_IDS_CONFIG',
33+
);
34+
}
35+
}
2536

2637
if (!_.includes(validEndpointTypes, endpointType.toUpperCase())) {
2738
const message = 'endpointType must be one of "REGIONAL" or "EDGE" or "PRIVATE". '
@@ -31,14 +42,20 @@ module.exports = {
3142
endpointType = endpointType.toUpperCase();
3243
}
3344

45+
const EndpointConfiguration = {
46+
Types: [endpointType],
47+
};
48+
49+
if (vpcEndpointIds) {
50+
EndpointConfiguration.VpcEndpointIds = vpcEndpointIds;
51+
}
52+
3453
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
3554
[this.apiGatewayRestApiLogicalId]: {
3655
Type: 'AWS::ApiGateway::RestApi',
3756
Properties: {
3857
Name: this.provider.naming.getApiGatewayName(),
39-
EndpointConfiguration: {
40-
Types: [endpointType],
41-
},
58+
EndpointConfiguration,
4259
},
4360
},
4461
});

lib/deploy/events/apiGateway/restApi.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ describe('#compileRestApi()', () => {
99
let serverless;
1010
let serverlessStepFunctions;
1111

12+
const serviceResourcesAwsResourcesPrivateObjectMock = {
13+
Resources: {
14+
ApiGatewayRestApi: {
15+
Type: 'AWS::ApiGateway::RestApi',
16+
Properties: {
17+
Name: 'dev-new-service',
18+
EndpointConfiguration: {
19+
Types: ['PRIVATE'],
20+
VpcEndpointIds: [
21+
'vpc-11abcdefgh',
22+
],
23+
},
24+
},
25+
},
26+
},
27+
};
28+
1229
const serviceResourcesAwsResourcesObjectMock = {
1330
Resources: {
1431
ApiGatewayRestApi: {
@@ -125,6 +142,23 @@ describe('#compileRestApi()', () => {
125142
});
126143
});
127144

145+
it('throw error if vpcEndpointIds and endpointType is not PRIVATE', () => {
146+
serverlessStepFunctions.serverless.service.provider.vpcEndpointIds = ['vpc-11abcdefgh'];
147+
serverlessStepFunctions.serverless.service.provider.endpointType = 'EDGE';
148+
expect(() => serverlessStepFunctions.compileRestApi()).to.throw(Error);
149+
});
150+
151+
it('should create a Private REST API Resource with VPC Endpoints', () => {
152+
serverlessStepFunctions.serverless.service.provider.vpcEndpointIds = ['vpc-11abcdefgh'];
153+
serverlessStepFunctions.serverless.service.provider.endpointType = 'PRIVATE';
154+
return serverlessStepFunctions.compileRestApi().then(() => {
155+
expect(serverlessStepFunctions.serverless.service
156+
.provider.compiledCloudFormationTemplate.Resources).to.deep.equal(
157+
serviceResourcesAwsResourcesPrivateObjectMock.Resources,
158+
);
159+
});
160+
});
161+
128162
it('throw error if endpointType property is not a string', () => {
129163
serverlessStepFunctions.serverless.service.provider.endpointType = ['EDGE'];
130164
expect(() => serverlessStepFunctions.compileRestApi()).to.throw(Error);

0 commit comments

Comments
 (0)