UNPKG

@amaabca/aws-lex-custom-resources

Version:
91 lines (90 loc) 4.77 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomResourceBaseStack = void 0; const cdk = __importStar(require("@aws-cdk/core")); const custom_resources_1 = require("@aws-cdk/custom-resources"); const aws_lambda_1 = require("@aws-cdk/aws-lambda"); const aws_lambda_nodejs_1 = require("@aws-cdk/aws-lambda-nodejs"); const aws_iam_1 = require("@aws-cdk/aws-iam"); const custom_resource_data_types_1 = require("./custom-resource-data-types"); class CustomResourceBaseStack extends cdk.NestedStack { constructor(scope, id, env, props) { var _a, _b, _c; super(scope, id); this.env = env; this.props = props; this.id = id; if (this.props.type && this.props.type === custom_resource_data_types_1.BotType.V2 && !((_a = this.props.role) === null || _a === void 0 ? void 0 : _a.customRole)) { throw new Error("Must provide a custom IAM role for Lex V2 resources in props.role.customRole field!"); } else if ((!this.props.type || this.props.type === custom_resource_data_types_1.BotType.V1) && !((_b = this.props.role) === null || _b === void 0 ? void 0 : _b.parentResource) && !((_c = this.props.role) === null || _c === void 0 ? void 0 : _c.childResource)) { throw new Error("Must provide a parentResource and a childResource for Lex V1 resources in props.role fields!"); } this.createHandler(); } createHandler() { var _a; if (this.props.type === custom_resource_data_types_1.BotType.V2) { const handlerFunction = new aws_lambda_nodejs_1.NodejsFunction(this, `${this.id}-handlerFunc`, { runtime: this.props.handler.runtime || aws_lambda_1.Runtime.NODEJS_14_X, entry: `${this.props.handler.folder}/${this.props.handler.entry}`, handler: this.props.handler.handlerName, timeout: cdk.Duration.seconds(this.props.handler.timeout), environment: this.props.handler.environment, role: (_a = this.props.role) === null || _a === void 0 ? void 0 : _a.customRole.withoutPolicyUpdates() }); this.createProvider(handlerFunction); } else { const handlerFunction = new aws_lambda_1.Function(this, `${this.id}-handlerFunc`, { runtime: this.props.handler.runtime || aws_lambda_1.Runtime.NODEJS_14_X, code: aws_lambda_1.Code.fromAsset(`${this.props.handler.folder}/`), handler: this.props.handler.handlerName, timeout: cdk.Duration.seconds(this.props.handler.timeout), environment: this.props.handler.environment, role: this.createHandlerRole().withoutPolicyUpdates() }); this.createProvider(handlerFunction); } } createProvider(func) { const lambdaProvider = new custom_resources_1.Provider(this, `${this.id}-handlerProvider`, { onEventHandler: func }); new cdk.CfnOutput(this, `${this.id}-intentProvider`, { value: lambdaProvider.serviceToken, exportName: this.props.exportName }); } createHandlerRole() { const handlerRole = new aws_iam_1.Role(this, `${this.id}-handlerFuncRole`, { assumedBy: new aws_iam_1.ServicePrincipal('lambda.amazonaws.com').grantPrincipal }); handlerRole.addToPolicy(new aws_iam_1.PolicyStatement({ resources: [`arn:aws:${this.props.role.parentResource}:${this.env.region}:${this.env.account}:${this.props.role.childResource}:*`], actions: this.props.role.actions })); handlerRole.addManagedPolicy(aws_iam_1.ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSLambdaBasicExecutionRole")); return handlerRole; } } exports.CustomResourceBaseStack = CustomResourceBaseStack;