对于 Ruby 2.7:
正如有人所说,如果您不需要环境变量,请运行以下命令
BUNDLE_PATH=/var/app/current/vendor/bundle/ bundle exec rails c
但是,如果您需要 ENV,我建议按照 AWS 文档执行此操作:
https ://aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-linux2/
tl;博士
在 Amazon Linux 2 上,所有环境属性都集中到一个名为/opt/elasticbeanstalk/deployment/env
. 没有用户可以在应用程序之外访问这些内容。因此,他们建议在部署后添加一些钩子脚本以基本上创建副本。
#!/bin/bash
#Create a copy of the environment variable file.
cp /opt/elasticbeanstalk/deployment/env /opt/elasticbeanstalk/deployment/custom_env_var
#Set permissions to the custom_env_var file so this file can be accessed by any user on the instance. You can restrict permissions as per your requirements.
chmod 644 /opt/elasticbeanstalk/deployment/custom_env_var
#Remove duplicate files upon deployment.
rm -f /opt/elasticbeanstalk/deployment/*.bak
如果由于某种原因您不想以 root 身份运行,请执行以下操作以将 env vars 从 root 传递到新的用户环境:
sudo -u <user> -E env "PATH=$PATH" bash -c 'cd /var/app/current/ && <wtv you want to run>'