Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
Added 'subnetGroup' option (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoseong authored and jplock committed Jun 3, 2019
1 parent 70ec7f1 commit f300429
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ custom:
services:
- kms
- secretsmanager

# optional
# can pick one of subnet groups in (rds / redshift / elasticache / dax)
# By default, if not specified, all of the subnet groups will be created.
# ex) DAX is not available in ap-northeast-2 so I don't want to make DAX subnet group
subnetGroups:
- rds
```
## CloudFormation Outputs
Expand Down
29 changes: 29 additions & 0 deletions __tests__/subnet_groups.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ const {
buildElastiCacheSubnetGroup,
buildRedshiftSubnetGroup,
buildDAXSubnetGroup,
buildSubnetGroups,
} = require('../src/subnet_groups');

describe('subnet_groups', () => {
let subnetGroupList = {};

describe('#buildRDSSubnetGroup', () => {
it('skips building an RDS subnet group with no zones', () => {
const actual = buildRDSSubnetGroup();
Expand Down Expand Up @@ -35,6 +38,7 @@ describe('subnet_groups', () => {
},
},
};
subnetGroupList.rds = expected;
const actual = buildRDSSubnetGroup(2);
expect(actual).toEqual(expected);
expect.assertions(1);
Expand Down Expand Up @@ -99,6 +103,7 @@ describe('subnet_groups', () => {
},
},
};
subnetGroupList.elasticache = expected;
const actual = buildElastiCacheSubnetGroup(2);
expect(actual).toEqual(expected);
expect.assertions(1);
Expand Down Expand Up @@ -160,6 +165,7 @@ describe('subnet_groups', () => {
},
},
};
subnetGroupList.redshift = expected;
const actual = buildRedshiftSubnetGroup(2);
expect(actual).toEqual(expected);
expect.assertions(1);
Expand Down Expand Up @@ -221,6 +227,7 @@ describe('subnet_groups', () => {
},
},
};
subnetGroupList.dax = expected;
const actual = buildDAXSubnetGroup(2);
expect(actual).toEqual(expected);
expect.assertions(1);
Expand Down Expand Up @@ -255,4 +262,26 @@ describe('subnet_groups', () => {
expect.assertions(1);
});
});

describe('#buildSubnetGroups', () => {
it('no subnetGroups option', () => {
const expected = Object.keys(subnetGroupList).reduce(
(acc, key) => Object.assign(acc, subnetGroupList[key]),
{},
);
const actual = buildSubnetGroups(2, []);
expect(actual).toEqual(expected);
expect.assertions(1);
});
it('get specific subnetGroups option', () => {
const getSubnetGroupsOptions = ['rds', 'redshift'];
const expected = getSubnetGroupsOptions.reduce(
(acc, key) => Object.assign(acc, subnetGroupList[key]),
{},
);
const actual = buildSubnetGroups(2, getSubnetGroupsOptions);
expect(actual).toEqual(expected);
expect.assertions(1);
});
});
});
17 changes: 15 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
buildLambdaSecurityGroup,
} = require('./vpc');
const { buildAppNetworkAcl, buildPublicNetworkAcl, buildDBNetworkAcl } = require('./nacl');
const { buildSubnetGroups } = require('./subnet_groups');
const { buildSubnetGroups, validSubnetGroups } = require('./subnet_groups');
const { buildEndpointServices, buildLambdaVPCEndpointSecurityGroup } = require('./vpce');
const { buildLogBucket, buildLogBucketPolicy, buildVpcFlowLogs } = require('./flow_logs');
const { buildBastion } = require('./bastion');
Expand Down Expand Up @@ -38,6 +38,7 @@ class ServerlessVpcPlugin {
let createNatInstance = false;
let createBastionHost = false;
let bastionHostKeyName = null;
let subnetGroups = [];

const { vpcConfig } = this.serverless.service.custom;

Expand Down Expand Up @@ -70,6 +71,9 @@ class ServerlessVpcPlugin {
if (Array.isArray(vpcConfig.services)) {
services = vpcConfig.services.map(s => s.trim().toLowerCase());
}
if (Array.isArray(vpcConfig.subnetGroups) && vpcConfig.subnetGroups.length > 0) {
({ subnetGroups } = vpcConfig);
}

if ('createDbSubnet' in vpcConfig && typeof vpcConfig.createDbSubnet === 'boolean') {
({ createDbSubnet } = vpcConfig);
Expand Down Expand Up @@ -222,7 +226,16 @@ class ServerlessVpcPlugin {
if (numZones < 2) {
this.serverless.cli.log('WARNING: less than 2 AZs; skipping subnet group provisioning');
} else {
Object.assign(resources, buildSubnetGroups(numZones));
for (let i = 0; i < subnetGroups.length; i += 1) {
const subnetGrp = subnetGroups[i];
if (validSubnetGroups.indexOf(subnetGrp) === -1) {
throw new this.serverless.classes.Error(
`WARNING: '${subnetGrp}' is invalid subnetGroups option.
you should input among these options: ['rds', 'redshift', 'elasticache', 'dax']`,
);
}
}
Object.assign(resources, buildSubnetGroups(numZones, subnetGroups));
}
}

Expand Down
20 changes: 19 additions & 1 deletion src/subnet_groups.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { DB_SUBNET } = require('./constants');

const validSubnetGroups = ['rds', 'redshift', 'elasticache', 'dax'];

/**
* Build an RDSubnetGroup for a given number of zones
*
Expand Down Expand Up @@ -133,12 +135,27 @@ function buildDAXSubnetGroup(numZones = 0, { name = 'DAXSubnetGroup' } = {}) {
* Build the database subnet groups
*
* @param {Number} numZones Number of availability zones
* @param {Array} subnetGroups options of subnet groups
* @return {Object}
*/
function buildSubnetGroups(numZones = 0) {
function buildSubnetGroups(numZones = 0, subnetGroups = []) {
if (numZones < 2) {
return {};
}
const subnetGroupList = {
rds: buildRDSSubnetGroup,
redshift: buildRedshiftSubnetGroup,
elasticache: buildElastiCacheSubnetGroup,
dax: buildDAXSubnetGroup,
};
function assembleSubnetGrp(acc, service) {
const builtSubnetGroup = subnetGroupList[service.toLowerCase()](numZones);
return Object.assign(acc, builtSubnetGroup);
}

if (subnetGroups.length > 0) {
return subnetGroups.reduce(assembleSubnetGrp, {});
}
return Object.assign(
{},
buildRDSSubnetGroup(numZones),
Expand All @@ -154,4 +171,5 @@ module.exports = {
buildRDSSubnetGroup,
buildRedshiftSubnetGroup,
buildSubnetGroups,
validSubnetGroups,
};

0 comments on commit f300429

Please sign in to comment.