From 94d93e3d62d8d90e83163d7792385761cf022b09 Mon Sep 17 00:00:00 2001 From: bryan pedlar Date: Sat, 19 Nov 2016 18:35:26 -0500 Subject: [PATCH 01/10] added environment variable support --- .../aws/deploy/compile/functions/index.js | 10 +++ .../deploy/compile/functions/tests/index.js | 78 +++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/lib/plugins/aws/deploy/compile/functions/index.js b/lib/plugins/aws/deploy/compile/functions/index.js index 7a2c28dd7..9bbaeb98b 100644 --- a/lib/plugins/aws/deploy/compile/functions/index.js +++ b/lib/plugins/aws/deploy/compile/functions/index.js @@ -81,6 +81,13 @@ class AwsCompileFunctions { newFunction.Properties.Description = functionObject.description; } + if (functionObject.environment) { + Object.assign(newFunction.Properties.Environment.Variables, + this.serverless.service.provider.environment); + Object.assign(newFunction.Properties.Environment.Variables, + functionObject.environment); + } + if ('role' in functionObject) { newFunction.Properties.Role = this.compileRole(functionObject.role); } else if ('role' in this.serverless.service.provider) { @@ -143,6 +150,9 @@ class AwsCompileFunctions { }, S3Key: 'S3Key', }, + Environment: { + Variables:{} + }, FunctionName: 'FunctionName', Handler: 'Handler', MemorySize: 'MemorySize', diff --git a/lib/plugins/aws/deploy/compile/functions/tests/index.js b/lib/plugins/aws/deploy/compile/functions/tests/index.js index 7bb651926..467274730 100644 --- a/lib/plugins/aws/deploy/compile/functions/tests/index.js +++ b/lib/plugins/aws/deploy/compile/functions/tests/index.js @@ -297,6 +297,9 @@ describe('AwsCompileFunctions', () => { awsCompileFunctions.serverless.service.package.artifact}`, S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, + Environment: { + Variables:{} + }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', MemorySize: 1024, @@ -334,6 +337,9 @@ describe('AwsCompileFunctions', () => { S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, FunctionName: 'new-service-dev-func', + Environment: { + Variables:{} + }, Handler: 'func.function.handler', MemorySize: 1024, Role: { 'Fn::GetAtt': ['IamRoleLambdaExecution', 'Arn'] }, @@ -360,6 +366,63 @@ describe('AwsCompileFunctions', () => { }; }); + it('should create a function resource with Environment config', () => { + awsCompileFunctions.serverless.service.functions = { + func: { + handler: 'func.function.handler', + name: 'new-service-dev-func', + environment: { + test1: 'test1', + test2: 'test2', + }, + }, + }; + + awsCompileFunctions.serverless.service.provider.environment = { + "providerTest1":"providerTest1" + } + + const compliedFunction = { + Type: 'AWS::Lambda::Function', + Properties: { + Code: { + S3Key: `${awsCompileFunctions.serverless.service.package.artifactDirectoryName}/${ + awsCompileFunctions.serverless.service.package.artifact}`, + S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, + }, + FunctionName: 'new-service-dev-func', + Environment: { + Variables:{} + }, + Handler: 'func.function.handler', + MemorySize: 1024, + Role: { 'Fn::GetAtt': ['IamRoleLambdaExecution', 'Arn'] }, + Runtime: 'nodejs4.3', + Timeout: 6, + Environment: { + Variables: { + test1: 'test1', + test2: 'test2', + providerTest1: 'providerTest1' + } + }, + }, + }; + + awsCompileFunctions.compileFunctions(); + + expect( + awsCompileFunctions.serverless.service.provider.compiledCloudFormationTemplate + .Resources.FuncLambdaFunction + ).to.deep.equal(compliedFunction); + + awsCompileFunctions.serverless.service.functions = { + func: { + handler: 'func.function.handler', + }, + }; + }); + it('should consider function based config when creating a function resource', () => { awsCompileFunctions.serverless.service.functions = { func: { @@ -378,6 +441,9 @@ describe('AwsCompileFunctions', () => { S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, FunctionName: 'customized-func-function', + Environment: { + Variables:{} + }, Handler: 'func.function.handler', MemorySize: 128, Role: { 'Fn::GetAtt': ['IamRoleLambdaExecution', 'Arn'] }, @@ -414,6 +480,9 @@ describe('AwsCompileFunctions', () => { awsCompileFunctions.serverless.service.package.artifact}`, S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, + Environment: { + Variables:{} + }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', MemorySize: 1024, @@ -445,6 +514,9 @@ describe('AwsCompileFunctions', () => { awsCompileFunctions.serverless.service.package.artifact}`, S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, + Environment: { + Variables:{} + }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', MemorySize: 1024, @@ -480,6 +552,9 @@ describe('AwsCompileFunctions', () => { awsCompileFunctions.serverless.service.package.artifact}`, S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, + Environment: { + Variables:{} + }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', MemorySize: 128, @@ -517,6 +592,9 @@ describe('AwsCompileFunctions', () => { awsCompileFunctions.serverless.service.package.artifact}`, S3Bucket: bucketName, }, + Environment: { + Variables:{} + }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', MemorySize: 128, From 07f8a5b7cc536d206aa0df13064c5347cf78d40a Mon Sep 17 00:00:00 2001 From: bryan pedlar Date: Sat, 19 Nov 2016 18:52:49 -0500 Subject: [PATCH 02/10] added documentation around environment variable configuration --- docs/providers/aws/guide/functions.md | 35 +++++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/docs/providers/aws/guide/functions.md b/docs/providers/aws/guide/functions.md index 6ead0c38d..81166a205 100644 --- a/docs/providers/aws/guide/functions.md +++ b/docs/providers/aws/guide/functions.md @@ -220,19 +220,38 @@ Then, when you run `serverless deploy`, VPC configuration will be deployed along ## Environment Variables -We're working on great environment variable support. Until then, you'll be able to use the following tools for different languages to set environment variables and make them available to your code. +You can add Environment Variable configuration to a specific function in `serverless.yml` by adding an `environment` object property in the function configuration. This object should contain a a key/value collection of string:string: -## Javascript +```yml +# serverless.yml +service: service-name +provider: aws -You can use [dotenv](https://www.npmjs.com/package/dotenv) to load files with environment variables. Those variables can be set during your CI process or locally and then packaged and deployed together with your function code. +functions: + hello: + handler: handler.hello + environment: + TABLE_NAME: tableName +``` -## Python +Or if you want to apply Environment Variable configuration to all functions in your service, you can add the configuration to the higher level `provider` object, and overwrite these service level config at the function level. For example: -You can use [python-dotenv](https://github.com/theskumar/python-dotenv) to load files with environment variables. Those variables can be set during your CI process or locally and then packaged and deployed together with your function code. +```yml +# serverless.yml +service: service-name +provider: + name: aws + environment: + TABLE_NAME: tableName1 -## Java - -For Java the easiest way to set up environment like configuration is through [property files](https://docs.oracle.com/javase/tutorial/essential/environment/properties.html). While those will not be available as environment variables they are very commonly used configuration mechanisms throughout Java. +functions: + hello: # this function will overwrite the service level environment config above + handler: handler.hello + environment: + TABLE_NAME: tableName2 + users: # this function will inherit the service level environment config above + handler: handler.users +``` ## Log Group Resources From d4b16a12c2b04d5d0bddacd2a611ff2ff419b63e Mon Sep 17 00:00:00 2001 From: bryan pedlar Date: Sat, 19 Nov 2016 19:24:02 -0500 Subject: [PATCH 03/10] fixed linting errors --- .../aws/deploy/compile/functions/index.js | 2 +- .../deploy/compile/functions/tests/index.js | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/functions/index.js b/lib/plugins/aws/deploy/compile/functions/index.js index 9bbaeb98b..7524a157a 100644 --- a/lib/plugins/aws/deploy/compile/functions/index.js +++ b/lib/plugins/aws/deploy/compile/functions/index.js @@ -151,7 +151,7 @@ class AwsCompileFunctions { S3Key: 'S3Key', }, Environment: { - Variables:{} + Variables: {}, }, FunctionName: 'FunctionName', Handler: 'Handler', diff --git a/lib/plugins/aws/deploy/compile/functions/tests/index.js b/lib/plugins/aws/deploy/compile/functions/tests/index.js index 467274730..76ab7d3db 100644 --- a/lib/plugins/aws/deploy/compile/functions/tests/index.js +++ b/lib/plugins/aws/deploy/compile/functions/tests/index.js @@ -298,7 +298,7 @@ describe('AwsCompileFunctions', () => { S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, Environment: { - Variables:{} + Variables: {}, }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', @@ -338,7 +338,7 @@ describe('AwsCompileFunctions', () => { }, FunctionName: 'new-service-dev-func', Environment: { - Variables:{} + Variables: {}, }, Handler: 'func.function.handler', MemorySize: 1024, @@ -380,7 +380,7 @@ describe('AwsCompileFunctions', () => { awsCompileFunctions.serverless.service.provider.environment = { "providerTest1":"providerTest1" - } + }; const compliedFunction = { Type: 'AWS::Lambda::Function', @@ -392,7 +392,7 @@ describe('AwsCompileFunctions', () => { }, FunctionName: 'new-service-dev-func', Environment: { - Variables:{} + Variables: {}, }, Handler: 'func.function.handler', MemorySize: 1024, @@ -442,7 +442,7 @@ describe('AwsCompileFunctions', () => { }, FunctionName: 'customized-func-function', Environment: { - Variables:{} + Variables: {}, }, Handler: 'func.function.handler', MemorySize: 128, @@ -481,7 +481,7 @@ describe('AwsCompileFunctions', () => { S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, Environment: { - Variables:{} + Variables: {}, }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', @@ -515,7 +515,7 @@ describe('AwsCompileFunctions', () => { S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, Environment: { - Variables:{} + Variables: {}, }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', @@ -553,7 +553,7 @@ describe('AwsCompileFunctions', () => { S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, Environment: { - Variables:{} + Variables: {}, }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', @@ -593,7 +593,7 @@ describe('AwsCompileFunctions', () => { S3Bucket: bucketName, }, Environment: { - Variables:{} + Variables: {}, }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', From 3fc632f4c1e19700fd3dcb3de615dd9e7615c017 Mon Sep 17 00:00:00 2001 From: bryan pedlar Date: Sat, 19 Nov 2016 19:27:32 -0500 Subject: [PATCH 04/10] fixed more linting errors --- lib/plugins/aws/deploy/compile/functions/tests/index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/functions/tests/index.js b/lib/plugins/aws/deploy/compile/functions/tests/index.js index 76ab7d3db..f603d0bd2 100644 --- a/lib/plugins/aws/deploy/compile/functions/tests/index.js +++ b/lib/plugins/aws/deploy/compile/functions/tests/index.js @@ -379,7 +379,7 @@ describe('AwsCompileFunctions', () => { }; awsCompileFunctions.serverless.service.provider.environment = { - "providerTest1":"providerTest1" + providerTest1: 'providerTest1' }; const compliedFunction = { @@ -391,9 +391,6 @@ describe('AwsCompileFunctions', () => { S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, FunctionName: 'new-service-dev-func', - Environment: { - Variables: {}, - }, Handler: 'func.function.handler', MemorySize: 1024, Role: { 'Fn::GetAtt': ['IamRoleLambdaExecution', 'Arn'] }, From e79bb272223aa1818c96e6f880c77c982f0dffd8 Mon Sep 17 00:00:00 2001 From: bryan pedlar Date: Sat, 19 Nov 2016 19:30:21 -0500 Subject: [PATCH 05/10] fixed yet more linting errors --- lib/plugins/aws/deploy/compile/functions/tests/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/functions/tests/index.js b/lib/plugins/aws/deploy/compile/functions/tests/index.js index f603d0bd2..b1c9abf55 100644 --- a/lib/plugins/aws/deploy/compile/functions/tests/index.js +++ b/lib/plugins/aws/deploy/compile/functions/tests/index.js @@ -379,7 +379,7 @@ describe('AwsCompileFunctions', () => { }; awsCompileFunctions.serverless.service.provider.environment = { - providerTest1: 'providerTest1' + providerTest1: 'providerTest1', }; const compliedFunction = { @@ -400,8 +400,8 @@ describe('AwsCompileFunctions', () => { Variables: { test1: 'test1', test2: 'test2', - providerTest1: 'providerTest1' - } + providerTest1: 'providerTest1', + }, }, }, }; From 1f1c63dc7aa06aac19085189b49195215b1af533 Mon Sep 17 00:00:00 2001 From: bryan pedlar Date: Sat, 19 Nov 2016 21:38:46 -0500 Subject: [PATCH 06/10] global environment variables are added appropriately --- lib/plugins/aws/deploy/compile/functions/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/functions/index.js b/lib/plugins/aws/deploy/compile/functions/index.js index 7524a157a..88b374e23 100644 --- a/lib/plugins/aws/deploy/compile/functions/index.js +++ b/lib/plugins/aws/deploy/compile/functions/index.js @@ -81,11 +81,11 @@ class AwsCompileFunctions { newFunction.Properties.Description = functionObject.description; } - if (functionObject.environment) { - Object.assign(newFunction.Properties.Environment.Variables, - this.serverless.service.provider.environment); - Object.assign(newFunction.Properties.Environment.Variables, - functionObject.environment); + newFunction.Properties.Environment.Variables = Object.assign( + {}, + this.serverless.service.provider.environment, + functionObject.environment + ) } if ('role' in functionObject) { From ca41f63c48a2188e6dd5bfe583d71b527c2ce30e Mon Sep 17 00:00:00 2001 From: Bryan Pedlar Date: Sat, 19 Nov 2016 21:40:10 -0500 Subject: [PATCH 07/10] added validation of environment variable names --- .../aws/deploy/compile/functions/index.js | 6 ++++++ .../aws/deploy/compile/functions/tests/index.js | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/plugins/aws/deploy/compile/functions/index.js b/lib/plugins/aws/deploy/compile/functions/index.js index 88b374e23..143878400 100644 --- a/lib/plugins/aws/deploy/compile/functions/index.js +++ b/lib/plugins/aws/deploy/compile/functions/index.js @@ -86,6 +86,12 @@ class AwsCompileFunctions { this.serverless.service.provider.environment, functionObject.environment ) + + for(var key in newFunction.Properties.Environment.Variables){ + // I pulled this from the bash man pages + if(!key.match(/^[A-Za-z_][a-zA-Z0-9_]*$/)){ + throw new Error("Invalid characters in environment variable"); + } } if ('role' in functionObject) { diff --git a/lib/plugins/aws/deploy/compile/functions/tests/index.js b/lib/plugins/aws/deploy/compile/functions/tests/index.js index b1c9abf55..d6d5a6ccf 100644 --- a/lib/plugins/aws/deploy/compile/functions/tests/index.js +++ b/lib/plugins/aws/deploy/compile/functions/tests/index.js @@ -420,6 +420,22 @@ describe('AwsCompileFunctions', () => { }; }); + + it('should throw if invalid environment variable name', () => { + awsCompileFunctions.serverless.service.functions = { + func: { + handler: 'func.function.handler', + name: 'new-service-dev-func', + environment: { + '1test1': 'test1', + test2: 'test2', + }, + }, + }; + + expect(() => awsCompileFunctions.compileFunctions()).to.throw(Error); + }); + it('should consider function based config when creating a function resource', () => { awsCompileFunctions.serverless.service.functions = { func: { From e381e9e4ef01093b3009249b7180c49b89d9d4b7 Mon Sep 17 00:00:00 2001 From: bryan pedlar Date: Sat, 19 Nov 2016 21:55:56 -0500 Subject: [PATCH 08/10] fixed linting issues --- lib/plugins/aws/deploy/compile/functions/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/functions/index.js b/lib/plugins/aws/deploy/compile/functions/index.js index 143878400..059f9dbff 100644 --- a/lib/plugins/aws/deploy/compile/functions/index.js +++ b/lib/plugins/aws/deploy/compile/functions/index.js @@ -82,17 +82,17 @@ class AwsCompileFunctions { } newFunction.Properties.Environment.Variables = Object.assign( - {}, - this.serverless.service.provider.environment, + {}, + this.serverless.service.provider.environment, functionObject.environment - ) + ); - for(var key in newFunction.Properties.Environment.Variables){ + Object.keys(newFunction.Properties.Environment.Variables).forEach((key) => { // I pulled this from the bash man pages - if(!key.match(/^[A-Za-z_][a-zA-Z0-9_]*$/)){ - throw new Error("Invalid characters in environment variable"); + if (!key.match(/^[A-Za-z_][a-zA-Z0-9_]*$/)) { + throw new Error('Invalid characters in environment variable'); } - } + }); if ('role' in functionObject) { newFunction.Properties.Role = this.compileRole(functionObject.role); From 2d390edfce2c817a62ad1d2f2cb63881cc8e0757 Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Mon, 21 Nov 2016 09:22:40 +0100 Subject: [PATCH 09/10] Only create environment variables if config is given --- .../aws/deploy/compile/functions/index.js | 29 ++++++++++--------- .../deploy/compile/functions/tests/index.js | 23 +-------------- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/functions/index.js b/lib/plugins/aws/deploy/compile/functions/index.js index 059f9dbff..44c61a827 100644 --- a/lib/plugins/aws/deploy/compile/functions/index.js +++ b/lib/plugins/aws/deploy/compile/functions/index.js @@ -81,18 +81,22 @@ class AwsCompileFunctions { newFunction.Properties.Description = functionObject.description; } - newFunction.Properties.Environment.Variables = Object.assign( - {}, - this.serverless.service.provider.environment, - functionObject.environment - ); + if (functionObject.environment || this.serverless.service.provider.environment) { + newFunction.Properties.Environment = {}; + newFunction.Properties.Environment.Variables = Object.assign( + {}, + this.serverless.service.provider.environment, + functionObject.environment + ); - Object.keys(newFunction.Properties.Environment.Variables).forEach((key) => { - // I pulled this from the bash man pages - if (!key.match(/^[A-Za-z_][a-zA-Z0-9_]*$/)) { - throw new Error('Invalid characters in environment variable'); - } - }); + Object.keys(newFunction.Properties.Environment.Variables).forEach((key) => { + // taken from the bash man pages + if (!key.match(/^[A-Za-z_][a-zA-Z0-9_]*$/)) { + const errorMessage = 'Invalid characters in environment variable'; + throw new this.serverless.classes.Error(errorMessage); + } + }); + } if ('role' in functionObject) { newFunction.Properties.Role = this.compileRole(functionObject.role); @@ -156,9 +160,6 @@ class AwsCompileFunctions { }, S3Key: 'S3Key', }, - Environment: { - Variables: {}, - }, FunctionName: 'FunctionName', Handler: 'Handler', MemorySize: 'MemorySize', diff --git a/lib/plugins/aws/deploy/compile/functions/tests/index.js b/lib/plugins/aws/deploy/compile/functions/tests/index.js index d6d5a6ccf..cef812d96 100644 --- a/lib/plugins/aws/deploy/compile/functions/tests/index.js +++ b/lib/plugins/aws/deploy/compile/functions/tests/index.js @@ -297,9 +297,6 @@ describe('AwsCompileFunctions', () => { awsCompileFunctions.serverless.service.package.artifact}`, S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, - Environment: { - Variables: {}, - }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', MemorySize: 1024, @@ -337,9 +334,6 @@ describe('AwsCompileFunctions', () => { S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, FunctionName: 'new-service-dev-func', - Environment: { - Variables: {}, - }, Handler: 'func.function.handler', MemorySize: 1024, Role: { 'Fn::GetAtt': ['IamRoleLambdaExecution', 'Arn'] }, @@ -421,7 +415,7 @@ describe('AwsCompileFunctions', () => { }); - it('should throw if invalid environment variable name', () => { + it('should throw an error if environment variable has invalid name', () => { awsCompileFunctions.serverless.service.functions = { func: { handler: 'func.function.handler', @@ -454,9 +448,6 @@ describe('AwsCompileFunctions', () => { S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, FunctionName: 'customized-func-function', - Environment: { - Variables: {}, - }, Handler: 'func.function.handler', MemorySize: 128, Role: { 'Fn::GetAtt': ['IamRoleLambdaExecution', 'Arn'] }, @@ -493,9 +484,6 @@ describe('AwsCompileFunctions', () => { awsCompileFunctions.serverless.service.package.artifact}`, S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, - Environment: { - Variables: {}, - }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', MemorySize: 1024, @@ -527,9 +515,6 @@ describe('AwsCompileFunctions', () => { awsCompileFunctions.serverless.service.package.artifact}`, S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, - Environment: { - Variables: {}, - }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', MemorySize: 1024, @@ -565,9 +550,6 @@ describe('AwsCompileFunctions', () => { awsCompileFunctions.serverless.service.package.artifact}`, S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, }, - Environment: { - Variables: {}, - }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', MemorySize: 128, @@ -605,9 +587,6 @@ describe('AwsCompileFunctions', () => { awsCompileFunctions.serverless.service.package.artifact}`, S3Bucket: bucketName, }, - Environment: { - Variables: {}, - }, FunctionName: 'new-service-dev-func', Handler: 'func.function.handler', MemorySize: 128, From 26ff0fafdd87f74a04af8fff62b9226a6e922784 Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Mon, 21 Nov 2016 09:39:17 +0100 Subject: [PATCH 10/10] Add additional tests --- .../deploy/compile/functions/tests/index.js | 96 ++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/lib/plugins/aws/deploy/compile/functions/tests/index.js b/lib/plugins/aws/deploy/compile/functions/tests/index.js index cef812d96..10792eca6 100644 --- a/lib/plugins/aws/deploy/compile/functions/tests/index.js +++ b/lib/plugins/aws/deploy/compile/functions/tests/index.js @@ -360,7 +360,7 @@ describe('AwsCompileFunctions', () => { }; }); - it('should create a function resource with Environment config', () => { + it('should create a function resource with environment config', () => { awsCompileFunctions.serverless.service.functions = { func: { handler: 'func.function.handler', @@ -414,6 +414,100 @@ describe('AwsCompileFunctions', () => { }; }); + it('should create a function resource with function level environment config', () => { + awsCompileFunctions.serverless.service.functions = { + func: { + handler: 'func.function.handler', + name: 'new-service-dev-func', + environment: { + test1: 'test1', + }, + }, + }; + + const compliedFunction = { + Type: 'AWS::Lambda::Function', + Properties: { + Code: { + S3Key: `${awsCompileFunctions.serverless.service.package.artifactDirectoryName}/${ + awsCompileFunctions.serverless.service.package.artifact}`, + S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, + }, + FunctionName: 'new-service-dev-func', + Handler: 'func.function.handler', + MemorySize: 1024, + Role: { 'Fn::GetAtt': ['IamRoleLambdaExecution', 'Arn'] }, + Runtime: 'nodejs4.3', + Timeout: 6, + Environment: { + Variables: { + test1: 'test1', + }, + }, + }, + }; + + awsCompileFunctions.compileFunctions(); + + expect( + awsCompileFunctions.serverless.service.provider.compiledCloudFormationTemplate + .Resources.FuncLambdaFunction + ).to.deep.equal(compliedFunction); + + awsCompileFunctions.serverless.service.functions = { + func: { + handler: 'func.function.handler', + }, + }; + }); + + it('should create a function resource with provider level environment config', () => { + awsCompileFunctions.serverless.service.functions = { + func: { + handler: 'func.function.handler', + name: 'new-service-dev-func', + }, + }; + + awsCompileFunctions.serverless.service.provider.environment = { + providerTest1: 'providerTest1', + }; + + const compliedFunction = { + Type: 'AWS::Lambda::Function', + Properties: { + Code: { + S3Key: `${awsCompileFunctions.serverless.service.package.artifactDirectoryName}/${ + awsCompileFunctions.serverless.service.package.artifact}`, + S3Bucket: { Ref: 'ServerlessDeploymentBucket' }, + }, + FunctionName: 'new-service-dev-func', + Handler: 'func.function.handler', + MemorySize: 1024, + Role: { 'Fn::GetAtt': ['IamRoleLambdaExecution', 'Arn'] }, + Runtime: 'nodejs4.3', + Timeout: 6, + Environment: { + Variables: { + providerTest1: 'providerTest1', + }, + }, + }, + }; + + awsCompileFunctions.compileFunctions(); + + expect( + awsCompileFunctions.serverless.service.provider.compiledCloudFormationTemplate + .Resources.FuncLambdaFunction + ).to.deep.equal(compliedFunction); + + awsCompileFunctions.serverless.service.functions = { + func: { + handler: 'func.function.handler', + }, + }; + }); it('should throw an error if environment variable has invalid name', () => { awsCompileFunctions.serverless.service.functions = {