#mynavi(AWSメモ) #setlinebreak(on); * 目次 [#jf8eb443] #contents - 参考 -- [[https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/eb-cli3.html]] -- [[https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/create-deploy-python-flask.html#python-flask-setup-venv]] - 関連 * 概要 [#gbb526bd] #html(<div style="padding-left: 10px;">) ここでは Elastic Beanstalk のコマンドラインインターフェースである EB CLI を使用してアプリケーションのデプロイ 及び 手動でのスケールを行う。 尚、当記事では Auto Scaling や ELB、Cloudformatrion については記載しない。(別記事にて記載予定) #html(</div>) * EB CLIのインストール [#h6056e25] #html(<div style="padding-left: 10px;">) 参考 https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/eb-cli3-install.html EB CLIインストール #myterm2(){{ pip install awsebcli --upgrade --user }} PATHに追加 #myterm2(){{ echo PATH=~/Library/Python/3.X/bin:\$PATH>>~/.bash_profile source ~/.bash_profile }} #html(</div>) * プロジェクトフォルダ作成 [#o59d6264] #html(<div style="padding-left: 10px;">) #myterm2(){{ mkdir SampleApp1 cd SampleApp1 }} #html(</div>) * デプロイするアプリケーションの作成 [#hf7a8575] #html(<div style="padding-left: 10px;">) ** 仮想環境作成 [#n6662cf1] #myterm2(){{ python3 -m venv venv source ./venv/bin/activate }} ** flaskインストール [#maca2aa9] #myterm2(){{ pip install flask }} ** パッケージ一括インストール用に requirements.txt を作成 [#jdc5e0de] #myterm2(){{ pip freeze >requirements.txt }} ** サンプルアプリケーション作成 [#i950ac0e] application.py #mycode2(){{ from flask import Flask import datetime # wsgi で動作するので 変数名を application にしておく。(起動用のファイルを別で作成しても良いかも) application = Flask(__name__) @application.route('/') def index(): now = datetime.datetime.now().strftime('%H:%M:%S.%f') return f'<h1>top page!({now})</h1>\n' @application.route('/sample') def sample(): now = datetime.datetime.now().strftime('%H:%M:%S.%f') return f'<h1>sample page!({now})</h1>\n' if __name__ == '__main__': application.run(debug=True) }} 動作確認用にflask起動 #myterm2(){{ python application.py * Serving Flask app "app" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: on * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 158-691-400 }} 別ターミナルから動作確認 #myterm2(){{ curl http://127.0.0.1:5000/ <h1>top page!(20:02:44.414536)</h1> }} ※問題なければ、起動したflaskを CTRL+C で終了。 #html(</div>) * デプロイ [#d3fb37a4] #html(<div style="padding-left: 10px;">) ** EB CLI でプロジェクトを初期化 [#b958de2a] ※以下は、「eb init -p python-3.6 SampleApp1 --region ap-northeast-1」と同義。 #myterm2(){{ eb init Select a default region 1) us-east-1 : US East (N. Virginia) 2) us-west-1 : US West (N. California) 3) us-west-2 : US West (Oregon) 4) eu-west-1 : EU (Ireland) 5) eu-central-1 : EU (Frankfurt) 6) ap-south-1 : Asia Pacific (Mumbai) 7) ap-southeast-1 : Asia Pacific (Singapore) 8) ap-southeast-2 : Asia Pacific (Sydney) 9) ap-northeast-1 : Asia Pacific (Tokyo) 10) ap-northeast-2 : Asia Pacific (Seoul) 11) sa-east-1 : South America (Sao Paulo) 12) cn-north-1 : China (Beijing) 13) cn-northwest-1 : China (Ningxia) 14) us-east-2 : US East (Ohio) 15) ca-central-1 : Canada (Central) 16) eu-west-2 : EU (London) 17) eu-west-3 : EU (Paris) 18) eu-north-1 : EU (Stockholm) (default is 3): 9 Enter Application Name (default is "SampleApp1"): Application SampleApp1 has been created. Select a platform. 1) Node.js 2) PHP 3) Python 4) Ruby 5) Tomcat 6) IIS 7) Docker 8) Multi-container Docker 9) GlassFish 10) Go 11) Java 12) Packer (default is 1): 3 Select a platform version. 1) Python 3.6 2) Python 3.4 3) Python 3.4 (Preconfigured - Docker) 4) Python 2.7 5) Python (default is 1): 1 Cannot setup CodeCommit because there is no Source Control setup, continuing with initialization Do you want to set up SSH for your instances? (Y/n): n }} ** venvをデプロイ対象から外す [#ne21813e] #myterm2(){{ echo venv>>.ebignore }} ** SSH鍵の設定 [#s441ea82] #myterm2(){{ eb init Do you want to set up SSH for your instances? (y/n): y Select a keypair. 1) my-keypair 2) [ Create new KeyPair ] }} ** デプロイ&起動 [#f47b4ff3] #myterm2(){{ eb create eb-sample-app1 }} ** 起動確認 [#y5cbb71c] #myterm2(){{ eb open eb-sample-app1 }} ** スケール [#r883af92] #html(<div style="padding-left: 10px;">) インスタンス数を2に増やす #myterm2(){{ eb scale 2 eb-sample-app1 }} 増えたか確認 #myterm2(){{ aws ec2 describe-instances --filter "Name=tag:Name,Values=eb-sample-app1" "Name=instance-state-name,Values=running" --output text | grep INSTANCES | wc -l 2 }} インスタンス数を1に減らす #myterm2(){{ eb scale 1 eb-sample-app1 }} 減ったか確認 #myterm2(){{ aws ec2 describe-instances --filter "Name=tag:Name,Values=eb-sample-app1" "Name=instance-state-name,Values=running" --output text | grep INSTANCES | wc -l 1 }} #html(</div>) ** 終了 [#uc72e2d4] #myterm2(){{ eb terminate eb-sample-app1 }} #html(</div>)