如何修复 AWS Elastic Beanstalk 部署的 Rails 5.2 应用程序捆绑程序问题

Posted

技术标签:

【中文标题】如何修复 AWS Elastic Beanstalk 部署的 Rails 5.2 应用程序捆绑程序问题【英文标题】:How to fix Rails 5.2 application bundler issue for AWS Elastic Beanstalk deployment 【发布时间】:2019-09-01 06:59:20 【问题描述】:

我正在尝试将 Rails 5.2 应用程序部署到 AWS Elastic Beanstalk,但出现此错误。

Creating application version archive "app-c6b5-190405_225348". Uploading example-rails52/app-c6b5-190405_225348.zip to S3. This may take a while. Upload Complete. Environment details for: example-eb-rails52-env Application name: example-rails52 Region: us-east-2 Deployed Version: app-c6b5-190405_225348 Environment ID: e-ehbkcfzrrt Platform: arn:aws:elasticbeanstalk:us-east-2::platform/Puma with Ruby 2.5 running on 64bit Amazon Linux/2.9.2 Tier: WebServer-Standard-1.0 CNAME: UNKNOWN Updated: 2019-04-05 16:53:59.311000+00:00 Printing Status: INFO: createEnvironment is starting. INFO: Using elasticbeanstalk-us-east-2-525254074082 as Amazon S3 storage bucket for environment data. INFO: Created security group named: sg-073de6677d8e02d77 INFO: Created load balancer named: awseb-e-e-AWSEBLoa-985TNPDWPYRI INFO: Created security group named: awseb-e-ehbkcfzrrt-stack-AWSEBSecurityGroup-1XWZBQC4TD76I INFO: Created Auto Scaling launch configuration named: awseb-e-ehbkcfzrrt-stack-AWSEBAutoScalingLaunchConfiguration-SAZWMJ3L0UV7 INFO: Created Auto Scaling group named: awseb-e-ehbkcfzrrt-stack-AWSEBAutoScalingGroup-1E8GGM4K5S26 INFO: Waiting for EC2 instances to launch. This may take a few minutes. INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-east-2:525254074082:scalingPolicy:bb866da3-dac2-47f0-81a2-ebc1c582d68a:autoScalingGroupName/awseb-e-ehbkcfzrrt-stack-AWSEBAutoScalingGroup-1E8GGM4K5S26:policyName/awseb-e-ehbkcfzrrt-stack-AWSEBAutoScalingScaleUpPolicy-ON6YTENHUIOE INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-east-2:525254074082:scalingPolicy:f13cedac-d71f-4b89-a0fd-dd2db97bb43a:autoScalingGroupName/awseb-e-ehbkcfzrrt-stack-AWSEBAutoScalingGroup-1E8GGM4K5S26:policyName/awseb-e-ehbkcfzrrt-stack-AWSEBAutoScalingScaleDownPolicy-3MK7B2H0O52F INFO: Created CloudWatch alarm named: awseb-e-ehbkcfzrrt-stack-AWSEBCloudwatchAlarmHigh-1R89A12TU33EK INFO: Created CloudWatch alarm named: awseb-e-ehbkcfzrrt-stack-AWSEBCloudwatchAlarmLow-1KHMKXF2A73T1 ERROR: [Instance: i-050185633c1ecb322] Command failed on instance. Return code: 1 Output: (TRUNCATED)...:infind_spec_for_exe': 找不到带有可执行包 (Gem::GemNotFoundException) 的 gem bundler (>= 0.a) 来自 /opt/rubies/ruby-2.5.5/lib/ruby/site_ruby/2.5.0/rubygems.rb:308:in activate_bin_path' from /opt/rubies/ruby-2.5.5/bin/bundle:23:in'。 挂钩 /opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh 失败。有关更多详细信息,请使用控制台或 EB CLI 检查 /var/log/eb-activity.log。 INFO:在所有实例上完成命令执行。摘要:[成功:0,失败:1]。`

谁能给我解决方案?

【问题讨论】:

【参考方案1】:

在 Rails 5.2 中,您必须安装 bundler 2.0.1 版本。要解决此问题,您可以按照以下步骤操作 -

第 1 步。您已添加 eb 扩展,用于将 bundler 2.0.1 版本安装到 AWS Elastic beanstalk -

# Go to your project root directory
$ mkdir .ebextensions
$ vim .ebextensions/bundler_install.config 

  files:
    # Runs before `./10_bundle_install.sh`:
    "/opt/elasticbeanstalk/hooks/appdeploy/pre/09_gem_install_bundler.sh" :
      mode: "000775"
      owner: root
      group: root
      content: |
        #! /bin/bash

        EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
        EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
        # Source the application's ruby, i.e. 2.6. Otherwise it will be 2.3, which will give this error: `bundler requires Ruby version >= 2.3.0`
        . $EB_SCRIPT_DIR/use-app-ruby.sh

        cd $EB_APP_STAGING_DIR
        echo "Installing compatible bundler"
        gem install bundler -v 2.0.1

步骤 #2。转到 AWS Elastic beanstalk All applications -> example-rails52 -> example-eb-rails52-env 然后单击 Configuration -> Software -> Modify 然后添加此 env 变量

 BUNDLER_VERSION=2.0.1

然后点击保存

第 3 步。再次部署应用

$ eb deploy

希望它应该工作。

【讨论】:

可怕。我没有机会把它放进钩子里。必须有更好的解决方案吗?我想是容器。 @DamienRoche 我更新了答案。请再次检查。我希望它会起作用。

以上是关于如何修复 AWS Elastic Beanstalk 部署的 Rails 5.2 应用程序捆绑程序问题的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Elastic beanstalk 和 Dockerrun.aws.json 正确部署到 AWS?

如何在运行 AWS Linux 2 的 AWS Elastic Beanstalk 上配置 Linux 交换空间?

如何在 AWS Elastic Beanstalk 上设置 HTTPS

如何在 AWS Elastic Beanstalk 上设置 HTTPS

如何获取命令以从 Elastic Beanstalk 上的 Dockerfile.aws.json 运行?

如何在 aws elastic beanstalk 环境实例启动上运行 shell 脚本