LoginSignup
26
27

More than 5 years have passed since last update.

ebコマンドを使ってElasticBeanstalkでデプロイしてみる

Last updated at Posted at 2015-08-10

環境

  • Ubuntu14.04
  • AWS credentialの設定済み(~/.aws配下のcredentialが配置済み)

参考

ebコマンドのインストール

$sudo pip install awsbcli
・・・
    ext/_yaml.c:8:22: fatal error: pyconfig.h: No such file or directory

     #include "pyconfig.h"

                          ^

    compilation terminated.

エラー。。。。
同じような問題がある場所を発見。python-devインストールすれば良いらしいのでやってみます。

Install linux: pyconfig.h No such file or directory #83

$sudo apt-get install python-dev
$sudo pip install awsbcli
$eb --version
EB CLI 3.4.7 (Python 2.7.6)

インストールできました。

また、ebコマンドのサブコマンドなどの補完を効かせるためにcompletionを.bashrcで読み込むようにします。

$echo "source /usr/local/bin/eb_completion.bash" >> ~/.bashrc
$source ~/.bashrc

Webアプリの作成

テスト用PHPアプリを作成して、gitに登録します。

$mkdir -p git/elastic-beanstalk
$cd git/elastic-beanstalk
$git init .
$vi index.php
index.php
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World!</p>'; ?> 
 </body>
</html> 
$git add index.php
git commit -m "initial check-in"

アプリケーションの設定

eb init コマンドを使ってアプリケーションの登録を行います。

$eb init

以降、対話形式で設定の質問が聞かれます。

最初にリージョン、アプリケーション名称を入力します。リージョンはTokyo、アプリケション名は何も入力せず、デフォルトではelastic-beanstalkというアプリケーション名にします。

$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-southeast-1 : Asia Pacific (Singapore)
7) ap-southeast-2 : Asia Pacific (Sydney)
8) ap-northeast-1 : Asia Pacific (Tokyo)
9) sa-east-1 : South America (Sao Paulo)
(default is 3): 8

Enter Application Name
(default is "elastic-beanstalk"):
Application elastic-beanstalk has been created.

次にアプリケーションの言語を指定します。今回はPHP5.6なので、最初はyを入力し、次に1を入力します。

It appears you are using PHP. Is this correct?
(y/n):y

Select a platform version.
1) PHP 5.6
2) PHP 5.5
3) PHP 5.4
4) PHP 5.3
(default is 1):

次にインスタンスに対してSSHキーを設定するか確認されるので必要に応じて既存のキーや新規作成したキーの割り当てを行います。

Do you want to set up SSH for your instances?
(y/n):y
Select a keypair.
1) ABCKey
2) HogeFugaKey
3) [ Create new KeyPair ]
(default is 3):2

上記でアプリケーションの作成は完了です。
作成後、以下2つのファイルの作成が確認できます。

.gitignore
# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
.elasticbeanstalk/config.yml
branch-defaults:
  master:
    environment: null
global:
  application_name: elastic-beanstalk
  default_ec2_keyname: HogeFugaKey
  default_platform: PHP 5.6
  default_region: null
  profile: null
  sc: git

環境の設定をする

先ほど登録したアプリケーションに対応する環境を作成します。

環境とは例えば

  • 検証環境
  • 開発環境
  • 本番環境

といったように1つのアプリケーションでも複数の環境でのデプロイが必要な場合などに有用な機能です。デプロイする場合に最低1つは環境が必要なので作成します。

eb ceateコマンドによって新しい環境の作成を行います。

最初に環境の名前を入力します。defaultの場合、何も入力せず、enter。

$eb create
Enter Environment Name
(default is elastic-beanstalk-dev):

次にDNS CNAMEを聞かれます。defaultの場合、何も入力せず、enter。

Enter DNS CNAME prefix
(default is elastic-beanstalk-dev):

上記実施後、実際にアプリケーションが登録され、環境にデプロイが行われます。(EC2の起動などあるため多少時間がかかります)

Creating application version archive "5538".
Uploading elastic-beanstalk/5538.zip to S3. This may take a while.
Upload Complete.
Environment details for: elastic-beanstalk-dev
  Application name: elastic-beanstalk
  Region: ap-northeast-1
  Deployed Version: 5538
  Environment ID: hogefuga
  Platform: 64bit Amazon Linux 2015.03 v1.4.6 running PHP 5.6
  Tier: WebServer-Standard
  CNAME: elastic-beanstalk-dev.elasticbeanstalk.com
  Updated: 2015-08-07 03:00:20.698000+00:00
Printing Status:
INFO: Using elasticbeanstalk-ap-northeast-1-1111111 as Amazon S3 storage bucket for environment data.
・・・
INFO: Successfully launched environment: elastic-beanstalk-dev

なお、以下のようにすること事で指定したVPC,サブネット内にインスタンスの作成が可能です。

$eb create --vpc.id vpc-xxxxxx --vpc.ec2subnets subnet-xxxx --vpc.publicip --vpc.elbpublic
  • --vpc.publicipオプションがないとEC2にパブリックIPアドレスが設定されないため、ElasticBeanstalkのサーバーと通信ができずエラーになる
  • --vpc.elbpublicオプションがないと作成されるELBがinternalになる(vpcの設定をしなければこのプションがなくてもpublicなELBになる)

デプロイしたサイトを見たい場合、eb openコマンドを使います。

$eb open

とするとなにか?が立ち上がってCLI上でも内容が確認できます。

Hello World!

Ctrl+Zで終了できます。

ログを見たい場合、eb logsコマンドを使います。

$eb logs
============= i-54cc4da6 ==============
-------------------------------------
/var/log/eb-version-deployment.log
-------------------------------------
・・・・

-------------------------------------
/var/log/httpd/error_log
-------------------------------------
・・・・

-------------------------------------
/var/log/httpd/access_log
-------------------------------------
・・・

-------------------------------------
/var/log/eb-activity.log
-------------------------------------
・・・

SSHログインしなくてもhttpdのログなど見れるようです。

以下に詳細な記述あり。

ログ

変更を反映する

ローカルで変更した内容を反映させます。
今回は!を追加してみました。

index.php
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World!!!!!<</p>'; ?> 
 </body>
</html>

変更後、gitにコミットし、その後eb deploy コマンドを使えば実際の環境への反映ができます。

$git add index.php
$git commit -m "my third check-in"
$eb deploy

eb openコマンドを使うと変更した内容が反映されているのが確認できます!

26
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
27