Registry / devops / serverless-cloudformation-sub-variables

serverless-cloudformation-sub-variables

JSON →
library0.2.1jsnpmunverified

This plugin for the Serverless Framework enhances `serverless.yml` files by automatically converting a custom `#{Variable}` syntax into AWS CloudFormation `Fn::Sub` functions. It supports a wide range of CloudFormation variables, including template parameters, resource logical IDs, resource attributes, pseudo parameters (e.g., `AWS::Region`), literal variables, and key-value map `Fn::Sub` functions. The plugin operates during the `package` command, executing its transformations after Serverless variables (`${self:variable}`) are resolved but before the CloudFormation template is packaged for deployment. A key feature is its ability to automatically wrap strings with `Fn::Sub` unless `Fn::Sub` is already explicitly defined in the parent YAML node, providing flexibility for complex scenarios. The current stable version is 0.2.1, released in late 2023, reflecting a deliberate, slower release cadence focused on stability and specific bug fixes. Its primary differentiator is the transparent and generic conversion of `#{...}` syntax across the entire CloudFormation template portion of `serverless.yml`, simplifying complex CloudFormation variable usage without extensive manual `Fn::Sub` declarations.

npm install serverless-cloudformation-sub-variables
INSTALL
IMPORT
SIG · SERVERLESS-CLOUDFO
S
serverless-cloudformation-sub-variables
devopsjavascriptv0.2.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

serverless-cloudformation-sub-variables
plugins: - serverless-cloudformation-sub-variables
import * as SubVariablesPlugin from 'serverless-cloudformation-sub-variables'; const plugin = require('serverless-cloudformation-sub-variables');
This package is a Serverless Framework plugin and is activated by including its exact package name as a string entry in the 'plugins' array within your 'serverless.yml' configuration. It does not expose any JavaScript or TypeScript symbols for direct programmatic import or use in application code.

Demonstrates how to install the plugin via npm and configure it in `serverless.yml` to automatically convert `#{...}` syntax into `Fn::Sub` for CloudFormation resources and outputs, including pseudo-parameters and resource references.

npm install serverless-cloudformation-sub-variables # serverless.yml service: my-sub-service plugins: - serverless-cloudformation-sub-variables provider: name: aws runtime: nodejs18.x stage: dev region: us-east-1 resources: Resources: MyBucket: Type: AWS::S3::Bucket Properties: BucketName: #{AWS::StackName}-#{self:provider.stage}-unique-bucket Tags: - Key: Environment Value: #{AWS::Region} MyLambdaRole: Type: AWS::IAM::Role Properties: RoleName: #{AWS::StackName}-#{AWS::Region}-MyLambdaRole AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole outputs: BucketNameOutput: Description: The name of the S3 bucket created by the service. Value: #{MyBucket} Export: Name: #{AWS::StackName}-MyBucketName # To deploy after setting up serverless.yml # serverless deploy
serverless --version
Debug
Known issues
gotchaThe plugin uses `#{Variable}` syntax, which is similar to, but distinct from, Serverless Framework's native variable syntax (`${self:variable}`, `${env:VARIABLE}`). Mixing these can lead to confusion or incorrect evaluation order if not understood.
fix
Understand the processing order: Serverless variables (`${...}`) are resolved first by the Serverless Framework itself. Afterwards, this plugin converts `#{...}` into CloudFormation `Fn::Sub` functions. Design your `serverless.yml` accordingly, using `${...}` for Serverless-specific variables and `#{...}` for CloudFormation intrinsic variables.
affects: >=0.1.0
breakingPrior to version 0.2.1, the plugin would fail or behave unexpectedly if a key targeted for `Fn::Sub` conversion had a `null` or `undefined` value, leading to potential CloudFormation template errors.
fix
Upgrade to version 0.2.1 or newer to benefit from the fix that gracefully handles `null` or `undefined` values by skipping their processing. If upgrading is not immediately possible, ensure all values targeted for `Fn::Sub` conversion are explicitly defined and not `null` or `undefined`.
affects: <0.2.1
gotchaIf `Fn::Sub` is already explicitly defined on a direct parent node that contains a string with `#{...}` variables, the plugin will *not* add an additional `Fn::Sub` wrapper to that string. This is by design to prevent double wrapping but can be a source of confusion if expecting every `#{...}` string to receive a new `Fn::Sub` wrapper.
fix
Design your CloudFormation templates knowing this behavior. If you want to declare a key-value map for `Fn::Sub` (e.g., `Fn::Sub: [ 'string', { key: value } ]`), explicitly use `Fn::Sub`. For simpler strings containing `#{...}` without a parent `Fn::Sub`, the plugin will automatically add the wrapper.
affects: >=0.1.0
Errors
Common errors & fixes
The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [...]
A CloudFormation intrinsic variable (e.g., a resource logical ID like `#{MyResource}` or a pseudo-parameter) was not correctly resolved by the plugin, or the referenced resource/parameter does not exist or is misspelled.
fix
Double-check the exact spelling and casing of your CloudFormation logical IDs, pseudo-parameters, or attributes (e.g., `#{MyResource.Arn}`). Ensure the resource or parameter you are referencing actually exists in your template or is a valid AWS pseudo-parameter. The plugin only converts `#{...}` to `${...}` for CloudFormation, so the underlying CloudFormation validation still applies.
Cannot resolve variable at 'plugins[1]' in serverless.yml
The Serverless Framework could not find or load the plugin, often due to a typo in the plugin name in `serverless.yml` or the package not being correctly installed.
fix
Verify that `serverless-cloudformation-sub-variables` is correctly spelled in your `plugins` section of `serverless.yml`. Confirm that the package is installed in your project's `node_modules` by running `npm install serverless-cloudformation-sub-variables` or `yarn add serverless-cloudformation-sub-variables`.
YAMLException: can not read a block mapping entry; a multiline key may not be an implicit key
Incorrect YAML indentation or syntax in your `serverless.yml`, particularly around complex structures involving `Fn::Sub` or plugin configuration.
fix
Review your `serverless.yml` for correct YAML syntax, paying close attention to indentation, colons, and hyphens. Use a YAML linter or an IDE with good YAML support to help identify syntax errors. Ensure that string values containing special characters or multiline content are properly quoted if necessary.
Upgrade
Version history
0.2.1latest on npm
Audit
Dependencies
serverlessrequiredThis package is a plugin for the Serverless Framework and requires the Serverless CLI to function.
Agent activity
10 hits · last 30 days
node
10
Resources
serverless-cloudformation-sub-variables — npm install serverless-cloudformation-sub-variables · libregistry