npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serverless-plugin-ifelse

v1.0.7

Published

A Serverless Plugin to write If Else conditions in serverless YAML file

Downloads

216,909

Readme

NPM

Build Status devDependencies Status

Serverless Plugin IfElse

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.

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.

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.

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.

Installation

npm i serverless-plugin-ifelse --save-dev

Example

serverless.yml

service: serverlessIfElseExample
custom:
  currentStage: ${opt:stage, self:provider.stage}
  serverlessIfElse:
      - If: '"${self:custom.currentStage}" == "dev"'
        Exclude:
          - provider.role
          - provider.environment.ENV1
          - functions.func3
          - functions.func1.events.0.http.authorizer
        Set:
          provider.timeout: 90
          provider.profile: dev
        ElseExclude:
          - provider.environment.ENV2
        ElseSet:
           provider.timeout: 120

      - ExcludeIf:
           functions.func1: '"${self:provider.region}" == "ap-southeast-2"'
           functions.func2: '"${opt:region}" == "us-east-1"'

      - If: '"${self:provider.region}" == "us-east-1"'
        Exclude:
          - functions.func1
        Set:
          provider.timeout: 300


plugins:
  - serverless-plugin-ifelse

provider:
  name: aws
  runtime: nodejs8.10
  stage: dev
  region : ap-southeast-2
  timeout: 60
  environment:
    ENV1: env-val-1
    ENV2: env-val-2
  profile: default
  iamRoleStatements:
    - Effect: Allow
      Action:
      - s3:*
      Resource: "*"
  role: arn:aws:iam::xxxxxxxxxxxx:role/testRole

functions:
  func1:
    name: Function 1
    handler: lambda.func1
    events:
      - http:
          path: "path1"
          method: "post"
          authorizer:
            arn: arn:aws:cognito-idp:us-east-1:123456789012:userpool/us-east-1_0acCDefgh

  func2:
    name: Function 2
    handler: lambda.func2
    events:
      - http:
           path: "path2"
           method: "post"

  func3:
    name: Function 3
    handler: lambda.func2
    events:
      - http:
           path: "path3"
           method: "post"

Put your conditions in custom variable serverlessIfElse.

- If

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.

- If: '"${self:provider.stage}" == "dev"'

Exclude

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.

Set

If condition in If is true, the value of the attribute will be updated with new value.

ElseExclude

If condition in If is false,the attibutes will be ignored.

ElseSet

If condition in If is false, the value of the attribute will be updated with new value.

- ExcludeIf

Use ExcludeIf, if you want to write conditions per attribute. If condition is true, only that attribute will be ignored.

You can write as many conditions as you like and exclude or set attributes any level deep in the yml file.

Note

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.

The plugin will not remove or update any first level attributes in serverless.yml file like service or provider or functions.

Warning

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.