[英]Load environment variables when running locally via serverless offline
I want to load env variables from.env file, when running locally, So here's my serverless.yaml file,我想在本地运行时从 .env 文件加载环境变量,所以这是我的 serverless.yaml 文件,
functions:
health:
handler: src/api/health.check
name: ${self:provider.stackName}-health
environment:
USER_POOL_APP_CLIENT_ID: !Ref UserPoolClient
You see, it sets a userpool id that gets created in the resources section as an environment variable to a lambda. This works perfectly fine, when deployed as expected.你看,它将在资源部分创建的用户池 ID 作为环境变量设置为 lambda。按预期部署时,这工作得很好。
However, when I try to run it locally via serverless-offline, no matter how I set the env variables, via dotenv or manually, it seems to get overriden by serverless, in this case all I see is "[object object]".但是,当我尝试通过 serverless-offline 在本地运行它时,无论我如何通过 dotenv 或手动设置 env 变量,它似乎都被 serverless 覆盖了,在这种情况下,我看到的只是“[object object]”。
Other workflows that I see, load all env variables from files, like below我看到的其他工作流程,从文件加载所有环境变量,如下所示
functions:
health:
handler: src/api/health.check
name: ${self:provider.stackName}-health
environment:
USER_POOL_APP_CLIENT_ID: {env:USER_POOL_APP_CLIENT_ID}
But wouldn't this require us to have variables of all stages, stored locally as files?但这是否需要我们将所有阶段的变量都存储在本地作为文件?
I was hoping to store only the dev version, locally, and have all the remaining fetched from the serverless file itself, automatically via !Ref
like shown at the beginning.我希望只在本地存储开发版本,并通过
!Ref
自动从无服务器文件本身获取所有剩余版本,如开头所示。
So, how do I prevent, serverless from populating/polluting my env variables when I run locally, while sticking to the first format?那么,当我在本地运行时,如何防止无服务器填充/污染我的环境变量,同时坚持第一种格式?
Or are there other better ways to handle this?或者还有其他更好的方法来处理这个问题吗?
It happened here with the new version of serverless-offline (v12.0.4).它发生在新版本的无服务器离线(v12.0.4)中。
My solution was to use: https://www.serverless.com/plugins/serverless-plugin-ifelse我的解决方案是使用: https://www.serverless.com/plugins/serverless-plugin-ifelse
See the example below:请参见下面的示例:
serverlessIfElse:
- If: '"${env:NODE_ENV}" == "development"'
Set:
functions.health.environment.USER_POOL_APP_CLIENT_ID: "${USER_POOL_APP_CLIENT_ID, ''}"
You can change it for your use case.您可以根据您的用例更改它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.