Skip to content

fix: run plugin after all variables resolved#10

Open
Luke-Davies wants to merge 1 commit intoconcon121:masterfrom
Luke-Davies:master
Open

fix: run plugin after all variables resolved#10
Luke-Davies wants to merge 1 commit intoconcon121:masterfrom
Luke-Davies:master

Conversation

@Luke-Davies
Copy link
Copy Markdown

@Luke-Davies Luke-Davies commented Oct 26, 2021

Variables that have a delayed resolution will not be resolved before the plugin runs. This affects custom variables.

For example:

  serverless-aws-resource-names:
    source: mapping.json
    variables: 
      resourceNamesPrefix: ${ssm:/path/to/service/myParam}

or

  serverless-aws-resource-names:
    source: mapping.json
    variables: 
      resourceNamesPrefix: ${s3:my-env-bucket/env-prefix.txt}

resourceNamesPrefix may not be resolved before the plugin executes, so the unresolved variable string is used. I'm seeing this occur on a simple serverless file where I use a plugin to load common environment config from S3:

service: test-app

frameworkVersion: '2'

variablesResolutionMode: 20210326

provider:
  name: aws
  runtime: nodejs14.x
  lambdaHashingVersion: 20201221
  region: eu-west-1
  stackName: ${self:custom.config.prefix}-test-app
  stage: dev
  profile: dev

functions:
  helloWorld:
    handler: helloWorld.handler

package:
  patterns:
    - '!node_modules/aws-sdk'
    - '!.serverless'
    - '!README.md'

custom:
  # reads a json file from S3
  config: ${s3obj:orgnamehere-${opt:stage, self:provider.stage}-env/config.json}
  serverless-aws-resource-names:
    source: mapping.json
    variables: 
      resourceNamesPrefix: ${self:custom.config.prefix}


plugins:
  - serverless-s3obj-variable-resolver
  - serverless-aws-resource-names

This is mentioned in the docs:

Note: Variable references in the serverless instance are not resolved before a Plugin's constructor is called, so if you need these, make sure to wait to access those from your hooks.
https://www.serverless.com/framework/docs/providers/aws/guide/plugins#serverless-instance

This PR fixes this by executing the plugin on a hook as described in the docs.

@Luke-Davies
Copy link
Copy Markdown
Author

@concon121 Any chance this could be looked at and merged in?

If you are looking for help maintaining this project then I would be interested in taking it on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant