├── .eslintrc.json ├── .gitignore ├── .travis.yml ├── README.md ├── __tests__ └── index.test.js ├── index.js ├── package-lock.json └── package.json /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "node": true, 5 | "jest": true 6 | }, 7 | "extends": [ 8 | "eslint:recommended" 9 | ], 10 | "parserOptions": { 11 | "sourceType": "module", 12 | "ecmaVersion": 2017 13 | }, 14 | "rules": { 15 | "indent": [ 16 | "error", 17 | 4, 18 | { 19 | "SwitchCase": 1 20 | } 21 | ], 22 | "linebreak-style": [ 23 | "error", 24 | "unix" 25 | ], 26 | "quotes": [ 27 | "error", 28 | "double", 29 | "avoid-escape" 30 | ], 31 | "semi": [ 32 | "error", 33 | "always" 34 | ] 35 | } 36 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | branches: 2 | only: 3 | - master 4 | language: node_js 5 | node_js: 6 | - "12" 7 | script: 8 | - npm test 9 | - npm run eslint 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![NPM](https://nodei.co/npm/serverless-plugin-ifelse.png)](https://nodei.co/npm/serverless-plugin-ifelse/) 2 | 3 | [![Build Status](https://api.travis-ci.org/anantab/serverless-plugin-ifelse.png)](https://travis-ci.org/anantab/serverless-plugin-ifelse) 4 | [![devDependencies Status](https://david-dm.org/anantab/serverless-plugin-ifelse/dev-status.svg)](https://david-dm.org/anantab/serverless-plugin-ifelse?type=dev) 5 | 6 | # Serverless Plugin IfElse 7 | While you can use serverless variables to define different values for your atrributes based on either stage or other properties, it sometimes is not as straightforward. 8 | 9 | For example, If you have a serverless project with 3 functions and you want to deploy all 3 functions in one region but only 2 of them in other region, then there is no easier way to exclude the third function based on region you are deploying. 10 | 11 | Another use case that inspired me to write this plugin was, I wanted to use ```iamRoleStatements``` for all my Lambda functions in staging but use a pre-define ```role``` in production. You cannot have both attributes in serverless.yml file as serverless ignores ```iamRoleStatements``` if there is ```role``` attribute. 12 | 13 | This plugin allows you to write if else conditions in ```serverless.yml``` file to add, remove or change the values of attributes in the yml file. It works with both ```package``` and ```deploy``` commands. It also works with ```serverless-offline``` plugin. 14 | 15 | 16 | # Installation 17 | ```npm i serverless-plugin-ifelse --save-dev``` 18 | 19 | ## Example 20 | serverless.yml 21 | ``` 22 | service: serverlessIfElseExample 23 | custom: 24 | currentStage: ${opt:stage, self:provider.stage} 25 | serverlessIfElse: 26 | - If: '"${self:custom.currentStage}" == "dev"' 27 | Exclude: 28 | - provider.role 29 | - provider.environment.ENV1 30 | - functions.func3 31 | - functions.func1.events.0.http.authorizer 32 | Set: 33 | provider.timeout: 90 34 | provider.profile: dev 35 | ElseExclude: 36 | - provider.environment.ENV2 37 | ElseSet: 38 | provider.timeout: 120 39 | 40 | - ExcludeIf: 41 | functions.func1: '"${self:provider.region}" == "ap-southeast-2"' 42 | functions.func2: '"${opt:region}" == "us-east-1"' 43 | 44 | - If: '"${self:provider.region}" == "us-east-1"' 45 | Exclude: 46 | - functions.func1 47 | Set: 48 | provider.timeout: 300 49 | 50 | 51 | plugins: 52 | - serverless-plugin-ifelse 53 | 54 | provider: 55 | name: aws 56 | runtime: nodejs8.10 57 | stage: dev 58 | region : ap-southeast-2 59 | timeout: 60 60 | environment: 61 | ENV1: env-val-1 62 | ENV2: env-val-2 63 | profile: default 64 | iamRoleStatements: 65 | - Effect: Allow 66 | Action: 67 | - s3:* 68 | Resource: "*" 69 | role: arn:aws:iam::xxxxxxxxxxxx:role/testRole 70 | 71 | functions: 72 | func1: 73 | name: Function 1 74 | handler: lambda.func1 75 | events: 76 | - http: 77 | path: "path1" 78 | method: "post" 79 | authorizer: 80 | arn: arn:aws:cognito-idp:us-east-1:123456789012:userpool/us-east-1_0acCDefgh 81 | 82 | func2: 83 | name: Function 2 84 | handler: lambda.func2 85 | events: 86 | - http: 87 | path: "path2" 88 | method: "post" 89 | 90 | func3: 91 | name: Function 3 92 | handler: lambda.func2 93 | events: 94 | - http: 95 | path: "path3" 96 | method: "post" 97 | ``` 98 | 99 | Put your conditions in custom variable ```serverlessIfElse```. 100 | 101 | ## - If 102 | Write your if condition inside single quote ```''```. Inside your condition, all your variables that resolve in string or the string themselves should be inside double quote. The plugin will otherwise encounter undefined variable error and the condition will not work. The condition can be anything, like == , !=, <, > or even javascript regular expressions. 103 | 104 | ```- If: '"${self:provider.stage}" == "dev"'``` 105 | 106 | ### Exclude 107 | If condition in If is true, all attibutes in Exclude will be ignored before serverless package or deploy your stack and hence serverless will not see those attributes. 108 | 109 | ### Set 110 | If condition in If is true, the value of the attribute will be updated with new value. 111 | 112 | ### ElseExclude 113 | If condition in If is false,the attibutes will be ignored. 114 | 115 | ### ElseSet 116 | If condition in If is false, the value of the attribute will be updated with new value. 117 | 118 | ## - ExcludeIf 119 | Use ExcludeIf, if you want to write conditions per attribute. If condition is true, only that attribute will be ignored. 120 | 121 | 122 | ```You can write as many conditions as you like and exclude or set attributes any level deep in the yml file.``` 123 | 124 | ## Note 125 | This plugin will ignore or update value of attributes based on your conditions and does not evaluate if it causes any side effect. You are responsbile to make sure ignoring or setting new values will work as you expected and will not cause serverless to throw error. 126 | 127 | The plugin will not remove or update any first level attributes in serverless.yml file like ```service``` or ```provider``` or ```functions```. 128 | 129 | ## Warning 130 | The conditions are executed using ```eval```, which executes the conditions as Javascript. Basically any javascript code in ```If``` or ```Else``` conditions are executed. This may cause security issues, especially if you have multiple (or community) contributors. Please make sure the code is properly reviewed and it does not cause any security issues. 131 | -------------------------------------------------------------------------------- /__tests__/index.test.js: -------------------------------------------------------------------------------- 1 | const serverlessPluginIfElse = require("../index"); 2 | let serverless = {}; 3 | 4 | describe("Test Serverless IfElse Plugin With Condition Set 1", () => { 5 | beforeAll(() => { 6 | const condition = getConditions("condition1"); 7 | serverless = getServerless(); 8 | serverless.service.custom.serverlessIfElse = condition; 9 | const serverlessIfElse = new serverlessPluginIfElse(serverless); 10 | serverlessIfElse.applyConditions(); 11 | }); 12 | 13 | it("It Should Remove Serverless Properties in Exlude when If condition Matches", () => { 14 | expect(serverless.service.functions.func1).toBeUndefined(); 15 | expect(serverless.service.functions.role).toBeUndefined(); 16 | }); 17 | 18 | it("It Should Set Serverless Properties in Set When If condition Matches", () => { 19 | expect(serverless.service.provider.profile).toBe("dev"); 20 | }); 21 | 22 | it("It Should Not Remove Serverless Properties in ElseExclude condition when If condition Matches", () => { 23 | expect(serverless.service.functions.func2).toBeDefined(); 24 | expect(serverless.service.functions.func2.name).toBe("Function 2"); 25 | expect(serverless.service.provider.iamRoleStatements).toBeDefined(); 26 | }); 27 | 28 | it("It Should Not Set Serverless Properties in ElseSet when If condition Matches", () => { 29 | expect(serverless.service.provider.timeout).toBe(300); 30 | }); 31 | 32 | it("It Should Not Remove Serverless Properties in ExcludeIf when condition Does not Match", () => { 33 | expect(serverless.service.functions.func3).toBeDefined(); 34 | expect(serverless.service.functions.func3.name).toBe("Function 3"); 35 | }); 36 | 37 | it("It Should create property if not defined", () => { 38 | expect(serverless.service.provider.region).toBe("ap-southeast-2"); 39 | }); 40 | }); 41 | 42 | 43 | describe("Test Serverless IfElse Plugin With Condition Set 2", () => { 44 | beforeAll(() => { 45 | const condition = getConditions("condition2"); 46 | serverless = getServerless(); 47 | serverless.service.custom.serverlessIfElse = condition; 48 | const serverlessIfElse = new serverlessPluginIfElse(serverless); 49 | serverlessIfElse.applyConditions(); 50 | }); 51 | 52 | it("It Should Not Remove Serverless Properties in Exlude when If condition Does Not Match", () => { 53 | expect(serverless.service.functions.func1).toBeDefined(); 54 | expect(serverless.service.functions.func1.name).toBe("Function 1"); 55 | }); 56 | 57 | it("It Should Not Set Serverless Properties in Set When If condition Does Not Match", () => { 58 | expect(serverless.service.provider.profile).toBe("default"); 59 | }); 60 | 61 | it("It Should Remove Serverless Properties in ElseExclude when If condition Does Not Match", () => { 62 | expect(serverless.service.functions.func2).toBeUndefined(); 63 | expect(serverless.service.provider.iamRoleStatements).toBeUndefined(); 64 | }); 65 | 66 | it("It Should Set Serverless Properties in ElseSet when If condition Does Not Match", () => { 67 | expect(serverless.service.provider.timeout).toBe(90); 68 | }); 69 | 70 | it("It Should Remove Serverless Properties in ExcludeIf when condition Matches", () => { 71 | expect(serverless.service.functions.func3).toBeUndefined(); 72 | }); 73 | }); 74 | 75 | describe("Test Serverless IfElse Plugin With Condition Set 3", () => { 76 | beforeAll(() => { 77 | const condition = getConditions("condition3"); 78 | serverless = getServerless(); 79 | serverless.service.custom.serverlessIfElse = condition; 80 | const serverlessIfElse = new serverlessPluginIfElse(serverless); 81 | serverlessIfElse.applyConditions(); 82 | }); 83 | 84 | it("It Should Set Serverless Properties in Set when If condition Matches and the serverless property is falsy boolean value ", () => { 85 | expect(serverless.service.custom.customCertificate.enabled).toBeTruthy(); 86 | }); 87 | }); 88 | 89 | const getServerless = function () { 90 | return { 91 | service: { 92 | service: "Serverless Condition", 93 | custom: { 94 | serverlessExclude: [], 95 | customCertificate: { 96 | enabled: false 97 | } 98 | }, 99 | provider: { 100 | name: "aws", 101 | runtime: "nodejs6.10", 102 | timeout: 300, 103 | stage: "dev", 104 | profile: "default", 105 | role: "arn:aws:iam::xxxxxxxxxxxx:role/Test", 106 | iamRoleStatements: [{ 107 | Effect: "Allow", 108 | Action: ["s3:*"], 109 | Resource: "*" 110 | }] 111 | }, 112 | functions: { 113 | func1: 114 | { 115 | name: "Function 1", 116 | handler: "func1.handler", 117 | events: [{ 118 | http: { 119 | path: "path1", 120 | method: "post", 121 | private: true, 122 | cors: true 123 | } 124 | } 125 | ], 126 | }, 127 | func2: 128 | { 129 | name: "Function 2", 130 | handler: "func2.handler", 131 | events: [{ 132 | http: { 133 | path: "path2", 134 | method: "post", 135 | private: true, 136 | cors: true 137 | } 138 | } 139 | ], 140 | }, 141 | func3: 142 | { 143 | name: "Function 3", 144 | handler: "func3.handler", 145 | events: [{ 146 | http: { 147 | path: "path3", 148 | method: "post", 149 | private: true, 150 | cors: true 151 | } 152 | } 153 | ], 154 | } 155 | } 156 | }, 157 | cli: { 158 | log: jest.fn() 159 | } 160 | }; 161 | }; 162 | 163 | const getConditions = function (condition) { 164 | const conditions = { 165 | condition1: [ 166 | { 167 | If: '"true"=="true"', 168 | Exclude: [ 169 | "functions.func1", 170 | "provider.role" 171 | ], 172 | Set: { 173 | "provider.profile": "dev", 174 | "provider.region": "ap-southeast-2" 175 | }, 176 | ElseExclude: [ 177 | "functions.func2", 178 | "provider.iamRoleStatements" 179 | ], 180 | ElseSet: { 181 | "provider.stage": "production", 182 | "provider.timeout": 90 183 | } 184 | }, 185 | { 186 | ExcludeIf: 187 | { 188 | "functions.func3": '"true" == "false"', 189 | } 190 | } 191 | ], 192 | condition2: [ 193 | { 194 | If: '"true"=="false"', 195 | Exclude: [ 196 | "functions.func1", 197 | ], 198 | Set: { 199 | "provider.profile": "production", 200 | "provider.timeout": 60 201 | }, 202 | ElseExclude: [ 203 | "functions.func2", 204 | "provider.iamRoleStatements" 205 | ], 206 | ElseSet: { 207 | "provider.timeout": 90 208 | } 209 | }, 210 | { 211 | ExcludeIf: 212 | { 213 | "functions.func3": '"true" == "true"', 214 | } 215 | } 216 | ], 217 | condition3: [ 218 | { 219 | If: '"true"=="true"', 220 | Set: { 221 | "custom.customCertificate.enabled": "true", 222 | }, 223 | } 224 | ] 225 | }; 226 | return conditions[condition]; 227 | }; -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | class serverlessPluginIfElse { 2 | /** 3 | * 4 | * @param {*} serverless 5 | * @param {*} options 6 | */ 7 | constructor(serverless, options = {}) { 8 | this.serverless = serverless; 9 | this.options = options; 10 | this.hooks = { 11 | "initialize": this.applyConditions.bind(this), 12 | "before:remove:remove": this.applyConditions.bind(this), 13 | "before:offline:start:init": this.applyConditions.bind(this), 14 | "before:offline:start": this.applyConditions.bind(this), 15 | }; 16 | this.pluginName = "serverless-plugin-ifelse"; 17 | } 18 | /** 19 | * 20 | */ 21 | applyConditions() { 22 | let params = this.serverless.service.custom.serverlessIfElse; 23 | if (!this.isvalidObject(params)) { 24 | return; 25 | } 26 | params.forEach((item) => { 27 | if (item.If) { 28 | try { 29 | if (eval(item.If)) { 30 | this.conditionMatchLog(item.If, true); 31 | this.setItemValues(item.Set); 32 | this.removeItems(item.Exclude); 33 | } else { 34 | this.conditionMatchLog(item.If, false); 35 | this.setItemValues(item.ElseSet); 36 | this.removeItems(item.ElseExclude); 37 | } 38 | } catch (e) { 39 | this.evaluateErrorLog(item.If, e); 40 | } 41 | } 42 | 43 | if (this.isvalidObject(item.ExcludeIf)) { 44 | Object.keys(item.ExcludeIf).forEach((exludeKey) => { 45 | try { 46 | if (eval(item.ExcludeIf[exludeKey])) { 47 | this.conditionMatchLog(item.ExcludeIf[exludeKey], true); 48 | this.changeKey(exludeKey); 49 | } 50 | } 51 | catch (e) { 52 | this.evaluateErrorLog(item.ExcludeIf[exludeKey], e); 53 | } 54 | }); 55 | } 56 | }); 57 | } 58 | 59 | /** 60 | * 61 | * @param {*} item 62 | */ 63 | isvalidObject(item) { 64 | return item && typeof item == "object"; 65 | } 66 | 67 | /** 68 | * 69 | * @param {*} items 70 | */ 71 | setItemValues(items) { 72 | if (!this.isvalidObject(items)) { 73 | return; 74 | } 75 | Object.keys(items).forEach((key) => { 76 | this.changeKey(key, "set", items[key]); 77 | }); 78 | } 79 | /** 80 | * 81 | * @param {*} item 82 | */ 83 | removeItems(item) { 84 | if (!item) { 85 | return; 86 | } 87 | if (typeof item == "object") { 88 | item.forEach((key) => { 89 | this.changeKey(key); 90 | }); 91 | } else { 92 | this.changeKey(item); 93 | } 94 | } 95 | 96 | /** 97 | * 98 | * @param {*} keyPath 99 | */ 100 | changeKey(keyPath, type = "remove", newValue = null) { 101 | let path = keyPath.split("."); 102 | if (path.length <= 1) { 103 | return; 104 | } 105 | let item = this.serverless.service; 106 | let i = 0; 107 | for (i; i < path.length - 1; i++) { 108 | item = item[path[i]]; 109 | if (!item) { 110 | return; 111 | } 112 | } 113 | if (path[i] in item) { 114 | if (type == "remove") { 115 | this.serverless.cli.log(this.pluginName + " - Excluding: " + keyPath); 116 | delete item[path[i]]; 117 | } else if (type == "set") { 118 | item[path[i]] = newValue; 119 | if (typeof newValue == "object") { 120 | newValue = JSON.stringify(newValue); 121 | } 122 | this.serverless.cli.log(this.pluginName + " - Value Changed for : " + keyPath + " to: " + newValue); 123 | } 124 | // If item not exists => add it to path 125 | } else { 126 | if (type == "set") { 127 | item[path[i]] = newValue; 128 | if (typeof newValue == "object") { 129 | newValue = JSON.stringify(newValue); 130 | } 131 | this.serverless.cli.log(this.pluginName + " - Value Changed for : " + keyPath + " to: " + newValue); 132 | } 133 | } 134 | 135 | 136 | } 137 | 138 | /** 139 | * 140 | * @param {*} condition 141 | * @param {*} matched 142 | */ 143 | conditionMatchLog(condition, matched = true) { 144 | if (this.options.v || this.options.verbose) { 145 | this.serverless.cli.log(this.pluginName + " - (" + condition + ") Condition" + 146 | ((!matched) ? " not" : "") + " true."); 147 | } 148 | } 149 | 150 | /** 151 | * 152 | * @param {*} condition 153 | * @param {*} e 154 | */ 155 | evaluateErrorLog(condition, e) { 156 | this.serverless.cli.log(this.pluginName + " - cannot evaluate condition " + 157 | condition + " : " + e); 158 | } 159 | } 160 | 161 | module.exports = serverlessPluginIfElse; 162 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-plugin-ifelse", 3 | "version": "1.0.7", 4 | "description": "A Serverless Plugin to write If Else conditions in serverless YAML file", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "jest", 8 | "eslint": "eslint --ignore-path .gitignore \"./**/*.js\"", 9 | "eslint-fix": "eslint --ignore-path .gitignore --fix \"./**/*.js\"" 10 | }, 11 | "keywords": [ 12 | "serverless", 13 | "serverless-plugin", 14 | "serverless-ifelse", 15 | "ifelse" 16 | ], 17 | "author": "Ananta Bhandari", 18 | "license": "MIT", 19 | "devDependencies": { 20 | "eslint": "^7.20.0", 21 | "jest": "^26.6.3" 22 | }, 23 | "dependencies": {}, 24 | "repository": { 25 | "type": "git", 26 | "url": "git+https://github.com/anantab/serverless-plugin-ifelse.git" 27 | }, 28 | "bugs": { 29 | "url": "https://github.com/anantab/serverless-plugin-ifelse/issues" 30 | }, 31 | "homepage": "https://github.com/anantab/serverless-plugin-ifelse" 32 | } 33 | --------------------------------------------------------------------------------