Skip to content

Commit

Permalink
🎉 Add "--env-from-file" option to create command
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Feb 14, 2018
1 parent 7d23db5 commit f8c0083
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ usage:
options:
<name> Name of the Lambda function.
--env-from-file=<path> Read and set environmental variables from the specified file.
--deploy-to=<stage> Deploy the API to the specified stage, and make it callable from the Internet.
--help Show this help, then exit.
--rest-api-id=<rest-api-id> ID of the AWS API Gateway rest api to point to the Lambda function.
Expand All @@ -38,6 +39,7 @@ async function main () {

const createList = new Listr([
tasks.packageApp,
tasks.readEnvironmentFile,
tasks.createLambdaRole,
tasks.createLambdaFunction,
tasks.loadSwaggerDefinition,
Expand Down
5 changes: 3 additions & 2 deletions lib/amazon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion lib/tasks.js
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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['<name>'], role: ctx.roleArn })
ctx.lambdaArn = await amazon.createFunction({ zipFile: ctx.zipFile, functionName: ctx.args['<name>'], role: ctx.roleArn, environment: ctx.environment })
task.title = `Created new Lambda function with ARN: ${ctx.lambdaArn}`
}
}
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f8c0083

Please sign in to comment.