From f8c00839ff48ebb50c98d9728b87f50cd58deb6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Wed, 14 Feb 2018 19:58:44 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20Add=20"--env-from-file"=20option?= =?UTF-8?q?=20to=20create=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 2 ++ lib/amazon.js | 5 +++-- lib/tasks.js | 16 +++++++++++++++- package-lock.json | 5 +++++ package.json | 1 + 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index 3229582..c0f4a28 100755 --- a/app.js +++ b/app.js @@ -23,6 +23,7 @@ usage: options: Name of the Lambda function. + --env-from-file= Read and set environmental variables from the specified file. --deploy-to= Deploy the API to the specified stage, and make it callable from the Internet. --help Show this help, then exit. --rest-api-id= ID of the AWS API Gateway rest api to point to the Lambda function. @@ -38,6 +39,7 @@ async function main () { const createList = new Listr([ tasks.packageApp, + tasks.readEnvironmentFile, tasks.createLambdaRole, tasks.createLambdaFunction, tasks.loadSwaggerDefinition, diff --git a/lib/amazon.js b/lib/amazon.js index e34cb86..b9243a3 100644 --- a/lib/amazon.js +++ b/lib/amazon.js @@ -30,7 +30,7 @@ function addPermission ({ lambdaArn, restApiId }) { return lambda.addPermission(params).promise() } -exports.createFunction = function ({ zipFile, functionName, role }) { +exports.createFunction = function ({ zipFile, functionName, role, environment }) { const params = { Code: { ZipFile: zipFile }, FunctionName: functionName, @@ -39,7 +39,8 @@ exports.createFunction = function ({ zipFile, functionName, role }) { Publish: true, Role: role, Runtime: defaultParams.Runtime, - Timeout: defaultParams.Timeout + Timeout: defaultParams.Timeout, + Environment: (environment ? { Variables: environment } : undefined) } return lambda.createFunction(params).promise().then(res => res.FunctionArn) diff --git a/lib/tasks.js b/lib/tasks.js index 2a2b0e0..32c6043 100644 --- a/lib/tasks.js +++ b/lib/tasks.js @@ -1,3 +1,7 @@ +const fs = require('fs') +const util = require('util') + +const dotenv = require('dotenv') const parseArn = require('aws-arn-parser') const prettyBytes = require('pretty-bytes') @@ -6,6 +10,8 @@ const builder = require('./builder') const swagger = require('./swagger') const UserError = require('./user-error') +const readFile = util.promisify(fs.readFile) + exports.packageApp = { title: 'Packaging app for Lambda', task: async (ctx, task) => { @@ -14,6 +20,14 @@ exports.packageApp = { } } +exports.readEnvironmentFile = { + title: 'Read environment file', + enabled: (ctx) => Boolean(ctx.args['--env-from-file']), + task: async (ctx, task) => { + ctx.environment = dotenv.parse(await readFile(ctx.args['--env-from-file'], 'utf-8')) + } +} + exports.createLambdaRole = { title: 'Creating Lambda role', task: async (ctx, task) => { @@ -30,7 +44,7 @@ exports.createLambdaRole = { exports.createLambdaFunction = { title: 'Creating Lambda function', task: async (ctx, task) => { - ctx.lambdaArn = await amazon.createFunction({ zipFile: ctx.zipFile, functionName: ctx.args[''], role: ctx.roleArn }) + ctx.lambdaArn = await amazon.createFunction({ zipFile: ctx.zipFile, functionName: ctx.args[''], role: ctx.roleArn, environment: ctx.environment }) task.title = `Created new Lambda function with ARN: ${ctx.lambdaArn}` } } diff --git a/package-lock.json b/package-lock.json index fcb8304..705a092 100644 --- a/package-lock.json +++ b/package-lock.json @@ -137,6 +137,11 @@ "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-1.29.0.tgz", "integrity": "sha512-lbTXWZ6M20cWH8N9S6afb0SBm6tMk+uUg6z3MqHPKE9atmsY3kJkTm8vKe93izJ2B2+q5MV990sM2CHgtAZaOw==" }, + "dotenv": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-5.0.0.tgz", + "integrity": "sha512-p4A7snaxI9Hnj3GDWhTpckHYcd9WwZDmGPcvJJV3CoRFq0Dvsp96eYgXBl9WbmbJfuxqiZ2WenNaeWSs675ghQ==" + }, "elegant-spinner": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/elegant-spinner/-/elegant-spinner-1.0.1.tgz", diff --git a/package.json b/package.json index 8a1b572..e03fafe 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "aws-has-region": "^1.0.0", "aws-sdk": "^2.97.0", "cp-file": "^4.2.0", + "dotenv": "^5.0.0", "execa": "^0.8.0", "is-ci": "^1.1.0", "listr": "^0.13.0",