Skip to content

Instantly share code, notes, and snippets.

@s-tajima

s-tajima/failover.rb

Created Nov 30, 2013
Embed
What would you like to do?
Check DNS Failover, and exec promote read replica.
#!/usr/bin/env ruby
require 'rubygems'
require 'aws-sdk'
require 'resolv'
require 'pp'
$target_fqdn = 'target-fqen.example.com'
$target_elb = 'target-elb-XXXXXX.ap-northeast-1.elb.amazonaws.com'
$access_key_id = 'YOUR ACCESS KEY ID'
$secret_access_key = 'YOUR SECRET ACCESS KEY'
$failover_region = 'ap-southeast-1'
$failover_replica = 'drtest-apse'
fqdn_ip = Resolv.getaddress($target_fqdn)
elb_ip = Resolv.getaddress($target_elb)
if fqdn_ip == elb_ip
puts "[#{Time.now.strftime("%Y/%m/%d %H:%M:%S")}] Status OK."
exit
end
rds = AWS::RDS.new(
:access_key_id => $access_key_id,
:secret_access_key => $secret_access_key,
:region => $failover_region
).client
rds.promote_read_replica({:db_instance_identifier => $failover_replica})
warn 'Failover occurd.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment