Skip to content

Instantly share code, notes, and snippets.

@cvele
Last active October 30, 2018 15:46
Show Gist options
  • Save cvele/a3a0319ea3ce4f0a29d8 to your computer and use it in GitHub Desktop.
Save cvele/a3a0319ea3ce4f0a29d8 to your computer and use it in GitHub Desktop.
Capistrano 3 (ruby) snippet to deploy to multiple AWS servers attached to ELB dynamically. Add this to production.rb (or whatever stage) instead of servers definition. AWS ec2 cli required.
require 'rubygems'
require 'json'
LOADBALANCER = 'your-elb-load-balancer-name'
lb_instances = %x( aws elb describe-instance-health --load-balancer-name #{LOADBALANCER} )
lb_instances = JSON.parse(lb_instances)
instances = lb_instances["InstanceStates"]
ec2_cmd = "aws ec2 describe-instances --instance-ids "
instances.each do |data|
ec2_cmd += data['InstanceId'] + " ";
end
ec2_instances = %x( #{ec2_cmd} )
ec2_instances = JSON.parse(ec2_instances)
available_ips = Array.new
parse_instances = Array.new
if ec2_instances.has_key? 'Reservations'
reservations = ec2_instances["Reservations"]
reservations.each do |data|
data['Instances'].each do |instance_data|
server instance_data["PublicIpAddress"], user: 'ubuntu', roles: %w{web app db}, port: 22 #change to PrivateIpAddress if using access VPN
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment