UNPKG

cloud-red

Version:

Serverless Node-RED for your cloud integration needs

114 lines (104 loc) 3.73 kB
const claudia = require('claudia'); const path = require('path'); const AWS = require('aws-sdk'); const { util } = require('../util'); const { ConsoleLogger } = require('./common'); const CLOUD_RED_DEPLOYMENTS_BUCKET = 'cloud-red-deployments-bucket'; /** * @property {string=} userDir - directory where the user is running the project on * @property {string=} lambdaName - (optional) Lambda name (default. based on the property name of package.json) * @property {string=} lambdaHandler - (optional) Lambda handler. Default: `index.handler` * @property {string=} version - (optional) A version alias to automatically assign to the new function. * @property {string} profile - AWS profile to be used * @property {string} region - AWS region to be used. Default: `ap-southeast-2` * @property {string=} timeout - (optional) Lambda Timeout in secs. Default: `30` * @property {string=} memory - (optional) The amount of memory, in MB, your Lambda function is given. Default: `256` * @property {string=} runtime - (optional) Node.js runtime to use. For supported values, see http://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html. Default: `nodejs10.x` * @property {string=} env - (optional) comma-separated list of VAR=VALUE environment variables to set. For example: S3BUCKET=testbucket,SNSQUEUE=testqueue */ class AWSProvider { constructor( userDir, lambdaName, lambdaHandler, version, profile, region, timeout, memory, runtime, env ) { this.userDir = userDir; this.lambdaName = lambdaName; this.lambdaHandler = lambdaHandler || 'handler'; this.version = version; this.profile = profile; this.region = region; this.timeout = timeout || 30; this.memory = memory || 256; this.runtime = runtime || 'nodejs10.x'; this.env = env; } // create a lambda deployment async createDeployment() { // bundle the opts for claudia let claudiaOpts = { region: this.region, name: this.lambdaName || this.defaultLambdaName(), source: this.userDir, handler: this.lambdaHandler, timeout: this.timeout, memory: this.memory, config: 'cloud-red-config.json' }; if (this.version) { claudiaOpts['version'] = this.version; } claudiaOpts['use-local-dependencies'] = true; claudiaOpts['use-s3-bucket'] = CLOUD_RED_DEPLOYMENTS_BUCKET; if (this.env) { claudiaOpts['set-env'] = this.env; } //console.log(`Packaging source: ${claudiaOpts.source} ...`) // inject the profile to be used to aws AWS.config.credentials = new AWS.SharedIniFileCredentials({ profile: this.profile }); try { let output = await claudia.create(claudiaOpts, new ConsoleLogger()); console.log(JSON.stringify(output, null, 2)); } catch (err) { console.log(err); process.exit(1); } } async fetchLambdaLogs(lambdaName) { //TODO: read from cloudwatch and send to debug logs ... throw new Error('Not implemented yet'); } defaultLambdaName() { if (this.lambdaName) { return this.lambdaName; } // generate a default lambda name based on the `name` property of the project's package.json const pkg = util.readJSONSync(path.join(this.userDir, 'package.json')); return this.lambdaNameSanitize(pkg.name); } /** * clean up the lambda name to make it aws compatible * @param {string} str : Original name for the lambda. */ lambdaNameSanitize(original) { 'use strict'; return ( original && original .trim() .replace(/^@/, '') .replace(/[^a-zA-Z0-9-_]/g, '_') .substr(0, 140) ); } } module.exports.AWSProvider = AWSProvider;