You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elastic Beanstalk Ruby应用在私有子网部署失败求助

Ruby应用部署Elastic Beanstalk私有子网失败:rake加载错误

将Ruby应用部署到Elastic Beanstalk时,连接公有子网可成功运行,但连接私有子网时部署失败,报错如下:

2023/01/04 21:16:25.937590 [INFO] Running command /bin/su webapp -c bundle exec /opt/elasticbeanstalk/config/private/checkforraketask.rb assets:precompile
2023/01/04 21:16:26.178594 [INFO] bundler: failed to load command: /opt/elasticbeanstalk/config/private/checkforraketask.rb (/opt/elasticbeanstalk/config/private/checkforraketask.rb)
/opt/elasticbeanstalk/config/private/checkforraketask.rb:16:in `require': cannot load such file -- rake (LoadError)
    from /opt/elasticbeanstalk/config/private/checkforraketask.rb:16:in `<top (required)>'
    from /opt/rubies/ruby-2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli/exec.rb:58:in `load'
    from /opt/rubies/ruby-2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli/exec.rb:58:in `kernel_load'
    from /opt/rubies/ruby-2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli/exec.rb:23:in `run'
    from /opt/rubies/ruby-2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli.rb:491:in `exec'
    from /opt/rubies/ruby-2.7.7/lib/ruby/site_ruby/2.7.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
    from /opt/rubies/ruby-2.7.7/lib/ruby/site_ruby/2.7.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
    from /opt/rubies/ruby-2.7.7/lib/ruby/site_ruby/2.7.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
    from /opt/rubies/ruby-2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli.rb:34:in `dispatch'
    from /opt/rubies/ruby-2.7.7/lib/ruby/site_ruby/2.7.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
    from /opt/rubies/ruby-2.7.7/lib/ruby/site_ruby/2.7.0/bundler/cli.rb:28:in `start'
    from /opt/rubies/ruby-2.7.7/lib/ruby/gems/2.7.0/gems/bundler-2.4.1/exe/bundle:45:in `block in <top (required)>'
    from /opt/rubies/ruby-2.7.7/lib/ruby/site_ruby/2.7.0/bundler/friendly_errors.rb:117:in `with_friendly_errors'
    from /opt/rubies/ruby-2.7.7/lib/ruby/gems/2.7.0/gems/bundler-2.4.1/exe/bundle:33:in `<top (required)>'
    from /opt/elasticbeanstalk/.rbenv/versions/2.7.7/bin/bundle:25:in `load'
    from /opt/elasticbeanstalk/.rbenv/versions/2.7.7/bin/bundle:25:in `<main>'

最初因VPC配置错误导致部分子网无出站互联网访问,修正后公有子网恢复正常,但私有子网仍出现上述rake加载错误。已在私有子网单独启动EC2实例验证出站访问正常,且环境配置与官方VPC模板一致,但问题未解决。


排查与解决方案

  • 检查Beanstalk实例IAM角色权限
    确保Elastic Beanstalk实例的IAM角色拥有访问RubyGems的权限,包括允许s3:GetObject访问AWS托管的RubyGems镜像存储桶,以及VPC相关的基础权限(如描述子网、路由表等)。

  • 强制重新安装依赖包
    私有子网实例可能因缓存或网络波动导致依赖未完整安装,可通过.ebextensions配置强制重新执行bundle安装:

    1. 在项目根目录创建.ebextensions/01_bundle_install.config文件:
      container_commands:
        01_reinstall_gems:
          command: "bundle install --deployment --without development test"
          ignoreErrors: false
      
    2. 确认Gemfile中明确声明rake依赖,且版本与Ruby 2.7.7兼容。
  • 验证私有子网路由表与NAT网关关联
    检查私有子网绑定的路由表是否包含指向NAT网关的默认路由(0.0.0.0/0 -> NAT网关ID),同时确认NAT网关所在的公有子网有正确的互联网网关路由,且NAT网关状态正常。

  • 检查环境变量配置
    在Elastic Beanstalk控制台的「配置」->「软件」中,确认BUNDLE_PATH、RAILS_ENV等环境变量配置正确,避免因路径或环境上下文问题导致bundler无法定位rake gem。

  • 清理部署缓存
    Elastic Beanstalk可能留存了之前失败部署的缓存状态,可通过以下步骤重置:

    1. 进入环境的「版本」页面,删除所有旧部署版本
    2. 重新上传并部署最新代码包

内容的提问来源于stack exchange,提问作者Peter Karman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 20:40:18