Skip to content

Instantly share code, notes, and snippets.

@akira345
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akira345/f1aa6cc6c423345addbe to your computer and use it in GitHub Desktop.
Save akira345/f1aa6cc6c423345addbe to your computer and use it in GitHub Desktop.
modify_EC2_InstanceType
# -*- coding: utf-8 -*-
#
# http://buta9999.hatenablog.jp/entry/2014/01/30/010351を参考に作成
require 'aws-sdk'
require 'yaml'
require 'pp'
require "optparse"
opts = OptionParser.new
ec2_instance_id = nil
ec2_type = nil
opts.on("-i","--instance_id INSTANCE_ID") do |instance_id|
ec2_instance_id = instance_id
end
opts.on("-t","--type INSTANCE_TYPE") do |type|
ec2_type = type
end
opts.parse!(ARGV)
instance_types=%w{ m3.medium m3.large m3.xlarge m3.2xlarge
m1.small m1.medium m1.large m1.xlarge
c3.large c3.xlarge c3.2xlarge c3.4xlarge c3.8xlarge
c1.medium c1.xlarge cc2.8xlarge g2.2xlarge cg1.4xlarge
m2.xlarge m2.2xlarge m2.4xlarge cr1.8xlarge
i2.xlarge i2.2xlarge i2.4xlarge i2.8xlarge hs1.8xlarge
hi1.4xlarge t1.micro
}
#pp instance_types
#pp ec2_instance_id
#pp ec2_type
raise "Option instance_id Required!" if (ec2_instance_id == nil)
raise "Option instance_type Required!" if (ec2_type == nil)
if !instance_types.include?(ec2_type)
pp "Unknown Instance Type"
exit 1
end
config=YAML.load(File.read("config.yml"))
AWS.config(config)
ec2_region = "ec2.ap-northeast-1.amazonaws.com"
ec2 = AWS::EC2.new(
:ec2_endpoint => ec2_region
)
instance = ec2.instances["#{ec2_instance_id}"]
if !instance.exists?
pp "Instance NotFound!!"
exit 1
end
if instance.instance_type == ec2_type
exit 0
end
eip = instance.ip_address
pp "EIP:#{eip}"
pp "start"
if instance.status == :running
pp "stopping..."
instance.stop
while instance.status != :stopped
sleep(2)
end
end
if instance.status == :stopped
pp "modify instance"
ec2.client.modify_instance_attribute(:instance_id => "#{ec2_instance_id}",
:attribute => "instanceType",
:value => "#{ec2_type}")
instance.start
while instance.status != :running
sleep(2)
end
end
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment