Serverless Plugin Utilities
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
-
Cannot resolve variable at 'provider.environment.MY_VAR': Variable 'fn:lower' not found.
cause The `serverless-plugin-utils` plugin has not been correctly registered in your `serverless.yml` or the Serverless Framework version is incompatible.fixEnsure `serverless-plugin-utils` is listed under the `plugins` section in your `serverless.yml`: ```yaml plugins: - serverless-plugin-utils ``` Also, check the compatibility of the plugin with your Serverless Framework version. -
YAMLException: bad indentation of a mapping entry
cause Incorrect YAML syntax when defining or calling a utility function within `serverless.yml`.fixYAML is whitespace-sensitive. Carefully review the indentation and spacing of your `serverless.yml` file, especially around utility function calls and their arguments. Use a YAML linter if available.
Warnings
- gotcha This package is at a low version (0.2.0), which historically indicates it may be in early development. While it appears stable for its advertised functionality, anticipate potential breaking changes in minor or patch versions if the project becomes more actively developed, or if Serverless Framework itself introduces breaking changes that impact plugin compatibility.
- gotcha The utilities provided by this plugin are primarily designed for use within `serverless.yml` via the Serverless Framework's variable system (e.g., `${fn:lower('...')}`). Attempting to `import` or `require` these utility functions directly into your JavaScript/TypeScript Lambda code will not work as expected, as they are not exposed as standard module exports for programmatic consumption.
- gotcha This plugin registers its utilities under a specific variable namespace (often `fn:` or implicit). Ensure that your `serverless.yml` file correctly lists `serverless-plugin-utils` in the `plugins` section. Forgetting to register the plugin or using an incorrect variable prefix will lead to 'variable not found' errors.
Install
-
npm install serverless-plugin-utils -
yarn add serverless-plugin-utils -
pnpm add serverless-plugin-utils
Imports
- lower
import { lower } from 'serverless-plugin-utils'${fn:lower('YourString')} - join
require('serverless-plugin-utils').join()${fn:join(',', ['item1', 'item2'])} - ternary
const ternary = require('serverless-plugin-utils').ternary;${fn:ternary(condition, 'trueValue', 'falseValue')}
Quickstart
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,
// }),
// };
// };