目次

概要

ここでは Elastic Beanstalk のコマンドラインインターフェースである EB CLI を使用してアプリケーションのデプロイ 及び 手動でのスケールを行う。
尚、当記事では Auto Scaling や ELB、Cloudformatrion については記載しない。(別記事にて記載予定)

EB CLIのインストール

参考
https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/eb-cli3-install.html

EB CLIインストール

pip install awsebcli --upgrade --user

PATHに追加

echo PATH=~/Library/Python/3.X/bin:\$PATH>>~/.bash_profile
source ~/.bash_profile

プロジェクトフォルダ作成

mkdir SampleApp1
cd SampleApp1

デプロイするアプリケーションの作成

仮想環境作成

python3 -m venv venv
source ./venv/bin/activate

flaskインストール

pip install flask

パッケージ一括インストール用に requirements.txt を作成

pip freeze >requirements.txt

サンプルアプリケーション作成

application.py

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起動

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

別ターミナルから動作確認

curl http://127.0.0.1:5000/
<h1>top page!(20:02:44.414536)</h1>

※問題なければ、起動したflaskを CTRL+C で終了。

デプロイ

EB CLI でプロジェクトを初期化

※以下は、「eb init -p python-3.6 SampleApp1 --region ap-northeast-1」と同義。

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をデプロイ対象から外す

echo venv>>.ebignore

SSH鍵の設定

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 ]

デプロイ&起動

eb create eb-sample-app1

起動確認

eb open eb-sample-app1

スケール

インスタンス数を2に増やす

eb scale 2 eb-sample-app1

増えたか確認

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に減らす

eb scale 1 eb-sample-app1

減ったか確認

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

終了

eb terminate eb-sample-app1

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-05-12 (日) 21:39:05 (1810d)