AWS Elastic Beanstalk环境变量不会自动从配置文件中更新,但可以通过添加自定义脚本来实现。
以下是一种解决方法的示例代码:
创建一个名为.ebextensions
的文件夹在您的应用程序根目录中。
在.ebextensions
文件夹中创建一个名为01_update_env.config
的文件,并将以下内容添加到文件中:
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_update_env.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
# Change directory to the application root directory
cd /var/app/current
# Source the environment variables from the config file
.env/bin/activate
source .env
# Restart the application server to apply the updated environment variables
supervisorctl -c /opt/python/etc/supervisord.conf restart :
将上述代码中的
替换为您的应用程序名称。
将您的配置文件(例如.env
)添加到您的应用程序代码中,确保它位于应用程序根目录中。
将您的应用程序和.ebextensions
文件夹一起打包,并将其上传到AWS Elastic Beanstalk。
这样,每当您部署新版本时,Elastic Beanstalk将在部署后执行脚本99_update_env.sh
,该脚本将更新环境变量并重新启动应用程序服务器以应用更改。