[awsマイスターシリーズ] aws elastic beanstalk -python編-

58

Click here to load reader

Upload: amazon-web-services-japan

Post on 10-Jun-2015

3.034 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

AWSマイスターシリーズ Reloaded

AWS Elastic Beanstalk Python編

アマゾンデータサービスジャパン株式会社

2012.12.17

Page 2: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

2

Agenda

Elastic Beanstalkとは? • Elastic Beanstalkの概要

• 最近のアップデート、追加された機能をご紹介

仕組み • 技術的な仕組みを解説

Python環境について • Python利用に特化した環境関連など

開発ワークフロー • 具体的な開発からデプロイ、環境アップデートなどのワークフ

ロー

Page 3: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

3

Agenda

Elastic Beanstalkとは? • Elastic Beanstalkの概要

• 最近のアップデート、追加された機能をご紹介

仕組み • 技術的な仕組みを解説

Python環境について • Python利用に特化した環境関連など

開発ワークフロー • 具体的な開発からデプロイ、環境アップデートなどのワークフ

ロー

Page 4: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

4

Elastic Beanstalkをひとことで言うと

開発したコードをデプロイするだけでWEBアプリケーション環境を構築できるサービス

• ELBやRDSもまとめて構築

• 簡単にオートスケーリングが利用可能

• 監視やイベント通知も含む

Instance

Amazon RDS (オプション)

Elastic Load Balancer

Instance

Auto scaling Group CloudWatch

deploy! WAR

Page 5: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

5

AWS Elastic Beanstalkとは

Beanstalk = 豆の木

• 「アプリケーションが実る幹」のイメージ

Webアプリケーションの実行環境を構築・管理するサービス

以前にもマイスターで取り上げています。 • http://www.slideshare.net/AmazonWebServicesJapan/201

20416-aws-meisterreloadedawselasticbeanstalkpublic-12662630

Page 6: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

6

AWS Elastic Beanstalkとは

使用可能な言語/環境

言語 コンテナ

Java Apache +Tomcat6,

Apache+ Tomcat7

.NET IIS7.5, IIS8

PHP Apache + mod_php

Python Apache + mod_wsgi

Ruby Nginx + Passenger

Page 7: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

7

AWS Elastic Beanstalkとは

実行環境の設定

• サーバスペック、VM設定など40項目以上の設定が可能

ロードバランサー・オートスケーリング

• ロードバランサーとオートスケーリングが標準装備

• 詳細な条件設定が可能

Page 8: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

8

AWS Elastic Beanstalkとは

Webアプリケーションのバージョン管理

• WARやZIPの世代管理が可能

• アプリのロールバック・ロールフォワードが容易

複数環境の構築が可能

• 本番・テストなど目的別に構築可能

開発・管理ツールを提供

• AWS Management Console

• コマンドラインツール

• Eclipseプラグイン

Page 9: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

9

開発・管理ツール

AWS Management

Console

AWS Toolkit for

Eclipse

Page 10: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

10

CloudWatchによるリソース監視

各種リソースをCloudWatchの機能を使って監視できます。

Page 11: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

11

Eventログの閲覧と通知

環境の構築やオートスケーリングなどのイベントの監視が可能です。

各イベントはSNS経由でメール通知可能です。

Page 12: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

12

料金

Elastic Beanstalk自体は料金は発生しません。立ち上げたEC2やELB、RDSなどの、通常のAWS利用料金の分だけかかります。

Page 13: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

13

Agenda

Elastic Beanstalkとは? • Elastic Beanstalkの概要

• 最近のアップデート、追加された機能をご紹介

仕組み • 技術的な仕組みを解説

Python環境について • Python利用に特化した環境関連など

開発ワークフロー • 具体的な開発からデプロイ、環境アップデートなどのワークフ

ロー

Page 14: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

14

用語とアーキテクチャ

Page 15: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

15

アプリケーション管理の論理構成

Application

Environment Version

WAR/ZIP URL Environment Configuration

Configuration Template

Environment

URL Environment Configuration

WAR/ZIP

WAR/ZIP

WAR/ZIP

WAR/ZIP

Environment

URL Environment Configuration

Page 16: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

16

用語説明

Application

• トップレベルの論理単位

• Applicationの中に、EnvironmentやVersionが入る

• デプロイするアプリケーション(WAR/ZIPファイル)と混同しやすいので注意

Environment

• アプリをデプロイする環境

• 環境毎にURLが付与される

• URLは、XXX.elasticbeanstalk.com

• 各環境毎にEC2、ELBなどが立ち上がる

• ログも環境毎に取得できる(1時間おきにS3へ送信可能)

Page 17: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

17

用語説明

Version

• Webアプリケーションのバージョン管理

• アプリケーションファイルの履歴管理が可能

• S3上でファイルを保持

• Versionで管理したWAR/ZIPファイルを、各Environmentと紐付け

Page 18: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

18

テクニカルアーキテクチャ

この環境が

自動で構成される

Instance Amazon RDS (オプション)

Elastic Load Balancer

Instance

Auto scaling Group

Amazon S3

ログ集約 (オプション)

Amazon

CloudWatch

Email

Notification

Application

Page 19: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

19

Elastic Beanstalkの機能紹介 -環境(environment)の設定-

Page 20: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

20

設定項目

Server

• 起動するアプリケーションサーバのインスタンス関連の設定

LoadBalancer

• ELB関連の設定

Auto Scaling

• オートスケーリングに関する設定

Database

• RDSに関する設定(Java, Python, Ruby環境のみ)

Notifications

• 各種アラートの通知先設定

Container

• コンテナ設定

Page 21: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

21

設定項目

Serverで設定可能な項目 • インスタンスタイプ

• セキュリティグループ

• キーペア

• 監視インターバル(1min or 5min)

• AMI

Load Balancerで設定可能な項目 • 待受ポート番号(http, https)

• SSL証明書

• ヘルスチェック関連

• セッションスティックネス

Page 22: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

22

設定項目

Auto Scalingで設定可能な項目 • 最大/最小台数

• AZ(任意のAZ、もしくは任意のAZ数を指定可能)

• クールダウンタイム

• スケーリングトリガー

Databaseで設定可能な項目 • RDSインスタンスタイプ

• (必要に応じて)復元元スナップショット

• DBエンジン

• ストレージ容量

• 削除ポリシー(environment削除時にRDSを削除するかどうか)

• DBユーザー名

• Multi-AZ設定

Page 23: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

23

設定項目

Notificationsで設定可能な項目 • 通知先メールアドレス

• 環境構築完了時やオートスケーリング動作時などに通知が飛びます。

Containerで設定可能な項目 • S3へのログ集約のオン/オフ

• 環境変数

• AWSアクセスキー

• AWSシークレットキー

• その他言語環境に応じた変数

• 自由定義可能な変数

Page 24: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

24

Elastic Beanstalkの機能紹介 -eb-

Page 25: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

25

ebとは

2012/6リリース

eb

ElasticBeanstalkアプリのライフサイクル管理CLI

Python based

サブコマンドを受け取る短い単一コマンド • E.g. “eb init”

eb is NOT

個々のAPIコールを行うCLI • elastic-beanstalk-describe-applications

• elastic-beanstalk-*

Ruby based

Page 26: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

26

eb コマンド一覧

eb init

アプリケーションの初期設定

eb start

アプリケーション環境の開始

eb status

ステータスの確認

eb update

アプリケーション環境の設定更新

eb stop

アプリケーション環境の停止

eb delete

アプリケーションの削除

Page 27: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

27

Elastic Beanstalkの機能紹介 -RDS-

Page 28: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

28

Elastic Beanstalk環境へのRDS統合

2012/8リリース

環境構築時にRDSも一緒に構築できるようになりました。

現状(2012/12)、.NETではご利用いただけません。

スナップショットからの起動や、環境削除時にRDSを削除するかどうかなど、細かい設定が可能です。

構築したRDSの各種設定値(エンドポイントなど)は環境変数としてコンテナに渡されます。

Page 29: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

29

Elastic Beanstalk環境へのRDS統合

設定項目

Page 30: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

30

Elastic Beanstalkの機能紹介 -コンフィグによるコンテナカスタマイズ-

Page 31: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

31

コンフィグファイル 2012/10リリース

EC2内の環境(コンテナ)をカスタマイズするための機能

2012/12現在、.NETではご利用頂けません。

アプリケーションリポジトリ内にyamlにて定義

• /.ebextensions/*.config

• ファイルは複数配置可能。ファイルは名前の順に処理される

• リポジトリ内に配置するのでgit等での管理も容易

packages:

yum:

postfix: '2.6.6'

commands:

01prepare:

command: “scripts/prepare.sh”

postfixをインストールして

特定のシェルスクリプトを実行する例

Page 32: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

32

コンフィグファイルで実行可能な操作

Packages • yumやrpmを利用したパッケージのインストール

Sources • 外部からのアーカイブ(tarなど)をダウンロードして指定した場所に展開する

Files • 任意の場所にファイルを生成して配置する

• ファイルの内容は外部からダウンロードする

Users • 任意のユーザーを作成する

Groups • 任意のグループを作成する

より詳細な情報は下記から参照してください。 http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/customize-containers.html

Page 33: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

33

コンフィグファイルで実行可能な操作

Commands • 任意のコマンド・プログラムを起動する

Container commands • リポジトリ内の任意のコマンド・プラグラムを起動する

Services • serviceを起動したり、起動設定を変更したりする

Options settings • 環境変数の設定を実施する

• 特定のnamespaceを設定するとコンテナ側の設定を修正することができる。

• namespaceについての参考http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/command-options.html

Page 34: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

34

Elastic Beanstalkの機能紹介 -VPC-

Page 35: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

35

VPC内へのデプロイ

Elastic Beanstalkアプリケーションは既存のVPCへデプロイすることも可能です。

2012/12現在、.NETでは ご利用頂けません。

ELBとEC2が別々の subnetにデプロイされます。

Page 36: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

36

環境ごとElastic Beanstalk機能比較表

Java、Python、Rubyは新機能に対応済み

言語 コンテナ eb RDS コンフィグ VPC

Java Tomcat6,

Tomcat7

○ ○ ○ ○

.NET IIS7.5, IIS8 ○

PHP Apache +

mod_php

○ ○ ○ ○

Python Apache +

mod_wsgi

○ ○ ○ ○

Ruby Nginx +

Passenger

○ ○ ○ ○

Page 37: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

37

Agenda

Elastic Beanstalkとは? • Elastic Beanstalkの概要

• 最近のアップデート、追加された機能をご紹介

仕組み • 技術的な仕組みを解説

Python環境について • Python利用に特化した環境関連など

開発ワークフロー • 具体的な開発からデプロイ、環境アップデートなどのワークフ

ロー

Page 38: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

38

PythonでElastic Beanstalk

以下のような環境が立ち上がります。

Instance Amazon RDS (オプション)

Elastic Load Balancer

Instance

Auto scaling Group

Amazon S3

ログ集約 (オプション)

Apache+mod_wsgi

supervisord

Amazon

CloudWatch

Email

Notification

Application

Page 39: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

39

アプリケーションサーバの構成

supervisord

• apacheはsupervisordから起動され、プロセス監視されています。

• 設定ファイルのパスは下記のとおりです。

• /opt/python/etc/supervisord.conf

apache

• /etc/httpd/conf/httpd.confで動いています。

• mod_wsgi経由で/opt/python/current/app/application.pyを起動しています。(パスはデフォルト)

• アプリケーションのパスは、コンフィグファイルで指定できます。(httpd.confの内容が書き換わります)

Page 40: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

40

WEBアプリケーションフレームワークの利用

現在、下記の2つのWAFがテスト済みです。

• Django

• http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/create_deploy_Python_django.html

• Flask

• http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/create_deploy_Python_flask.html

もちろん、PyramidなどのWAFも動きます。

Page 41: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

41

pipによる依存モジュールのインストール

アプリケーションが依存するモジュールをインストール

• コード内で利用しているpythonの各種モジュールをリポジトリのトップレベルにrequirements.txtというファイル名で記述しておくことにより、それらのモジュールをデプロイ時にインストールしてくれます。

pip freezeを使うと簡単に作成可能です。

$ pip install Django

$ pip install xxx

$ pip freeze > requirements.txt

Django==1.4.1

Page 42: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

42

コンフィグファイルとpipの棲み分け

pipはpythonのモジュールインストール。コンフィグファイルはそれ以外。

スコープ:コンテナ(サーバー)グローバル

→ コンフィグファイル

スコープ:python環境

→ pip

Page 43: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

43

RDSの利用方法

RDSの各種情報・設定値

• 環境内に構築されたRDSの各種情報には、環境変数からアクセスできます。

• http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/create_deploy_Python.rds.html#create_deploy_Python.rds.newDB

Page 44: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

44

Agenda

Elastic Beanstalkとは? • Elastic Beanstalkの概要

• 最近のアップデート、追加された機能をご紹介

仕組み • 技術的な仕組みを解説

Python環境について • Python利用に特化した環境関連など

開発ワークフロー • 具体的な開発からデプロイ、環境アップデートなどのワークフロー

Page 45: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

45

Djangoを使ったウェブアプリケーションをElastic Beanstalk上にデプロイしてみます

• 下記の手順をベースにしています。 http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/create_deploy_Python_django.html

• 今回つかったコードは下記にアップロードしました。 https://github.com/imaifactory/elasticbeanstalk-python-django

Djangoアプリケーションをデプロイしてみる

Page 46: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

46

Djangoアプリケーションをデプロイしてみる

1. djangoアプリケーションを作成する

django_testという名前でプロジェクトを作成します。

2. gitのリポジトリ初期化

$django-admin.py startproject django_site

$cd django_site

$git init

Page 47: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

47

Djangoアプリケーションをデプロイしてみる

3. ebにてElastic Beanstalkアプリとして初期化

eb initコマンドを発行すると、対話形式で以下の項目が聞かれます。

1. AWSアクセスキー

2. AWSシークレットキー

3. 利用リージョン

4. コンテナ環境

5. RDS設定(コンテナにJava, Python, Rubyを選択した場合)

$eb init

Page 48: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

48

Djangoアプリケーションをデプロイしてみる

3. ebにてElastic Beanstalkアプリとして初期化

eb initコマンドが完了すると以下のようなファイルが出来上がってきます。

$vi .elasticbeanstalk/config

1 AwsCredentialFile=/home/ec2-user/.elasticbeanstalk/aws_credential_file

2 ApplicationName=django_site

3 EnvironmentName=djangosite-env

4 Region=ap-northeast-1

5 ServiceEndpoint=https://elasticbeanstalk.ap-northeast-1.amazonaws.com

6 RdsEndpoint=https://rds.ap-northeast-1.amazonaws.com

7 SolutionStack=64bit Amazon Linux running Python

8 RdsEnabled=Yes

9 RdsSourceSnapshotName=

10 RdsDeletionPolicy=Snapshot

Page 49: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

49

Djangoアプリケーションをデプロイしてみる

4. ebでアプリケーション環境を立ち上げる

数分~10分ほど時間がかかります。eb statusというコマンドか、マネージメントコンソールで状況が確認できます。完了すると、以下のようなWEB画面を表示するアプリケーションが動くようになっています。

$eb start

現状、サンプルアプリケーションが動いている状態なので、これからdjangoアプリをデプロイしていきます。

Page 50: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

50

Djangoアプリケーションをデプロイしてみる 5. 環境変数を設定するコンフィグファイルを作成 .ebextensionsというディレクトリを作ってその中に django_sample.configというファイルを作ります。

$mkdir .ebextensions

$vi .ebextensions/django_sample.config

1 option_settings:

2 - namespace: aws:elasticbeanstalk:container:python

3 option_name: WSGIPath

4 value: django_site/wsgi.py

5 - option_name: DJANGO_SETTINGS_MODULE

6 value: django_site.settings

• ファイル名は拡張子が.configであればなんでも構いません。アルファベット順にすべてのファイルが処理されます。

• WSGIPathという環境変数に、aws:elasticbeanstalk:container:pythonというnamespaceを付けて設定することにより、apacheのアプリのパスがここに設定されます。

Page 51: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

51

Djangoアプリケーションをデプロイしてみる

6. requirements.txtを作成

ここで作成したrequirements.txtに記載されているものがElastic Beanstalkの環境でも自動的にインストールされます。

7. アプリケーションをコミットしてpush

$pip freeze > requirements.txt

$git add ./*

$git commit -a -m ‘first commit’

$git aws.push

Page 52: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

52

Djangoアプリケーションをデプロイしてみる

8. こんな画面が表示されたらOK!

URLはマネージメントコンソールもしくは

eb statusで確認可能です。

Page 53: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

53

TIPS

Page 54: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

54

TIPS

SSHでのログイン

• SSHでEC2にログインするにはマネジメントコンソールなどでkeypairの設定をしてあげて下さい。

AMIのカスタマイズ

• コンフィグファイルやpip以外の環境カスタマイズの方法として、AMIを自分でカスタマイズすることも可能です。

• 環境カスタマイズについては、コンフィグ, pip, AMIなどいろいろな方法で実現可能ですが、どれが良い、どれがベストプラクティスというのはありませんので、それぞれご自分の環境で使いやすい方法をとってください。

Page 55: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

55

TIPS

継続的なコードデプロイ

• 今回ご紹介した一連のデプロイの流れはアプリケーションの初期化から始まっていますが、一旦環境を作ったあとは、コードを修正してgit aws.pushすれば新しいコードがデプロイされていきます。

Page 56: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

56

TIPS

VPC内へのデプロイ

• Elastic BeanstalkはVPC内へのデプロイも可能です。

• 手順的には、以下のものを事前に作った上でそれをコンフィグファイルに設定してデプロイします。

• VPC

• ELB用subnet

• EC2用subnet

• NATインスタンス

• RDS用subnet(option)

• 参考:http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/AWSHowTo-vpc-basic.html

Page 57: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

57

まとめ

Page 58: [AWSマイスターシリーズ] AWS Elastic Beanstalk -Python編-

© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

58

まとめ

Beanstalkを使うとWEBアプリケーションの開発/デプロイが容易。

RDSやVPCなどの他のAWSのサービスとの連携も容易。

注意点としては、言語ごとに利用できる機能に差異があること。

簡単に使うことも可能ですが、非常に柔軟な環境カスタマイズも可能!