hayashier Tech Blogs
  • hayashier Tech Blogs
  • Author's Books
    • 実践Redis入門 (日本語版)
    • 実践Redis入門 (한국어판)
  • Top Contents
    • Dive Deep Redis
    • Dive Deep Memcached
    • Kubernetes 入門
    • TCP 入門
    • TLS 入門
    • GPG 入門
    • サービス障害が発生した場合の対応方法
    • よく使うトラブルシューティング コマンド実行例 まとめ
    • コピペで使えるELBのアクセスログ解析による事象分析 (ShellScript, Athena)
  • Containers
    • Docker 入門
    • Nomad 導入
    • Dockerを利用してさっと検証環境構築
  • Kubernetes
    • Kubernetes 入門
    • Kubernetes 導入 with Amazon Linux 2
    • EKSを利用してKubernetesでSpring MVCをデプロイ (NLB + Auto Scaling)
  • Load Balancer
    • ALB 認証 導入
    • TLS extensions support with ALB
    • ELB(CLB,ALB,NLB)の種類ごとのHTTPレスポンスの違い
    • ELB(CLB) で WebSocket 通信
  • RDBMS
    • PostgreSQL DBA 入門
    • RDBMS Benchmark Get Started
    • RDBMS サンプルデータ生成 Get Started
    • RDS PostgreSQL Extensions Get Started
    • RDBMS Engine Inspection for Troubleshooting
  • Redis
    • Dive Deep Redis ~ 入門から実装の確認まで
    • Dive Deep Redis Internals ~ GETコマンド実行時の動作 ~
    • RedisのString型は今でも本当に512MBが上限か?
    • Redis 公式ドキュメント まとめ
    • Redis / Memcached Source Code Reading - Overview -
  • Memcached
    • Dive Deep Memcached ~ 入門から実装の確認まで ~
    • Dive Deep Memcached ~ SETコマンド実行時の動作 ~
    • Memcached 公式ドキュメント まとめ
    • memtier_benchmark + memcached-tool の導入
    • Redis / Memcached Source Code Reading - Overview -
  • Hadoop
    • Hadoop Get Started
  • Networking
    • TCP 入門
    • TLS 入門
    • ksnctf: HTTPS is secure, Writeup (TLS 通信解読)
    • オンプレ側ルーター(Cisco 1812J, Juniper SRX210, YAMAHA RTX 1210)から Direct Connect へ BGP 設定
  • Software
    • アルゴリズムとデータ構造 入門
    • デザインパターン 入門
    • ソフトウェアテスト 入門
  • System Admin
    • Shell Script 入門
    • サービス障害が発生した場合の対応方法
    • よく使うトラブルシューティング コマンド実行例 まとめ
    • コピペで使えるELBのアクセスログ解析による事象分析 (ShellScript, Athena)
    • GPG 入門
    • Operation Misc
  • Development
    • ローカル環境のプログラミング言語のバージョンを切り替え macOS
    • /usr/local/Cellar/pyenv/1.2.21/libexec/pyenv: No such file or directoryのエラーの対処方法
  • AWS
    • AWS Misc
    • AWS CLI, AWS SDKのリトライ処理の実装について
    • AWS CLI バージョンアップでエラー発生を解消
    • Elastic Beanstalkで稼働しているアプリケーション(Ruby, Sinatra)をAmazon Linux AMIからAmazon Linux2へ移行
    • Elastic Beanstalkでインスタンス入れ替え後にnginxのデフォルトの画面が表示されてしまう問題の対応
    • Amazon Lightsail に SSL 証明書設置 with Let's Encrypt (自動更新)
    • Amazon Lightsailで10分で作るお手軽Markdownで書く独自ドメインのブログサイト構築
    • Lambdaをローカルでテスト(with Docker)
    • ECS + ALB でダウンタイムなしでデプロイ
    • `Repository packages-microsoft-com-prod is listed more than once in the configuration`のメッセージの解消方法
  • Others
    • Pandoc 導入
    • textlint + prh による文章校正
    • 紙書籍をPDFに変換
    • Sphinx 導入
    • さくっとPocketのブックマークをはてなブックマークに移行
    • Macが突然起動しなくなった話
    • Macでターミナルが開かない (zsh編)
    • ホスト型 IDS Tripwire とネットワーク型 IDS Snort の導入 with CentOS 6
    • JMeter 導入
    • Squid 導入 with Amazon Linux AMI
    • Spring MVCを導入 (+ MySQL, Redis)
    • 外資系企業で働いている場合の確定申告方法 (RSU考慮)
Powered by GitBook
On this page
  • Elastic Beanstalkで稼働しているアプリケーション(Ruby, Sinatra)をAmazon Linux AMIからAmazon Linux2へ移行
  • Errors
  1. AWS

Elastic Beanstalkで稼働しているアプリケーション(Ruby, Sinatra)をAmazon Linux AMIからAmazon Linux2へ移行

Elastic Beanstalkで稼働しているアプリケーション(Ruby, Sinatra)をAmazon Linux AMIからAmazon Linux2へ移行

Systems ManagerのメンテナンスウィンドウでAWS-RunPatchBaselineというSSMドキュメントにより、インスタンスにパッチ適用を定期的に実行するようにしていました。 管理している一部のインフラ内にセキュリティチェックを導入したのですが、一部のサーバーのみセキュリティ要件に引っかかり続けている状態が発生しました。理由はOS内の各種ライブラリが最新版になっていないことによるものでした。

これはAmazon Linux AMIを使用し続けており、サポートが終了していることが理由でした。サポートされないパッケージのリンクを確認すると、たしかにいずれもDeprecated Packagesに含まれているものでした。

Amazon Linux AMI は、2020 年 12 月 31 日をもって標準サポートを終了しました。現在、メンテナンスサポート段階にあります。サポートされるパッケージとサポートされないパッケージの詳細なリストはこちらにあります。

アップデートできなかったのは、放置し続けていたため、当然といえば当然ですね。

これらのサーバーはElastic Beanstalkで管理しているものでした。PlatformとしてPuma with Ruby 2.6 running on 64bit Amazon Linuxとして元々稼働していました。これをRuby 2.7 running on 64bit Amazon Linux 2に移行することにしました。

Elastic Beanstalkの公式ドキュメントにはAmazon Linux AMIからAmazon Linux 2に移行するためのガイドが用意されています。

  • Elastic Beanstalk Linux アプリケーションを Amazon Linux 2 に移行する

本当は上記ドキュメントを先に読んでおくべきでしたが、先走って以下のように進めていきました。 Sinatraのアプリケーションを動かしていたのですが、単純にPlatformを置き換えて、アプリケーションをzipファイルとして圧縮してデプロイすると、/var/log/eb-engine.logで以下のエラーが発生しました。

2021/09/29 11:53:18.758807 [ERROR] An error occurred during execution of command [app-deploy] - [stage ruby application]. Stop running the command. Error: install dependencies in Gemfile failed with error Command /bin/sh -c bundle config set --local deployment true failed with error exit status 1. Stderr:/opt/rubies/ruby-2.7.4/lib/ruby/site_ruby/2.7.0/rubygems.rb:281:in `find_spec_for_exe': Could not find 'bundler' (1.17.2) required by your /var/app/staging/Gemfile.lock. (Gem::GemNotFoundException)To update to the latest version installed on your system, run `bundle update --bundler`.To install the missing version, run `gem install bundler:1.17.2`        from /opt/rubies/ruby-2.7.4/lib/ruby/site_ruby/2.7.0/rubygems.rb:300:in `activate_bin_path'        from /opt/elasticbeanstalk/.rbenv/versions/2.7.4/bin/bundle:23:in `<main>'

現状のBundlerのバージョンを確認すると以下のようになっていました。

$ bundle --version 
Bundler version 2.1.4

ローカルで以下を実行して新しいGemfile.lockを生成しました。

$ gem install bundler
$ bundle update --bundler
$ bundle install

アップデート後のBundlerのバージョンを確認のようになりました。

$ rbenv exec bundle --version
Bundler version 2.2.28

再度、アプリケーションをzipファイルに固めてアップロードしたところ、Nginxのデフォルトページが表示される状態になりました。

/var/log/puma/puma.logで以下のエラーが繰り返し出ている状況であることを確認しました。

[2105] ! Unable to start worker
[2105] /opt/rubies/ruby-2.7.4/lib/ruby/site_ruby/2.7.0/rubygems/core_ext/kernel_require.rb:85:in `require'
[2105] Early termination of worker
[2107] + Gemfile in context: /var/app/current/Gemfile

調べてみたところ、類似の事例に遭遇している方をこちら(Amazon Linux 2 - Ruby 2.6 - Puma: Early Termination of Workers)でお見かけし、対処方法を参考にしました。 

Gemfileに以下を追加。

gem 'puma', '4.3.8'

Procfileを作成し、以下を追記。

web: bundle exec puma -C /opt/elasticbeanstalk/config/private/pumaconf.rb 

Procfileはこちらを参照。

すると、無事一通りの動作確認もできるようになりました。 

ここでAmazon Linux 2への移行ガイドが公式ドキュメントにあることを認知し、一読しました。 諸々書いてありますが、.ebextensions/nginxディレクトリに置かれているプロキシの設定ファイルを.platform/nginxディレクトリに移動する必要があります。

そのため、.ebextensions以下にあった以下のようなすべての設定ファイルを削除しました。

  • .ebextensions/nginx/conf.d/00_elastic_beanstalk_proxy.conf

    upstream my_app {
    server unix:///var/run/puma/my_app.sock;
    }
    
    log_format healthd '$msec"$uri"'
                    '$status"$request_time"$upstream_response_time"'
                    '$http_x_forwarded_for';
  • .ebextensions/nginx/conf.d/elasticbeanstalk/01_elastic_beanstalk_server.conf

    server_name _ localhost; # need to listen to localhost for worker tier
    
    # Hide server information
    server_tokens off;
    
    # Add secure header
    add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
    add_header Content-Security-Policy "default-src 'none'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; font-src 'self';";
    
    if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
    set $year $1;
    set $month $2;
    set $day $3;
    set $hour $4;
    }
    
    access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
    
    location / {
    proxy_pass http://my_app; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    
    location /assets {
    alias /var/app/current/public/assets;
    gzip_static on;
    gzip on;
    expires max;
    add_header Cache-Control public;
    }
    
    location /public {
    alias /var/app/current/public;
    gzip_static on;
    gzip on;
    expires max;
    add_header Cache-Control public;
    }

代わりに.platform以下にあった不要な設定をこれを機に削除して、以下の設定ファイルを追加しました。

  • .platform/nginx/conf.d/sec_requirement.conf

    # Hide server information
    server_tokens off;
    
    # Add secure header
    add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
    add_header Content-Security-Policy "default-src 'none'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; font-src 'self';";

Errors

Creating load balancer named: arn:aws:elasticloadbalancing:ap-northeast-1:012345678901:loadbalancer/app/awseb-AWSEB-XXXXXXXXXXXXX/xxxxxxxxxxxxxxxx failed Reason: The value of 'access_logs.s3.prefix' cannot start or end with '/' (Service: AmazonElasticLoadBalancing; Status Code: 400; Error Code: ValidationError; Request ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; Proxy: null)

Elastic BeantalkでELB(ALB)の設定をする際に、アクセスログのパス名の先頭および最後に/を含めてはいけないにも関わらず設定されていて、Beanstalkの環境のセットアップに失敗していました。 そこで編集しようとしても、Environment named **** is in an invalid state for this operation. Must be Ready.というエラーで編集できない状態が発生しました。

そのため、環境を再作成で対応しました。

  • References

    • aws beanstalk for sinatra app - Could not find 'bundler' (2.0.2) required by your /var/app/ondeck/Gemfile.lock

    • Find _ spec for _ exe & # 39; : can & # 39; t find Gem bundler (> = 0. a) with executable bundle (Gem: : : GemNotFoundException)

    • AWS: Environment named *** is in an invalid state for this operation. Must be Ready

PreviousAWS CLI バージョンアップでエラー発生を解消NextElastic Beanstalkでインスタンス入れ替え後にnginxのデフォルトの画面が表示されてしまう問題の対応

Last updated 1 month ago