UNPKG

serverless-vpc-discovery

Version:
118 lines (117 loc) 4.85 kB
"use strict"; 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 crypto_1 = require("crypto"); class LambdaFunction { ec2Wrapper; basicVPCDiscovery; vpcIdsCache = {}; subnetsIdsCache = {}; SGIdsCache = {}; constructor(credentials, basicVPCDiscovery) { this.ec2Wrapper = new ec2_wrapper_1.EC2Wrapper(credentials); this.basicVPCDiscovery = basicVPCDiscovery; } /** * Validate and return VPC config for the given function * @returns {Promise<VPC>} */ async getFuncVPC(funcName, funcVPCDiscovery) { 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 await 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>} */ async getVpcConfig(vpcDiscovery) { const vpc = {}; const vpcId = await 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 = await 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 = await 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>} */ async getVPCId(vpcName) { if (this.vpcIdsCache[vpcName] === undefined) { this.vpcIdsCache[vpcName] = await this.ec2Wrapper.getVpcId(vpcName); } return this.vpcIdsCache[vpcName]; } /** * Get the subnet ids from cache or read from AWS * @returns {Promise<object>} */ async getVPCSubnets(vpcId, tagKey, tagValues) { const hash = (0, crypto_1.createHash)("md5").update(vpcId + tagKey + tagValues.join()).digest("hex"); if (!this.subnetsIdsCache[hash]) { this.subnetsIdsCache[hash] = await this.ec2Wrapper.getSubnetIds(vpcId, tagKey, tagValues); } return this.subnetsIdsCache[hash]; } /** * Get the security group ids from cache or read from AWS * @returns {Promise<object>} */ async getVPCSecurityGroups(vpcId, names, tagKey, tagValues) { const hash = (0, crypto_1.createHash)("md5").update(vpcId + (names || []).join() + tagKey + (tagValues || []).join()).digest("hex"); if (!this.SGIdsCache[hash]) { this.SGIdsCache[hash] = await this.ec2Wrapper.getSecurityGroupIds(vpcId, names, tagKey, tagValues); } return this.SGIdsCache[hash]; } } exports.LambdaFunction = LambdaFunction;