Repository: motdotla/node-lambda
Pull request #480 (2019-03-08 10:42:36)

Add option to specify Lambda Layers
* https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
* https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#createFunction-property


Patch:
diff --git a/bin/node-lambda b/bin/node-lambda
index 6fd3e05..0f88445 100755
--- a/bin/node-lambda
+++ b/bin/node-lambda
@@ -37,6 +37,7 @@ const AWS_FUNCTION_VERSION = process.env.AWS_FUNCTION_VERSION || ''
 const AWS_VPC_SUBNETS = process.env.AWS_VPC_SUBNETS || ''
 const AWS_VPC_SECURITY_GROUPS = process.env.AWS_VPC_SECURITY_GROUPS || ''
 const AWS_TRACING_CONFIG = process.env.AWS_TRACING_CONFIG || ''
+const AWS_LAYERS = process.env.AWS_LAYERS || ''
 const AWS_LOGS_RETENTION_IN_DAYS = process.env.AWS_LOGS_RETENTION_IN_DAYS || ''
 const EVENT_FILE = process.env.EVENT_FILE || 'event.json'
 const PACKAGE_DIRECTORY = process.env.PACKAGE_DIRECTORY
@@ -90,6 +91,7 @@ program
     AWS_DLQ_TARGET_ARN)
   .option('-c, --tracingConfig [AWS_TRACING_CONFIG]', 'Lambda tracing settings',
     AWS_TRACING_CONFIG)
+  .option('-l, --layers [AWS_LAYERS]', 'Lambda Layers settings', AWS_LAYERS)
   .option('-R, --retentionInDays [AWS_LOGS_RETENTION_IN_DAYS]', 'CloudWatchLogs retentionInDays settings',
     AWS_LOGS_RETENTION_IN_DAYS)
   .option('-A, --packageDirectory [PACKAGE_DIRECTORY]', 'Local Package Directory', PACKAGE_DIRECTORY)
diff --git a/lib/main.js b/lib/main.js
index 0ad1d36..ba6f5c7 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -215,7 +215,8 @@ so you can easily test run multiple events.
       },
       TracingConfig: {
         Mode: null
-      }
+      },
+      Layers: []
     }
 
     if (this._isUseS3(program)) {
@@ -252,6 +253,9 @@ so you can easily test run multiple events.
     if (program.tracingConfig) {
       params.TracingConfig.Mode = program.tracingConfig
     }
+    if (program.layers) {
+      params.Layers = program.layers.split(',')
+    }
 
     return params
   }
@@ -538,7 +542,8 @@ so you can easily test run multiple events.
           'Environment': params.Environment,
           'KMSKeyArn': params.KMSKeyArn,
           'DeadLetterConfig': params.DeadLetterConfig,
-          'TracingConfig': params.TracingConfig
+          'TracingConfig': params.TracingConfig,
+          'Layers': params.Layers
         }, (err, data) => {
           if (err) return reject(err)
           resolve(data)


Test patch:
diff --git a/test/main.js b/test/main.js
index 181ae9b..b4fa2aa 100644
--- a/test/main.js
+++ b/test/main.js
@@ -23,6 +23,7 @@ const originalProgram = {
   runtime: 'nodejs8.10',
   deadLetterConfigTargetArn: '',
   tracingConfig: '',
+  Layers: '',
   retentionInDays: 30,
   region: 'us-east-1,us-west-2,eu-west-1',
   eventFile: 'event.json',
@@ -298,6 +299,18 @@ describe('lib/main', function () {
       assert.isNull(params.TracingConfig.Mode)
     })
 
+    it('appends Layers to params when params set', () => {
+      program.layers = 'Layer1,Layer2'
+      const params = lambda._params(program)
+      assert.deepEqual(params.Layers, ['Layer1', 'Layer2'])
+    })
+
+    it('does not append Layers when params are not set', () => {
+      program.layers = ''
+      const params = lambda._params(program)
+      assert.deepEqual(params.Layers, [])
+    })
+
     describe('S3 deploy', () => {
       it('Do not use S3 deploy', () => {
         const params = lambda._params(program, 'Buffer')