serverless-vpc-discovery
Version:
Serverless Plugin to modify VPC values
135 lines (134 loc) • 6.05 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LambdaFunction = void 0;
const ec2_wrapper_1 = require("../aws/ec2-wrapper");
const utils_1 = require("../utils");
const validation_1 = require("../validation");
const logging_1 = __importDefault(require("../logging"));
const ts_md5_1 = require("ts-md5");
class LambdaFunction {
constructor(credentials, basicVPCDiscovery) {
this.vpcIdsCache = {};
this.subnetsIdsCache = {};
this.SGIdsCache = {};
this.ec2Wrapper = new ec2_wrapper_1.EC2Wrapper(credentials);
this.basicVPCDiscovery = basicVPCDiscovery;
}
/**
* Validate and return VPC config for the given function
* @returns {Promise<VPC>}
*/
getFuncVPC(funcName, funcVPCDiscovery) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof funcVPCDiscovery === "boolean" && !funcVPCDiscovery) {
// skip vpc setup for `vpcDiscovery=false` option
logging_1.default.logInfo(`Skipping VPC config for the function '${funcName}'`);
return null;
}
// inherit the `custom.vpcDiscovery`
const vpcDiscovery = Object.assign({}, this.basicVPCDiscovery, funcVPCDiscovery);
// return null in case vpcDiscovery not setup
if ((0, utils_1.isObjectEmpty)(vpcDiscovery)) {
return null;
}
// validate func vpcDiscovery config
try {
(0, validation_1.validateVPCDiscoveryConfig)(vpcDiscovery);
}
catch (e) {
throw new Error(`Function '${funcName}' is not configured correctly: ${e} VPC not configured. ` +
"Please see the README for the proper setup.");
}
try {
logging_1.default.logInfo(`Getting VPC config for the function: '${funcName}'\n`);
return yield this.getVpcConfig(vpcDiscovery);
}
catch (e) {
logging_1.default.logError(`Function '${funcName}' VPC not configured based on the error: ${e}`);
}
return null;
});
}
/**
* Gets the desired vpc with the designated subnets and security groups
* that were set in serverless config file
* @returns {Promise<object>}
*/
getVpcConfig(vpcDiscovery) {
return __awaiter(this, void 0, void 0, function* () {
const vpc = {};
const vpcId = yield this.getVPCId(vpcDiscovery.vpcName);
logging_1.default.logInfo(`Found VPC with id '${vpcId}'`);
if (vpcDiscovery.subnets) {
vpc.subnetIds = [];
for (const subnet of vpcDiscovery.subnets) {
const subnetIds = yield this.getVPCSubnets(vpcId, subnet.tagKey, subnet.tagValues);
vpc.subnetIds = vpc.subnetIds.concat(subnetIds);
}
// remove duplicate elements from the array
vpc.subnetIds = [...new Set(vpc.subnetIds)];
}
if (vpcDiscovery.securityGroups) {
vpc.securityGroupIds = [];
for (const group of vpcDiscovery.securityGroups) {
const groupIds = yield this.getVPCSecurityGroups(vpcId, group.names, group.tagKey, group.tagValues);
vpc.securityGroupIds = vpc.securityGroupIds.concat(groupIds);
}
// remove duplicate elements from the array
vpc.securityGroupIds = [...new Set(vpc.securityGroupIds)];
}
return vpc;
});
}
/**
* Get the VPC id from cache or read from AWS
* @returns {Promise<object>}
*/
getVPCId(vpcName) {
return __awaiter(this, void 0, void 0, function* () {
if (this.vpcIdsCache[vpcName] === undefined) {
this.vpcIdsCache[vpcName] = yield this.ec2Wrapper.getVpcId(vpcName);
}
return this.vpcIdsCache[vpcName];
});
}
/**
* Get the subnet ids from cache or read from AWS
* @returns {Promise<object>}
*/
getVPCSubnets(vpcId, tagKey, tagValues) {
return __awaiter(this, void 0, void 0, function* () {
const hash = ts_md5_1.Md5.hashStr(vpcId + tagKey + tagValues.join());
if (!this.subnetsIdsCache[hash]) {
this.subnetsIdsCache[hash] = yield this.ec2Wrapper.getSubnetIds(vpcId, tagKey, tagValues);
}
return this.subnetsIdsCache[hash];
});
}
/**
* Get the security group ids from cache or read from AWS
* @returns {Promise<object>}
*/
getVPCSecurityGroups(vpcId, names, tagKey, tagValues) {
return __awaiter(this, void 0, void 0, function* () {
const hash = ts_md5_1.Md5.hashStr(vpcId + (names || []).join() + tagKey + (tagValues || []).join());
if (!this.SGIdsCache[hash]) {
this.SGIdsCache[hash] = yield this.ec2Wrapper.getSecurityGroupIds(vpcId, names, tagKey, tagValues);
}
return this.SGIdsCache[hash];
});
}
}
exports.LambdaFunction = LambdaFunction;