Serverless Plugin Utilities

0.2.0 · active · verified Sun Apr 19

serverless-plugin-utils is a collection of essential utilities designed to augment the Serverless Framework's configuration capabilities. Currently stable at version 0.2.0, it provides a set of helper functions primarily consumed directly within `serverless.yml` files, enhancing the declarative power of serverless configurations. These utilities facilitate common operations such as string manipulation (lowercase, uppercase, capitalize, split, join), conditional logic (ternary, switch statements), and other dynamic value resolutions, which are frequently encountered when defining resources, environment variables, or other service properties. It allows developers to create more dynamic and adaptive Serverless configurations without resorting to external scripting or complex custom logic, streamlining tasks like enforcing S3 bucket naming conventions or dynamic domain construction. The package is typically installed as a development dependency and integrates seamlessly into the Serverless Framework's variable resolution system.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `serverless-plugin-utils` into your `serverless.yml` and use its functions to dynamically set environment variables for a Lambda function. It showcases `lower`, `capitalize`, `join`, and `ternary` utilities.

service: my-serverless-app
frameworkVersion: '3'

plugins:
  - serverless-plugin-utils

provider:
  name: aws
  runtime: nodejs18.x
  stage: ${opt:stage, 'dev'}
  environment:
    SERVICE_NAME_LOWER: ${fn:lower(self:service)}
    DEPLOY_STAGE_CAPITALIZED: ${fn:capitalize(self:provider.stage)}
    DYNAMIC_BUCKET_NAME: ${fn:join('-', [self:service, ${fn:lower(self:provider.stage)}, 'resources'])}
    IS_PROD: ${fn:ternary(${self:provider.stage} == 'prod', true, false)}

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: get

# handler.ts example (not directly importing utils, but using env vars)
// export const hello = async (event: any) => {
//   const serviceName = process.env.SERVICE_NAME_LOWER ?? '';
//   const stage = process.env.DEPLOY_STAGE_CAPITALIZED ?? '';
//   const isProd = process.env.IS_PROD === 'true';
//   return {
//     statusCode: 200,
//     body: JSON.stringify({
//       message: `Hello from ${serviceName} in ${stage} (Prod: ${isProd})!`,
//       input: event,
//     }),
//   };
// };

view raw JSON →