concon121 / serverless-plugin-aws-resource-names

Serverless plugin to enable custom AWS resource names
MIT License
5 stars 17 forks source link

fix: run plugin after all variables resolved #10

Open Luke-Davies opened 3 years ago

Luke-Davies commented 3 years ago

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 commented 3 years ago

@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.