AWS Elastic Beanstalkの(多分)最速設定

サーバー選定するときにawsが良いなって思うんだけどaws通信費高いしなってなってじゃあどこにしようかって考えるんだけどあんまり決まらなくて考える時間費やすのが本当に無駄な気がしてて金で解決できる事は金で解決した方が幸せなんじゃないか、金のために働くなんて本当にむなしいし金に働いてもらって想像力を無限に膨らませたいしairbnbで沖縄のペンションみたいなところ借りて海眺めながらTumblrでもしていたい。

なので金で解決するための[AWS Elastic Beanstalk](http://aws.amazon.com/jp/elasticbeanstalk/?nc1=h_l2_dm)の(多分)最速設定方法


参考 <http://dev.classmethod.jp/cloud/aws/eb-sinatra-in-10minutes/>

# AWS Elastic Beanstalkのcliコマンドのインストール

    brew install aws-elasticbeanstalk
    eb -h

```
$ eb -h
usage: eb {COMMAND} [options]

AWS Elastic Beanstalk Command Line Interface v2.6.0

positional arguments:
  COMMAND               init, branch, start, status, update, stop, delete,
                        logs, events, push
  SUBCOMMAND

optional arguments:
  -h, --help            show this help message and exit
  -I ACCESS_KEY_ID, --access-key-id ACCESS_KEY_ID
                        Your AWS Access Key ID.
  -S SECRET_ACCESS_KEY, --secret-key SECRET_ACCESS_KEY
                        Your AWS Secret Key.
  --aws-credential-file FILE_PATH_NAME
                        File location where your AWS credentials are saved.
                        Use the environment variable AWS_CREDENTIAL_FILE to
                        set the file location.
  -t ENVIRONMENT_TIER, --environment-tier ENVIRONMENT_TIER
                        Environment tier used for the application
  -s  [ ...], --solution-stack  [ ...]
                        Solution stack used as an application container type.
  -a APPLICATION_NAME, --application-name APPLICATION_NAME
                        The name of the application to deploy.
  -l VERSION_LABEL, --version-label VERSION_LABEL
                        The name of the application version to deploy.
  -e ENVIRONMENT_NAME, --environment-name ENVIRONMENT_NAME
                        A unique name for the environment deployment. The
                        environment name is used in the application URL.
  --verbose             Display verbose information.
  -f, --force           Skip confirmation prompt.
  --polling-timeout TIMEOUT_IN_SEC
                        Number of seconds before a start or stop operation
                        times out when polling for environment status. The
                        default is 600 second.
  --region REGION       AWS Elastic Beanstalk region (i.e., us-east-1).
  --url ENDPOINT        AWS Elastic Beanstalk endpoint (i.e., elasticbeanstalk
                        .us-east-1.amazonaws.com).
  --version             show program's version number and exit
```

# テストアプリの作成

    mkdir money
    cd money

    vi app.rb
```app.rb:ruby
require 'sinatra'

get '/' do
  'お金最高！！！'
end
```

    vi config.ru
```config.ru:ruby
require './app.rb'
run Sinatra::Application
```

    vi Gemfile
```Gemfile:ruby
source 'http://rubygems.org'
gem 'sinatra'
```

    bundle install
    bundle exec rackup

<http://localhost:9292>

`お金最高！！！`って出てればオッケー


    echo '.bundle' >> .gitignore
    git init
    git add -A
    git commit -m 'first commit'

# Elastic Beanstalkの設定

下準備

    eb init

以下、対話時のログ

```
 $ eb init
To get your AWS Access Key ID and Secret Access Key, 
  visit "https://aws-portal.amazon.com/gp/aws/securityCredentials".
Enter your AWS Access Key ID: ******************* # 自分のAccess Key
Enter your AWS Secret Access Key: ******************* # 自分のSecret Access Key
Select an AWS Elastic Beanstalk service region.
Available service regions are:
1) US East (Virginia)
2) US West (Oregon)
3) US West (North California)
4) EU West (Ireland)
5) Asia Pacific (Singapore)
6) Asia Pacific (Tokyo)
7) Asia Pacific (Sydney)
8) South America (Sao Paulo)
Select (1 to 8): 6 # Tokyoで設定
Enter an AWS Elastic Beanstalk application name (auto-generated value is "money"): # 空エンター
Enter an AWS Elastic Beanstalk environment name (auto-generated value is "money-env"): # 空エンター
Select an environment tier.
Available environment tiers are:
1) WebServer::Standard::1.0
2) Worker::SQS/HTTP::1.0
Select (1 to 2): 1 # とりあえずスタンダードで
Select a solution stack.
Available solution stacks are:
1) 32bit Amazon Linux 2014.03 v1.0.2 running PHP 5.5
2) 64bit Amazon Linux 2014.03 v1.0.2 running PHP 5.5
3) 32bit Amazon Linux 2014.03 v1.0.2 running PHP 5.4
4) 64bit Amazon Linux 2014.03 v1.0.2 running PHP 5.4
5) 32bit Amazon Linux running PHP 5.3
6) 64bit Amazon Linux running PHP 5.3
7) 32bit Amazon Linux 2014.03 v1.0.2 running Node.js
8) 64bit Amazon Linux 2014.03 v1.0.2 running Node.js
9) 64bit Windows Server 2008 R2 running IIS 7.5
10) 64bit Windows Server 2012 running IIS 8
11) 32bit Amazon Linux 2014.03 v1.0.2 running Tomcat 7 Java 7
12) 64bit Amazon Linux 2014.03 v1.0.2 running Tomcat 7 Java 7
13) 32bit Amazon Linux 2014.03 v1.0.2 running Tomcat 7 Java 6
14) 64bit Amazon Linux 2014.03 v1.0.2 running Tomcat 7 Java 6
15) 32bit Amazon Linux running Tomcat 7
16) 64bit Amazon Linux running Tomcat 7
17) 32bit Amazon Linux running Tomcat 6
18) 64bit Amazon Linux running Tomcat 6
19) 32bit Amazon Linux 2014.03 v1.0.2 running Python 2.7
20) 64bit Amazon Linux 2014.03 v1.0.2 running Python 2.7
21) 32bit Amazon Linux 2014.03 v1.0.2 running Python
22) 64bit Amazon Linux 2014.03 v1.0.2 running Python
23) 32bit Amazon Linux running Python
24) 64bit Amazon Linux running Python
25) 64bit Amazon Linux 2014.03 v1.0.3 running Ruby 2.0 (Puma)
26) 64bit Amazon Linux 2014.03 v1.0.2 running Ruby 2.0 (Passenger Standalone)
27) 32bit Amazon Linux 2014.03 v1.0.2 running Ruby 1.9.3
28) 64bit Amazon Linux 2014.03 v1.0.2 running Ruby 1.9.3
29) 32bit Amazon Linux 2014.02 v1.0.1 running Ruby 1.8.7
30) 64bit Amazon Linux 2014.02 v1.0.1 running Ruby 1.8.7
31) 64bit Amazon Linux 2014.03 v1.0.4 running Docker 0.9.0
32) 64bit Amazon Linux 2014.03 v1.0.3 running Docker 0.9.0
Select (1 to 32): 26 # ruby 2.0 Passengerのタイプ
Select an environment type.
Available environment types are:
1) LoadBalanced
2) SingleInstance
Select (1 to 2): 2 # 今回は単一インスタンスのみ
Create an RDS DB Instance? [y/n]: n # DBなし
Attach an instance profile (current value is "[Create a default instance profile]"):
1) [Create a default instance profile]
2) [Other instance profile]
Select (1 to 2): 4 # 打ち間違い
Select (1 to 2): 1 # profileなんか作られてるっぽいからそれを指定
Updated AWS Credential file at "/Users/mugi/.elasticbeanstalk/aws_credential_file".

```

終わったら`.elasticbeanstalk/`が作られてて`.gitignore`にも記述追加されてると思う

# サーバーの作成

    eb start

```
 $ eb start
Starting application "money".
Would you like to deploy the latest Git commit to your environment? [y/n]: y
remote: 
remote: error: Unable to deploy application version: No Environment found for EnvironmentName = 'money-env'.
remote: 
To https://************めちゃめちゃ長いgitリポジトリurl************
 * [new branch]      HEAD -> master

Waiting for environment "money-env" to launch.
2014-05-23 19:25:11     INFO    createEnvironment is starting.
2014-05-23 19:25:16     INFO    Using elasticbeanstalk-ap-northeast-1-************ as Amazon S3 storage bucket for environment data.
2014-05-23 19:25:53     INFO    Created EIP: ***.***.***.***
2014-05-23 19:25:54     INFO    Created security group named: awseb-e-******-stack-AWSEBSecurityGroup-******
2014-05-23 19:26:56     INFO    Waiting for EC2 instances to launch. This may take a few minutes.
2014-05-23 19:31:43     INFO    Application available at money-env-******.elasticbeanstalk.com.
2014-05-23 19:31:43     INFO    Successfully launched environment: money-env
Application is available at "money-env-************.elasticbeanstalk.com".
```

数分かかった。5分未満だったかな。

# ソースコードのデプロイ

    git aws.push

```
$ git aws.push
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 626 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
remote: 
To https://************めちゃめちゃ長いgitリポジトリurl************
 + a0cec7a...4a300e7 HEAD -> master (forced update)
```

確認

    eb status

```
$ eb status
URL             : money-env-******.elasticbeanstalk.com
Status          : Ready
Health          : Green
```

<http://money-env-*****.elasticbeanstalk.com>

お金最高！！！