[英]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:我对解决方案的想法:
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.sls invoke
- 这会起作用,但意味着无法调试代码,我不知道测试覆盖率,而且速度会很慢。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
文件注入无服务器命令。
.env
at the project root with package.json
, and list variables in .env
. package.json
在项目根目录下创建.env
,并在.env
列出变量。 yarn add -D serverless dotenv
. yarn add -D serverless dotenv
dotenv在项目中安装无服务器和yarn add -D serverless dotenv
。 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.