简体   繁体   English

使用 CodeDeploy、Parameter Store 和 PM2 设置 EC2 环境变量

[英]Setting EC2 Environment Variables with CodeDeploy, Parameter Store and PM2

I am deploying a Node.js app to EC2 using CodeDeploy.我正在使用 CodeDeploy 将 Node.js 应用程序部署到 EC2。 I am storing credentials within AWS Systems Manager, Parameter Store however cannot find a method to expose these to my application.我将凭证存储在 AWS Systems Manager 和 Parameter Store 中,但是找不到将这些凭证公开给我的应用程序的方法。

I am using PM2 for process management.我正在使用 PM2 进行流程管理。 I can successfully retrieve the parameter from the Parameter Store on the target machine, so there are no permission issues.我可以成功地从目标机器上的 Parameter Store 中检索参数,所以没有权限问题。 For example:例如:

aws ssm get-parameters --region us-east-1 --names LOCAL_CACHE_PATH --with-decryption --query Parameters[0].Value`

...successfully returns the correct string. ...成功返回正确的字符串。 I attempt to use this in my applicationStart.sh CodeDeploy file and start the app:我尝试在我的applicationStart.sh CodeDeploy 文件中使用它并启动应用程序:

#!/bin/bash
export LOCAL_CACHE_PATH=$(aws ssm get-parameters --region us-east-1 --names LOCAL_CACHE_PATH --with-decryption --query Parameters[0].Value)

pm2 start ecosystem.config.js --env production

LOCAL_CACHE_PATH returns undefined in my app when accessing process.env.LOCAL_CACHE_PATH .访问process.env.LOCAL_CACHE_PATH时, LOCAL_CACHE_PATH在我的应用程序中返回undefined

So the environment variable is available within the applicationStart.sh script and yet undefined when the app starts from that script.因此,环境变量在applicationStart.sh脚本中可用,但在应用程序从该脚本启动时未定义。

I am looking for a recommended approach to use environment variables from the Parameter Store with CodeDeploy.我正在寻找一种推荐的方法来将 Parameter Store 中的环境变量与 CodeDeploy 一起使用。

I have read literally dozens of posts on similar topics but cannot resolve it.我已经阅读了数十篇关于类似主题的帖子,但无法解决。 Very much appreciate any guidance.非常感谢任何指导。

The solution I am using is to write the environment variables to a.env file and use that in my app:我正在使用的解决方案是将环境变量写入 a.env 文件并在我的应用程序中使用它:

afterInstall.sh : afterInstall.sh

echo LOCAL_CACHE_PATH=$(aws ssm get-parameters --output text --region us-east-1 --names LOCAL_CACHE_PATH --with-decryption --query Parameters[0].Value) >> /home/ubuntu/foo/.env

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM