Serverless: Referenced variables in the Serverless.yml incorrectly rendered as `[Object object]`

Created on 21 Dec 2016  路  3Comments  路  Source: serverless/serverless

This is a (Bug Report / Feature Proposal)

Description

When using site_config: ${file(./config/${opt:stage}/site.yml)} syntax in the serverless.yml file the variables referenced later in the serverless file (ex: site_secret_key: ${self:site_config.site_secret_key}) are output in process.env.site_secret_key as [Object object] where it should have been the string value associated with the site_secret_key value in the ./config/${opt:stage}/site.yml file.

Note that using the following syntax works correctly:
${file(./config/${opt:stage}/site.yml):site_secret_key}

Additional Data

  • Serverless Framework Version you're using:1.3.0
  • Operating System:OSX 10.12.1
  • Stack Trace:
  • Provider Error messages:
bug

Most helpful comment

Here's my config demonstrating the problem:

service: test-service

plugins:
  - debug-plugin

config: ${file(config.json)}
configEnv: ${file(config.json):env}
configEnvProp: ${file(config.json):env.development}

provider:
  name: aws
  stage: development
  runtime: nodejs4.3
  environment:
    NODE_ENV: ${self:provider.stage}
    fileConfig: ${file(config.json)}
    fileConfigEnv: ${file(config.json):env}
    fileConfigEnvProp: ${file(config.json):env.development}
    selfConfig: ${self:config}
    selfConfigEnv: ${self:configEnv}
    selfConfigEnvProp: ${self:configEnvProp}
    selfConfigDotEnv: ${self:config.env}
    selfConfigDotEnvDotProp: ${self:config.env.development}

functions:
  hello:
    handler: handler.hello

I wrote a plugin to dump serverless.service without the serverless parent prop:

% sls debug config
{ service: 'test-service',
  provider:
   { name: 'aws',
     stage: 'development',
     runtime: 'nodejs4.3',
     environment:
      { NODE_ENV: 'development',
        fileConfig:
         { env:
            { development: { region: 'ap-northeast-2' },
              production: { region: 'us-west-1' } },
           assets: { bucket: 'some-bucket' } },
        fileConfigEnv:
         { development: { region: 'ap-northeast-2' },
           production: { region: 'us-west-1' } },
        fileConfigEnvProp: { region: 'ap-northeast-2' },
        selfConfig: undefined,
        selfConfigEnv: undefined,
        selfConfigEnvProp: undefined,
        selfConfigDotEnv: {},
        selfConfigDotEnvDotProp: undefined },
     region: 'us-east-1',
     variableSyntax: '\\${([ :a-zA-Z0-9._,\\-\\/\\(\\)]+?)}' },
  defaults:
   { stage: 'development',
     region: 'us-east-1',
     variableSyntax: '\\${([ :a-zA-Z0-9._,\\-\\/\\(\\)]+?)}' },
  custom: undefined,
  plugins: [ 'debug-plugin' ],
  functions:
   { hello:
      { handler: 'handler.hello',
        events: [],
        name: 'test-service-development-hello' } },
  resources: undefined,
  package: {} }

Not quite sure what could cause that output!

Plugin source for reference:

class Debug {
  constructor(serverless, options) {
    this.serverless = serverless;
    this.options = options;

    this.commands = {
      debug: {
        usage: 'Debugging',
        commands: {
          config: {
            usage: 'Show configuration value',
            lifecycleEvents: [
              'print'
            ]
          }
        }
      }
    };

    this.hooks = {
      'debug:config:print': () => this.printConfig(),
    };
  }

  printConfig() {
    const config = Object.assign({}, this.serverless.service);
    delete config.serverless;
    const output = require('util').inspect(config, { colors: true, depth: null });
    console.log(output.substring(0, 100000)); // Paranoia: can't kill node sync output on Ubuntu on Windows?
  }
}

module.exports = Debug;

All 3 comments

Here's my config demonstrating the problem:

service: test-service

plugins:
  - debug-plugin

config: ${file(config.json)}
configEnv: ${file(config.json):env}
configEnvProp: ${file(config.json):env.development}

provider:
  name: aws
  stage: development
  runtime: nodejs4.3
  environment:
    NODE_ENV: ${self:provider.stage}
    fileConfig: ${file(config.json)}
    fileConfigEnv: ${file(config.json):env}
    fileConfigEnvProp: ${file(config.json):env.development}
    selfConfig: ${self:config}
    selfConfigEnv: ${self:configEnv}
    selfConfigEnvProp: ${self:configEnvProp}
    selfConfigDotEnv: ${self:config.env}
    selfConfigDotEnvDotProp: ${self:config.env.development}

functions:
  hello:
    handler: handler.hello

I wrote a plugin to dump serverless.service without the serverless parent prop:

% sls debug config
{ service: 'test-service',
  provider:
   { name: 'aws',
     stage: 'development',
     runtime: 'nodejs4.3',
     environment:
      { NODE_ENV: 'development',
        fileConfig:
         { env:
            { development: { region: 'ap-northeast-2' },
              production: { region: 'us-west-1' } },
           assets: { bucket: 'some-bucket' } },
        fileConfigEnv:
         { development: { region: 'ap-northeast-2' },
           production: { region: 'us-west-1' } },
        fileConfigEnvProp: { region: 'ap-northeast-2' },
        selfConfig: undefined,
        selfConfigEnv: undefined,
        selfConfigEnvProp: undefined,
        selfConfigDotEnv: {},
        selfConfigDotEnvDotProp: undefined },
     region: 'us-east-1',
     variableSyntax: '\\${([ :a-zA-Z0-9._,\\-\\/\\(\\)]+?)}' },
  defaults:
   { stage: 'development',
     region: 'us-east-1',
     variableSyntax: '\\${([ :a-zA-Z0-9._,\\-\\/\\(\\)]+?)}' },
  custom: undefined,
  plugins: [ 'debug-plugin' ],
  functions:
   { hello:
      { handler: 'handler.hello',
        events: [],
        name: 'test-service-development-hello' } },
  resources: undefined,
  package: {} }

Not quite sure what could cause that output!

Plugin source for reference:

class Debug {
  constructor(serverless, options) {
    this.serverless = serverless;
    this.options = options;

    this.commands = {
      debug: {
        usage: 'Debugging',
        commands: {
          config: {
            usage: 'Show configuration value',
            lifecycleEvents: [
              'print'
            ]
          }
        }
      }
    };

    this.hooks = {
      'debug:config:print': () => this.printConfig(),
    };
  }

  printConfig() {
    const config = Object.assign({}, this.serverless.service);
    delete config.serverless;
    const output = require('util').inspect(config, { colors: true, depth: null });
    console.log(output.substring(0, 100000)); // Paranoia: can't kill node sync output on Ubuntu on Windows?
  }
}

module.exports = Debug;

@simonbuchan I'm guessing the issue is that you're getting undefined for your variable references? I think the reason is that you have config, configEnv & configEnvProb properties defined as first parents of serverless.yml right next to functions and resources...etc. If I remember correctly, we put a restriction on custom properties in serverless.yml to only be loaded inside a custom property. So could you try moving your custom properties into custom object like so:

custom:
  config:
  configEnv:
  ...

And dont forget to update your references

Closing since the variables need to be nested in custom as @eahefnawy suggested above 馃敐

Was this page helpful?
0 / 5 - 0 ratings

Related issues

taptip picture taptip  路  3Comments

gmetzker picture gmetzker  路  3Comments

chris-hailstorm picture chris-hailstorm  路  3Comments

bradgreens picture bradgreens  路  3Comments

BarakChamo picture BarakChamo  路  3Comments