简体   繁体   English

如何在测试中获取 serverless.yml 中定义的环境变量

[英]How to get environment variables defined in serverless.yml in tests

I am using the serverless framework for running lambda functions on AWS.我正在使用无服务器框架在 AWS 上运行 lambda 函数。

In my serverless.yml there are environment variables that are fetched from SSM.在我的serverless.yml中有从 SSM 获取的环境变量。

When I write integration tests for the code, I need the code to have the environment variables and I can't find a good way to do this.当我为代码编写集成测试时,我需要代码具有环境变量,但我找不到执行此操作的好方法。

I don't want to duplicate all the variables definitions just for the tests, they are already defined in the serverless.yml.我不想仅为测试复制所有变量定义,它们已经在 serverless.yml 中定义。 Also, some are secrets and I can't commit them to source conrol, so I would have to also repeat them in the ci environment.另外,有些是机密,我无法将它们提交给源代码控制,因此我还必须在 ci 环境中重复它们。

Tried using the serverless-jest-plugin but it is not working and not well maintained.尝试使用serverless-jest-plugin但它不工作且维护不善。

Ideas I had for solutions:我对解决方案的想法:

  1. Make the tests exec sls invoke - this will work but would mean that the code cannot be debugged, I won't know the test coverage, and it will be slow.使测试 exec sls invoke - 这会起作用,但意味着无法调试代码,我不知道测试覆盖率,而且速度会很慢。
  2. Parse the serverless.yml myself and export the env variables - possible but rewriting the logic of pulling the SSM variables just for tests seems wrong.自己解析serverless.yml并导出 env 变量 - 可能但重写为测试拉动 SSM 变量的逻辑似乎是错误的。

Any ideas?有任何想法吗?

The solution we ended up using is a serverless plugin called serverless-export-env . 我们最终使用的解决方案是一个名为serverless-export-env的无服务器插件。

After adding this plugin you can run serverless export-env to export all the resolved environment variables to an .env file. 添加此插件后,您可以运行serverless export-env将所有已解析的环境变量导出到.env文件。 This resolves ssm parameters correctly and made integration testing much simpler for us. 这正确地解决了ssm参数,并使集成测试对我们来说更加简单。

BTW, to get the environment variables set from the .env file use the the dotenv npm package. 顺便说一句,要从.env文件中获取环境变量, .env使用dotenv npm包。

Credit to grishezz for finding the solution 感谢grishezz寻找解决方案

Are you looking to do mocked unit tests, or something more like integration tests? 您是否希望进行模拟单元测试,或更像集成测试?

In the first case, you don't need real values for the environment variables. 在第一种情况下,您不需要环境变量的实际值。 Mock your database, or whatever requires environment variables set. 模拟您的数据库,或任何需要设置的环境变量。 This is actually the preferable way because the tests will run super quickly with proper mocks. 这实际上是更好的方法,因为测试将通过适当的模拟快速运行。

If you are actually looking to go with end-to-end/integration kind of approach, then you would do something like sls invoke , but from jest using javascript. 如果你真的想要采用端到端/集成的方法,那么你会做一些类似于sls invoke东西,但是从jest使用javascript。 So, like regular network calls to your deployed api. 因此,像对已部署的api的常规网络调用一样。

Also, I would recommend not to store keys in serverless.yml . 另外,我建议不要将密钥存储在serverless.yml Try the secret: ${env:MY_SECRET} syntax instead ( https://serverless.com/framework/docs/providers/aws/guide/variables#referencing-environment-variables ), and use environment variables instead. 请尝试使用secret: ${env:MY_SECRET}语法( https://serverless.com/framework/docs/providers/aws/guide/variables#referencing-environment-variables ),然后使用环境变量。 If you have a ci/cd build server, you can store your secrets there. 如果你有一个ci / cd构建服务器,你可以在那里存储你的秘密。

You can run node with --require option to inject .env file to a serverless command. 您可以使用--require选项运行node以将.env文件注入无服务器命令。

  1. Create .env at the project root with package.json , and list variables in .env . 使用package.json在项目根目录下创建.env ,并在.env列出变量。
  2. Install serverless and dotenv in the project by yarn add -D serverless dotenv . 通过yarn add -D serverless dotenv dotenv在项目中安装无服务器和yarn add -D serverless dotenv
  3. Run a command like node -r dotenv/config ./node_modules/.bin/sls invoke . 运行node -r dotenv/config ./node_modules/.bin/sls invoke等命令。

Then, you can get environment variables in the handler process.env.XXX . 然后,您可以在处理程序process.env.XXX获取环境变量。

After searching I did my custom solution搜索后我做了我的自定义解决方案

import * as data from './secrets.[stage].json'

if( process.env.NODE_ENV === 'test'){
  process.env = Object.assign( data, process.env );
}
//'data' is the object that has the Serverless environment variables 

The SLS environment variables in my case at the file secrets.[stage].json在我的案例中,SLS 环境变量位于文件secrets.[stage].json

Serverless.yml has Serverless.yml 有

custom:
 secrets: ${file(secrets.[stage].json)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查serverless.yml文件中的env变量(无服务器框架) - Check env variables in serverless.yml file (Serverless Framework) 如何摆脱 serverless.yml 中的“警告:遇到无效配置”? - How can I get rid of the "Warning: Invalid configuration encountered" in serverless.yml? 如何从我的 Lambda NodeJS 中的 serverless.yml 获取服务名称和函数名称? - How can I get service-name and function-name from serverless.yml in my Lambda NodeJS? 启用 aws-xray serverless.yml - enabling aws-xray serverless.yml 在 serverless.yml 中定义可选的查询参数 - Define optional query parameters in serverless.yml 在serverless.yml中创建两个dynamoDB表 - Creating two dynamoDB tables in serverless.yml 如何从serverless.yml文件中提取REST API ID和资源ID? - How do I extract the REST API ID and Resource ID from a serverless.yml file? 如何为多运行时AWS Lambda服务设置serverless.yml和webpack.config - How to setup serverless.yml and webpack.config for a multiple-runtime AWS Lambda service 如何整理我的 serverless.yml 文件? - How do I de-clutter my serverless.yml file? 如何配置 serverless.yml 文件以在我收到 sqs 中的消息后立即触发 lambda - How to configure serverless.yml file to trigger lambda as soon as i recieving messages in sqs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM