Repository: serverless/serverless (JavaScript)
Date: 2019-09-18 21:04:08+00:00
Task type: Feature

Issue:
Add qualifier option for the invoke command
# This is a Feature Proposal

## Description
Currently, there's no way of calling a specific version/alias (qualifier) from the `serverless` CLI, so we have to fall back to the `aws` native CLI under the `--qualifier` option.

It would be good if there was an optional flag to pass a version/alias to the invoke command.


Issue discussion:

Solution patch:
diff --git a/docs/providers/aws/cli-reference/invoke.md b/docs/providers/aws/cli-reference/invoke.md
index f3ab278a0f6..09673e4fdb2 100644
--- a/docs/providers/aws/cli-reference/invoke.md
+++ b/docs/providers/aws/cli-reference/invoke.md
@@ -27,6 +27,7 @@ serverless invoke [local] --function functionName
 - `--function` or `-f` The name of the function in your service that you want to invoke. **Required**.
 - `--stage` or `-s` The stage in your service you want to invoke your function in.
 - `--region` or `-r` The region in your stage that you want to invoke your function in.
+- `--qualifier` or `-q` The version number or alias to invoke your function in. Default is `$LATEST`.
 - `--data` or `-d` String data to be passed as an event to your function. By default data is read from standard input.
 - `--raw` Pass data as a raw string even if it is JSON. If not set, JSON data are parsed and passed as an object.
 - `--path` or `-p` The path to a json file with input data to be passed to the invoked function. This path is relative to the root directory of the service.
diff --git a/lib/plugins/aws/invoke/index.js b/lib/plugins/aws/invoke/index.js
index 4bfef7ed1ae..3a85af595a4 100644
--- a/lib/plugins/aws/invoke/index.js
+++ b/lib/plugins/aws/invoke/index.js
@@ -77,6 +77,10 @@ class AwsInvoke {
       Payload: new Buffer(JSON.stringify(this.options.data || {})),
     };
 
+    if (this.options.qualifier) {
+      params.Qualifier = this.options.qualifier;
+    }
+
     return this.provider.request('Lambda', 'invoke', params);
   }
 
diff --git a/lib/plugins/invoke/invoke.js b/lib/plugins/invoke/invoke.js
index 7121feb061c..b424179087b 100644
--- a/lib/plugins/invoke/invoke.js
+++ b/lib/plugins/invoke/invoke.js
@@ -28,6 +28,10 @@ class Invoke {
             usage: 'Region of the service',
             shortcut: 'r',
           },
+          qualifier: {
+            usage: 'Version number or alias to invoke',
+            shortcut: 'q',
+          },
           path: {
             usage: 'Path to JSON or YAML file holding input data',
             shortcut: 'p',


Test patch:
diff --git a/lib/plugins/aws/invoke/index.test.js b/lib/plugins/aws/invoke/index.test.js
index d40e83c18c7..e6c476e3c4b 100644
--- a/lib/plugins/aws/invoke/index.test.js
+++ b/lib/plugins/aws/invoke/index.test.js
@@ -265,6 +265,26 @@ describe('AwsInvoke', () => {
         awsInvoke.provider.request.restore();
       });
     });
+
+    it('should be able to invoke with a qualifier', () => {
+      awsInvoke.options.qualifier = 'somelongqualifier';
+
+      return awsInvoke.invoke().then(() => {
+        expect(invokeStub.calledOnce).to.be.equal(true);
+
+        expect(
+          invokeStub.calledWithExactly('Lambda', 'invoke', {
+            FunctionName: 'customName',
+            InvocationType: 'RequestResponse',
+            LogType: 'None',
+            Payload: new Buffer(JSON.stringify({})),
+            Qualifier: 'somelongqualifier',
+          })
+        ).to.be.equal(true);
+
+        awsInvoke.provider.request.restore();
+      });
+    });
   });
 
   describe('#log()', () => {