diff --git a/docs/providers/aws/cli-reference/invoke-local.md b/docs/providers/aws/cli-reference/invoke-local.md index 20a618a9810..0df47645bf2 100644 --- a/docs/providers/aws/cli-reference/invoke-local.md +++ b/docs/providers/aws/cli-reference/invoke-local.md @@ -28,6 +28,7 @@ serverless invoke local --function functionName - `--raw` Pass data as a raw string even if it is JSON. If not set, JSON data are parsed and passed as an object. - `--contextPath` or `-x`, The path to a json file holding input context to be passed to the invoked function. This path is relative to the root directory of the service. - `--context` or `-c`, String data to be passed as a context to your function. Same like with `--data`, context included in `--contextPath` will overwrite the context you passed with `--context` flag. +* `--env` or `-e` String representing an environment variable to set when invoking your function, in the form `=`. Can be repeated for more than one environment variable. ## Environment @@ -94,6 +95,16 @@ serverless invoke local --function functionName --contextPath lib/context.json ``` This example will pass the json context in the `lib/context.json` file (relative to the root of the service) while invoking the specified/deployed function. +### Local function invocation, setting environment variables + +```bash +serverless invoke local -f functionName -e VAR1=value1 + +# Or more than one variable + +serverless invoke local -f functionName -e VAR1=value1 -e VAR2=value2 +``` + ### Limitations Currently, `invoke local` only supports the NodeJs, Python & Java runtimes. diff --git a/docs/providers/google/cli-reference/invoke-local.md b/docs/providers/google/cli-reference/invoke-local.md index 54ab1170190..ecdfafc2bbd 100644 --- a/docs/providers/google/cli-reference/invoke-local.md +++ b/docs/providers/google/cli-reference/invoke-local.md @@ -28,6 +28,7 @@ serverless invoke -f functionName * `--raw` Pass data as a raw string even if it is JSON. If not set, JSON data are parsed and passed as an object. * `--contextPath` or `-x`, The path to a json file holding input context to be passed to the invoked function. This path is relative to the root directory of the service. * `--context` or `-c`, String data to be passed as a context to your function. Same like with `--data`, context included in `--contextPath` will overwrite the context you passed with `--context` flag. +* `--env` or `-e` String representing an environment variable to set when invoking your function, in the form `=`. Can be repeated for more than one environment variable. > Keep in mind that if you pass both `--path` and `--data`, the data included in the `--path` file will overwrite the data you passed with the `--data` flag. @@ -54,3 +55,13 @@ serverless invoke local -f functionName -p path/to/file.json serverless invoke local -f functionName -p path/to/file.yaml ``` + +### Local function invocation, setting environment variables + +```bash +serverless invoke local -f functionName -e VAR1=value1 + +# Or more than one variable + +serverless invoke local -f functionName -e VAR1=value1 -e VAR2=value2 +``` diff --git a/docs/providers/openwhisk/cli-reference/invoke-local.md b/docs/providers/openwhisk/cli-reference/invoke-local.md index cad243fe160..4bb1ab52dc8 100644 --- a/docs/providers/openwhisk/cli-reference/invoke-local.md +++ b/docs/providers/openwhisk/cli-reference/invoke-local.md @@ -25,6 +25,7 @@ __*Please note that only the JavaScript and Python runtimes are supported with t - `--function` or `-f` The name of the function in your service that you want to invoke locally. **Required**. - `--path` or `-p` The path to a json file holding input data to be passed to the invoked function. This path is relative to the root directory of the service. The json file should have event and context properties to hold your mocked event and context data. - `--data` or `-d` String data to be passed as an event to your function. Keep in mind that if you pass both `--path` and `--data`, the data included in the `--path` file will overwrite the data you passed with the `--data` flag. +* `--env` or `-e` String representing an environment variable to set when invoking your function, in the form `=`. Can be repeated for more than one environment variable. ## Examples @@ -60,6 +61,16 @@ serverless invoke local --function functionName --path lib/data.json This example will pass the json data in the `lib/data.json` file (relative to the root of the service) while invoking the specified/deployed function. +### Local function invocation, setting environment variables + +```bash +serverless invoke local -f functionName -e VAR1=value1 + +# Or more than one variable + +serverless invoke local -f functionName -e VAR1=value1 -e VAR2=value2 +``` + ### Limitations Currently, `invoke local` only supports the NodeJs and Python runtimes. diff --git a/lib/plugins/invoke/invoke.js b/lib/plugins/invoke/invoke.js index 18f715735e0..4890bc8965e 100644 --- a/lib/plugins/invoke/invoke.js +++ b/lib/plugins/invoke/invoke.js @@ -5,8 +5,9 @@ const _ = require('lodash'); const userStats = require('../../utils/userStats'); class Invoke { - constructor(serverless) { + constructor(serverless, options) { this.serverless = serverless; + this.options = options || {}; this.commands = { invoke: { @@ -81,6 +82,10 @@ class Invoke { usage: 'Path to JSON or YAML file holding context data', shortcut: 'x', }, + env: { + usage: 'Override environment variables. e.g. --env VAR1=val1 --env VAR2=val2', + shortcut: 'e', + }, }, }, }, @@ -114,6 +119,14 @@ class Invoke { _.merge(process.env, defaultEnvVars); + // Turn zero or more --env options into an array + // ...then split --env NAME=value and put into process.env. + _.concat(this.options.env || []) + .forEach(itm => { + const splitItm = _.split(itm, '='); + process.env[splitItm[0]] = splitItm[1] || ''; + }); + return BbPromise.resolve(); } } diff --git a/lib/plugins/invoke/invoke.test.js b/lib/plugins/invoke/invoke.test.js index da707b70a97..c4830bee6f4 100644 --- a/lib/plugins/invoke/invoke.test.js +++ b/lib/plugins/invoke/invoke.test.js @@ -41,6 +41,20 @@ describe('Invoke', () => { return expect(invoke.hooks['invoke:local:loadEnvVars']()).to.be.fulfilled .then(() => expect(process.env.IS_LOCAL).to.equal('true')); }); + + it('should accept a single env option', () => { + invoke.options = { env: 'NAME=value' }; + expect(invoke.hooks['invoke:local:loadEnvVars']()).to.be.fulfilled + .then(() => expect(process.env.NAME).to.equal('value')); + }); + + it('should accept multiple env options', () => { + invoke.options = { env: ['NAME1=val1', 'NAME2=val2'] }; + + expect(invoke.hooks['invoke:local:loadEnvVars']()).to.be.fulfilled + .then(() => expect(process.env.NAME1).to.equal('val1')) + .then(() => expect(process.env.NAME2).to.equal('val2')); + }); }); }); });