UNPKG

@faceteer/cdk

Version:

CDK 2.0 constructs and helpers that make composing a Lambda powered service easier.

108 lines (107 loc) 4.82 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseFunction = void 0; const cdk = __importStar(require("aws-cdk-lib")); const lambdaNodeJs = __importStar(require("aws-cdk-lib/aws-lambda-nodejs")); const logs = __importStar(require("aws-cdk-lib/aws-logs")); const aws_lambda_1 = require("aws-cdk-lib/aws-lambda"); const mapArchNameToArchObject = { arm64: aws_lambda_1.Architecture.ARM_64, x86_64: aws_lambda_1.Architecture.X86_64, undefined: undefined, }; class BaseFunction extends lambdaNodeJs.NodejsFunction { definition; timeout; constructor(scope, id, { role, definition, bundlingOptions = {}, layers, defaults, network, environment, }) { const useVpc = definition.vpc ?? defaults?.vpc ?? false; if (useVpc && network === undefined) { throw new Error('Function is defined to use VPC, but no VPC has been provided for service.'); } const timeout = cdk.Duration.seconds(definition.timeout ?? defaults?.timeout ?? 30); const runtime = definition.runtime ?? defaults?.runtime; const architecture = definition.architecture ?? defaults?.architecture; super(scope, id, { awsSdkConnectionReuse: true, entry: definition.path, description: definition.description, memorySize: definition.memorySize ?? defaults?.memorySize ?? 192, reservedConcurrentExecutions: definition.reservedConcurrentExecutions, allowAllOutbound: definition.allowAllOutbound, allowPublicSubnet: definition.allowPublicSubnet, timeout, role, bundling: { sourceMap: true, sourceMapMode: lambdaNodeJs.SourceMapMode.INLINE, ...bundlingOptions, }, environment: { NODE_OPTIONS: '--enable-source-maps', HANDLER_NAME: definition.name, ACCOUNT_ID: cdk.Fn.ref('AWS::AccountId'), ...environment, }, layers, vpc: useVpc ? network?.vpc : undefined, vpcSubnets: useVpc ? network?.vpcSubnets : undefined, securityGroups: useVpc ? network?.securityGroups : undefined, runtime: runtime ? new aws_lambda_1.Runtime(runtime, aws_lambda_1.RuntimeFamily.NODEJS, { // This is enabled in AWS CDK built-in runtimes: // https://github.com/aws/aws-cdk/blob/bee883c27eef4840e067806740f4f0f242e7db50/packages/@aws-cdk/aws-lambda/lib/runtime.ts#L82-L92 supportsInlineCode: true, }) : undefined, architecture: architecture ? mapArchNameToArchObject[architecture] : undefined, }); this.definition = definition; this.timeout = timeout; const logRetention = definition.logRetention ?? defaults?.logRetention ?? 'destroy'; new logs.LogGroup(this, 'LogGroup', { logGroupName: `/aws/lambda/${this.functionName}`, removalPolicy: logRetention === 'destroy' ? cdk.RemovalPolicy.DESTROY : cdk.RemovalPolicy.RETAIN, retention: definition.logRetentionDuration ?? defaults?.logRetentionDuration ?? cdk.aws_logs.RetentionDays.TWO_MONTHS, }); } } exports.BaseFunction = BaseFunction;