AWS Elastic Beanstalkの使用方法を修得する。
ソフトウェア | バージョン | 備考 |
---|---|---|
AWS Elastic Beanstalk | API Version 2010-12-01 | |
Ruby | 2.1.2 | |
Python | 2.7.5 | |
hazel | 0.0.8 |
- セットアップ
- Rails アプリケーションの AWS Elastic Beanstalk へのデプロイ
- Sinatra アプリケーションの AWS Elastic Beanstalk へのデプロイ
- Ruby 環境のカスタマイズと設定
- Ruby での Amazon RDS の使用
- Ruby での VPC の使用
$ wget https://s3.amazonaws.com/elasticbeanstalk/cli/AWS-ElasticBeanstalk-CLI-2.6.3.zip
$ unzip AWS-ElasticBeanstalk-CLI-2.6.3.zip
$ cp -r AWS-ElasticBeanstalk-CLI-2.6.3/eb /usr/local/
$ export PATH=$PATH:/usr/local/eb/macosx/python2.7/
fooapp
$ wget https://s3.amazonaws.com/elasticbeanstalk/cli/AWS-ElasticBeanstalk-CLI-2.6.3.zip
$ cp -r AWS-ElasticBeanstalk-CLI-2.6.3/AWSDevTools /usr/local/
$ bash /usr/local/AWSDevTools/Linux/AWSDevTools-RepositorySetup.sh
$ sudo pip install boto
$ rails new fooapp
$ cd fooapp
$ git init && git add -A && git commit -m "Initial commit"
$ eb init
・・・
$ eb start
Starting application "fooapp".
Would you like to deploy the latest Git commit to your environment? [y/n]: y
Waiting for environment "fooapp-env" to launch.
2014-10-10 15:40:00 INFO createEnvironment is starting.
2014-10-10 15:40:05 INFO Using elasticbeanstalk-ap-northeast-1-262470114399 as Amazon S3 storage bucket for environment data.
2014-10-10 15:40:34 INFO Created EIP: 54.249.242.128
2014-10-10 15:40:35 INFO Created security group named: awseb-e-vtt2jy47na-stack-AWSEBSecurityGroup-1DYZF4TKYOAYG
2014-10-10 15:41:42 INFO Waiting for EC2 instances to launch. This may take a few minutes.
2014-10-10 15:45:40 INFO Application available at fooapp-env-unbndwrs9f.elasticbeanstalk.com.
2014-10-10 15:45:40 INFO Successfully launched environment: fooapp-env
Application is available at "fooapp-env-unbndwrs9f.elasticbeanstalk.com".
$ rake secret
5f7d10669f7dec9328825965a6ed265169532d330b23fc4908bf030d6b96709efab0f1f897173624e255dba0402e3ddeb9bc3cbec97b0b8ffae92f68f4f99b43
fooapp/.elasticbeanstalk/optionsettings.fooapp-env
・・・
[aws:elasticbeanstalk:application:environment]
BUNDLE_WITHOUT=test:development
PARAM1=
PARAM2=
PARAM3=
PARAM4=
PARAM5=
RACK_ENV=production
RAILS_SKIP_ASSET_COMPILATION=false
RAILS_SKIP_MIGRATIONS=false
SECRET_KEY_BASE=5f7d10669f7dec9328825965a6ed265169532d330b23fc4908bf030d6b96709efab0f1f897173624e255dba0402e3ddeb9bc3cbec97b0b8ffae92f68f4f99b43
・・・
$ eb update
$ eb status --verbose
Retrieving status of environment "fooapp-env".
URL : fooapp-env-unbndwrs9f.elasticbeanstalk.com
Status : Ready
Health : Green
Environment Name: fooapp-env
Environment ID : e-vtt2jy47na
Environment Tier: WebServer::Standard::1.0
Solution Stack : 64bit Amazon Linux 2014.03 v1.0.7 running Ruby 2.1 (Puma)
Version Label : git-4e2fcc53a1997d2fc38c4d78aa1e299868005a86-1412923196427
Date Created : 2014-10-10 15:40:01
Date Updated : 2014-10-10 15:45:40
Description :
$ open http://fooapp-env-unbndwrs9f.elasticbeanstalk.com
$ git add .gitignore && git commit -m "Ignore .elasticbeanstalk from Git"
$ git aws.push
$ rails g scaffold article url:string title:string category:string published:date access:integer comments_count:integer losed:boolean
$ rake db:migrate
fooapp/config/routes.rb
root 'articles#index'
$ git add .
$ git commit -am "Create Application"
$ git aws.push
fooapp/config/database.yml
production:
adapter: mysql2
encoding: utf8
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
fooapp/Gemfile
gem 'mysql2'
$ git add .
$ git commit -m "update database"
$ git aws.push
$ eb stop
fooapp_sinatra
$ hazel fooapp_sinatra
$ cd fooapp_sinatra
$ git init && git add -A && git commit -m "Initial commit"
$ eb init
eb start
このままだと_config/initializers_フォルダがレポジトリに含まれずエラーになるのでレポジトリに空フォルダを追加
$ touch config/initializers/.gitkeep
$ git commit -am "add folder"
$ git aws.push
$ eb status --verbose
Retrieving status of environment "production".
URL : production-bspwdwmpxe.elasticbeanstalk.com
Status : Ready
Health : Green
Environment Name: production
Environment ID : e-8unr2xqse9
Environment Tier: WebServer::Standard::1.0
Solution Stack : 64bit Amazon Linux 2014.03 v1.0.7 running Ruby 2.1 (Passenger Standalone)
Version Label : git-aee0edd5922790e2b0986b729e1c0f628798c7cb-1412931658208
Date Created : 2014-10-10 17:41:16
Date Updated : 2014-10-10 18:01:19
Description :
$ open http://production-bspwdwmpxe.elasticbeanstalk.com
$ eb stop
$ eb delete
split-sinatra-example
$ git clone https://github.com/andrew/split-sinatra-example.git
$ cd sqplit-sinatra-example
$ eb init
split-sinatra-example/.ebextensions/myapp.config
packages:
yum:
git: []
commands:
redis_build:
command: yum -y --enablerepo=epel install redis
services:
sysvinit:
redis:
enabled: true
ensureRunning: true
container_commands:
bundle_install:
command: bundle install --deployment
$ git add .
$ git commit -am "add myapp.config"
$ eb start
$ hazel fooapp_rds
$ cd fooapp_rds
fooapp_rds/Gemfile
gem "sinatra-activerecord"
gem "sqlite3"
fooapp_rds/fooapp_rds.rb
require "sinatra/activerecord"
class FooappRds < Sinatra::Base
register Sinatra::ActiveRecordExtension
set :database, {adapter: "sqlite3", database: "foo.sqlite3"}
set :public_folder => "public", :static => true
get "/" do
erb :welcome
end
end
fooapp_rds/Rakefile
require "sinatra/activerecord/rake"
namespace :db do
task :load_config do
require "./fooapp_rds"
end
end
$ bundle
$ rake -T
$ rake db:create_migration NAME=create_users
db/migrate/20141013044321_create_users.rb
fooapp_rds/db/migrate/20141013044321_create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
end
end
end
$ rake db:migrate
== 20141013044321 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0024s
== 20141013044321 CreateUsers: migrated (0.0026s) =============================
fooapp_rds/fooapp_rds.rb
class User < ActiveRecord::Base
validates_presence_of :name
end
get '/users' do
@users = User.all
erb :welcome
end
get '/users/:id' do
@user = User.find(params[:id])
erb :welcome
end
fooapp_rds/Gemfile
gem 'guard-shotgun', :git => 'https://github.com/rchampourlier/guard-shotgun.git'
gem "rack-livereload"
gem 'guard-livereload', require: false
fooapp_rds/config.ru
# Rack LiveReload
require 'rack-livereload'
use Rack::LiveReload
$ bundle
$ guard init shotgun
$ guard init livereload
fooapp_rds/Guardfile
group :server do
guard :shotgun do
watch(/.+/) # watch *every* file in the directory
end
end
guard 'livereload' do
watch(%r{views/.+\.(erb|haml|slim)$})
watch(%r{fooapp_rds.rb})
end
$ guard
14:00:14 - INFO - Guard is using TerminalTitle to send notifications.
14:00:14 - INFO - LiveReload is waiting for a browser to connect.
14:00:14 - INFO - Starting up Rack...
[2014-10-13 14:00:16] INFO WEBrick 1.3.1
[2014-10-13 14:00:16] INFO ruby 2.1.1 (2014-02-24) [x86_64-darwin12.0]
[2014-10-13 14:00:16] INFO WEBrick::HTTPServer#start: pid=4927 port=9292
14:00:16 - INFO - Guard is now watching at '/Users/k2works/projects/github/aws_elasticbeanstalk_introduction/fooapp_rds'
_fooapp_rds/fooapp_rds.rb_からset :database, {adapter: "sqlite3", database: "foo.sqlite3"}
を削除
_fooapp_rds/config/database.yml_作成
production:
adapter: mysql2
encoding: utf8
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
_fooapp_rds/Gemfile_編集
gem "sqlite3", :group => [:development,:test]
gem "mysql", :group => :production
$ eb init
・・・
Create an RDS DB Instance? [y/n] (current value is "Yes"):
・・・
$ eb start
fooapp_rds/.ebextensions
packages:
yum:
git: []
container_commands:
bundle_install:
command: bundle install --deployment
container_commands:
01-bundle_install:
command: bundle install --deployment
02-db_migrate:
command: bundle exec rake db:migrate
03-chown:
command: chown -R webapp:webapp ./
_fooapp_rds/config/initializers/.gitkeep_に追加
$ git add . && git commit -m "Add deploy setting"
$ git aws.push
その他DB設定はRDS付きのBeanstalkを使う際の注意点参照
AWS CloudFormationを使ってVPC環境をセットアップする。
cloudformeer.template.fooapp_vpc.json
$ git init && git add -A && git commit -m "Initial commit"
$ eb init
$ eb branch
The current branch is "master".
Enter an AWS Elastic Beanstalk environment name (auto-generated value is "fooappvpc-master-env"): fooappVpc-env
Do you want to copy the settings from environment "production" for the new branch? [y/n]: y
Updated AWS Credential file at "/Users/k2works/.elasticbeanstalk/aws_credential_file".
$ eb start
Starting application "fooapp_vpc".
Some of your option settings are ignored because they don't apply to your environment type.
Environment "fooappVpc-env" already exists. Skipped creating.
Application is available at "production-fooappVpc-env.elasticbeanstalk.com".
$ rails new fooapp_vpc
$ cd fooapp_vpc
$ rails g scaffold article url:string title:string category:string published:date access:integer comments_count:integer losed:boolean
$ rake db:migrate
$ rake secret
ea0f4840d6c3933cf1bab33ad45128a2c543fd1fac229411b5b81cdc7d71821b6c4f832613cd3bc6d8203f16e65521f41574fe446873f7c3956f17d9b72ef905
fooapp_vpc/.elasticbeanstalk/optionsettings.production
・・・
[aws:elasticbeanstalk:application:environment]
BUNDLE_WITHOUT=test:development
PARAM1=
PARAM2=
PARAM3=
PARAM4=
PARAM5=
RACK_ENV=production
RAILS_SKIP_ASSET_COMPILATION=false
RAILS_SKIP_MIGRATIONS=false
SECRET_KEY_BASE=ea0f4840d6c3933cf1bab33ad45128a2c543fd1fac229411b5b81cdc7d71821b6c4f832613cd3bc6d8203f16e65521f41574fe446873f7c3956f17d9b72ef905
・・・
fooapp_vpc/Gemfile
gem 'sqlite3', group: [:development,:test]
gem 'mysql2', group: :production
fooapp_vpc/config/database.yml
・・・
production:
adapter: mysql2
encoding: utf8
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
・・・
config/application.rb
config.time_zone = 'Tokyo'
fooapp/config/routes.rb
root 'articles#index'
$ git add -A && git commit -m "Application Setup"
$ eb update
$ eb push
$ eb stop
$ eb delete
CloudFormationで構築したVPCも削除する