LoginSignup
16
13

More than 5 years have passed since last update.

EC2のAuto Scalingで増えたサーバにCapistrano3からGateWay経由でデプロイする

Last updated at Posted at 2015-01-27

EC2のAuto Scalingで作られたインスタンスにCapistrano3でデプロイするための設定メモ。
インスタンスは外部から直接ログインできず、GateWay(NAT)経由でアクセスする必要があったため、
SSHのオプション設定も追加で設定。

参考にさせていただいたサイト

設定例

  • タグのkeyやvalueはAuto Scalingで設定した値に変更すること。
  • gatewayには~/.ssh/configで「ssh my_gateway」でログインできるようにしてある。
RAILS_ROOT/config/deploy/production.rb
require 'aws-sdk-core'
require 'net/ssh/proxy/command'

# auto scalingで作られたサーバのプライベートIPアドレス一覧を取得する。
ec2 = Aws::EC2::Client.new
f = -> (t) { t.key == "name" && t.value == "some_name" }
reservations = ec2.describe_instances.reservations.select{|r| r.instances.first.tags.any?{|t| f[t]}}
web_servers = reservations.map{|res| res.instances.map(&:private_ip_address)}.flatten.compact
role :web, web_servers

# Default branch is :master
set :branch, 'master'

# Custom SSH Options
# gateway経由でデプロイする。
set :ssh_options, {
  user: 'hoge',
  keys: %w(/Users/hoge/.ssh/id_rsa),
  forward_agent: true,
  auth_methods: %w(publickey),
  proxy: Net::SSH::Proxy::Command::new('ssh my_gateway -W %h:%p')
}
16
13
1

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
16
13