Serverless: Serverless.yml์—์„œ ์ฐธ์กฐ ๋œ ๋ณ€์ˆ˜๊ฐ€`[Object object]`๋กœ ์ž˜๋ชป ๋ Œ๋”๋ง ๋จ

์— ๋งŒ๋“  2016๋…„ 12์›” 21์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: serverless/serverless

(๋ฒ„๊ทธ ๋ฆฌํฌํŠธ / ๊ธฐ๋Šฅ ์ œ์•ˆ)์ž…๋‹ˆ๋‹ค.

๊ธฐ์ˆ 

serverless.yml ํŒŒ์ผ์—์„œ site_config: ${file(./config/${opt:stage}/site.yml)} ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋‚˜์ค‘์— ์„œ๋ฒ„๋ฆฌ์Šค ํŒŒ์ผ (์˜ˆ : site_secret_key: ${self:site_config.site_secret_key} )์—์„œ ์ฐธ์กฐ๋˜๋Š” ๋ณ€์ˆ˜๊ฐ€ process.env.site_secret_key ์—์„œ [Object object] ./config/${opt:stage}/site.yml ํŒŒ์ผ์˜ site_secret_key ๊ฐ’๊ณผ ์—ฐ๊ด€๋œ ๋ฌธ์ž์—ด ๊ฐ’์ด์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
${file(./config/${opt:stage}/site.yml):site_secret_key}

์ถ”๊ฐ€ ์ž๋ฃŒ

  • ์‚ฌ์šฉ์ค‘์ธ ์„œ๋ฒ„๋ฆฌ์Šค ํ”„๋ ˆ์ž„ ์›Œํฌ ๋ฒ„์ „ : 1.3.0
  • ์šด์˜ ์ฒด์ œ : OSX 10.12.1
  • ์Šคํƒ ์ถ”์  :
  • ์ œ๊ณต์ž ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ :

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋ฌธ์ œ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋‚ด ๊ตฌ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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

serverless parent prop์—†์ด serverless.service ๋คํ”„ํ•˜๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

% 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: {} }

๊ทธ ๊ฒฐ๊ณผ์˜ ์›์ธ์ด ๋ฌด์—‡์ธ์ง€ ํ™•์‹คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค!

์ฐธ์กฐ ์šฉ ํ”Œ๋Ÿฌ๊ทธ์ธ ์†Œ์Šค :

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;

๋ชจ๋“  3 ๋Œ“๊ธ€

๋ฌธ์ œ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋‚ด ๊ตฌ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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

serverless parent prop์—†์ด serverless.service ๋คํ”„ํ•˜๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

% 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: {} }

๊ทธ ๊ฒฐ๊ณผ์˜ ์›์ธ์ด ๋ฌด์—‡์ธ์ง€ ํ™•์‹คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค!

์ฐธ์กฐ ์šฉ ํ”Œ๋Ÿฌ๊ทธ์ธ ์†Œ์Šค :

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 ๋ฌธ์ œ๊ฐ€ ๋ณ€์ˆ˜ ์ฐธ์กฐ์— ๋Œ€ํ•ด ์ •์˜๋˜์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” config , configEnv & configEnvProb ์†์„ฑ์ด ํ•จ์ˆ˜ ๋ฐ ๋ฆฌ์†Œ์Šค ๋ฐ”๋กœ ์˜†์— serverless.yml ์˜ ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ชจ๋กœ ์ •์˜๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. . ์ •ํ™•ํžˆ ๊ธฐ์–ตํ•œ๋‹ค๋ฉด serverless.yml ์‚ฌ์šฉ์ž ์ง€์ • ์†์„ฑ์— ์ œํ•œ์„ ๋‘์–ด custom ์†์„ฑ ๋‚ด์—์„œ๋งŒ๋กœ๋“œ๋˜๋„๋กํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ์ž ์ง€์ • ์†์„ฑ์„ custom ๊ฐœ์ฒด๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

custom:
  config:
  configEnv:
  ...

๊ทธ๋ฆฌ๊ณ  ์ฐธ์กฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋งˆ์‹ญ์‹œ์˜ค

@eahefnawy ๊ฐ€ ์œ„์—์„œ ์ œ์•ˆํ•œ๋Œ€๋กœ ๋ณ€์ˆ˜๋ฅผ custom ์ค‘์ฒฉํ•ด์•ผํ•˜๋ฏ€๋กœ ์ข…๋ฃŒ

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰