serverless.yml file
provider:
name: aws
runtime: nodejs12.x
memorySize: 512
stage: ${opt:stage, 'test'}
timeout: 30
##
##...
custom:
getValue: ${file(key.js):randomVal} //pass the string from here
key.js file
module.exports.randomVal = async (context) => {
#code //get the string here
console.log(context.providers);
};
在上面的代码中,我从无服务器 yml 文件调用 randomVal() 函数,我想从 yml 文件将字符串传递给该函数。 有什么办法可以实现吗?
最佳答案
我的建议是使用无服务器环境变量。
在您的 serverless.yml
文件中,provider
下有一个 environment
部分。然后,这些值将作为进程环境变量填充到所有 lambda 中。
例如,在 serverless.yml
中包含此部分:
provider:
name: aws
environment:
SOME_USEFUL_VALUE: 'some_val'
然后在您的 typescript 文件中,您可以将此值用作
const THE_USEFUL_VALUE = process.env.SOME_USEFUL_VALUE
关于javascript - 如何将值从无服务器 yml 文件传递到 js 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67934877/