node.jsserverlessserverless-frameworkserverless-offline

How to pass custom cli commands to serverless-offline?


I am migrating a serverless application to it's most recent versions.

This is the starting point:

"devDependencies": {
    "serverless": "^2.43.1",
    "serverless-offline": "^7.0.0",
    "serverless-plugin-warmup": "^5.2.3",
    "serverless-prune-plugin": "^1.5.0"
}

New versions:

"devDependencies": {
    "serverless": "^3.33.0",
    "serverless-offline": "^12.0.4",
    "serverless-plugin-warmup": "^8.2.1",
    "serverless-prune-plugin": "^2.0.2"
}

In it's original state, we've passed in a custom variable to indicate the "account" to use:

"develop": "IS_LOCAL=true serverless offline --target-account local"

This flag is used to get the config in the serverless.yml

service: myApplication

frameworkVersion: '3'
unresolvedVariablesNotificationMode: error
provider:
  name: aws
  region: eu-central-1
  runtime: nodejs18.x
  stage: ${opt:stage, 'dev'}
custom:
  accountId: ${file(./serverless-helper.js):getAccountId}
  config: ${file(serverless-config.yml):${opt:target-account}}

The serverless-config.yml then has differend keys for the target account flag:

dev:
  rolePermissionsBoundaryArn: arn:aws:iam::
  cfnRoleArn: 
prd:
  rolePermissionsBoundaryArn: ...
  cfnRoleArn: ...
  logRetentionInDays: 30

local:
  rolePermissionsBoundaryArn: arn:aws:iam::...
  logRetentionInDays: 30

Running the develop script now returns the following error:

āžœ npm run develop

> myApplication@1.0.0 develop
> IS_LOCAL=true serverless offline --target-account local

Environment: linux, node 18.16.1, framework 3.33.0 (local), plugin 6.2.3, SDK 4.3.2
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
Detected unrecognized CLI options: "--target-account".

How can I pass in the custom target-account flag using serverless-offline in its current version?


Solution

  • The root cause is a breaking change in v3: https://www.serverless.com/framework/docs/guides/upgrading-v3

    Free form parameter are no longer allowed and have to be wrapped inside the --param options now.

    v2:

    IS_LOCAL=true serverless offline --target-account local`
    

    v3:

    IS_LOCAL=true serverless offline --param='target-account=local'
    

    if you need to reference them in your config, do not use the opt syntax. Instead use param now:

    config: ${file(serverless-config.yml):${param:target-account}}