简体   繁体   English

通过无服务器离线在本地运行时加载环境变量

[英]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.

相关问题 无服务器部署的环境变量不更新 - Serverless deployed environment variables do not update 从 lambda 层加载时无服务器离线未定义模块 - Serverless Offline undefined module when loaded from lambda layer 作为服务运行 Docker - 环境变量 - Running Docker as a Service - Environment Variables terraform GCP 云 function 无需通过 terraform 在 CI 中部署或在本地运行时打破过去的部署? - terraform GCP cloud function without having to deploy via terraform in CI or breaking past deployments when running locally? 本地运行lambda时知道 - Know when running lambda locally Sequelize 无服务器离线迁移错误 - Error with Sequelize Serverless Offline Migrations docker 上的 Serverless offline 和 DynamoDB local - Serverless offline and DynamoDB local on docker AWS - 无服务器离线框架上可用的 API 个密钥? - AWS - API keys available on the Serverless Offline framework? 在本地测试 Cloud Functions 时 Cloud Firestore 模拟器未运行 - Cloud Firestore emulator not running when testing Cloud Functions locally 如何在本地使用 AWS 无服务器后端 - How to use AWS serverless backend locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM