在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}
这是我的配置演示问题:
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.service
而没有serverless
父项:
% 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:
...
并且不要忘记更新您的参考
由于变量需要嵌套在custom
如@eahefnawy上面的建议suggested,因此关闭
最有用的评论
这是我的配置演示问题:
我编写了一个插件来转储
serverless.service
而没有serverless
父项:不太确定会导致该输出的原因!
插件来源供参考: