Last active
December 22, 2015 13:49
-
-
Save mikeda/6481632 to your computer and use it in GitHub Desktop.
AWS SDK for RubyでEC2インスタンスを作成するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# EC2のインスタンスを作成 | |
# VPC | |
# インスタンスはEBSタイプ | |
# 追加EBSをcreate & attach & mount | |
# EIPをallocate & associate | |
# インスタンス、EBSボリュームにはNameタグをつける | |
# 使用例 | |
# $ ruby run_instance.rb app01 m1.small 10.0.0.101 30 | |
require 'aws-sdk' | |
require 'pp' | |
aws_config = { | |
access_key_id: 'xxxxx', | |
secret_access_key: 'xxxxx', | |
region: 'ap-northeast-1' | |
} | |
if ARGV.size != 4 | |
puts "usage: #{$0} <hostname> <instance_type> <private_ip_address> <ebs_size>" | |
exit 1 | |
end | |
hostname, instance_type, private_ip_address, ebs_size = ARGV | |
ebs_size = ebs_size.to_i | |
key_name = 'mikeda_aws' | |
image_id = 'ami-5f840e5e' # Amazon Linux x86_64 | |
vpc = 'vpc-4c6f2825' | |
subnet = 'subnet-456f282c' | |
security_group_ids = [ 'sg-d65d42ba' ] # only 'default' | |
ebs_device = '/dev/sdb' | |
user_data = <<EOS | |
#!/bin/bash | |
sed -i "s/HOSTNAME=.*/HOSTNAME=#{hostname}/" /etc/sysconfig/network | |
mkfs.ext4 #{ebs_device} | |
mkdir /data | |
echo '#{ebs_device} /data ext4 defaults 0 0' >> /etc/fstab | |
reboot | |
EOS | |
AWS.config(aws_config) | |
ec2 = AWS::EC2.new | |
### インスタンス作成 | |
puts "create instance" | |
instance = ec2.instances.create( | |
image_id: image_id, | |
instance_type: instance_type, | |
key_name: key_name, | |
subnet: subnet, | |
security_group_ids: security_group_ids, | |
private_ip_address: private_ip_address, | |
user_data: user_data, | |
block_device_mappings: [ | |
{ | |
device_name: ebs_device, | |
ebs: { | |
volume_size: ebs_size, | |
} | |
} | |
] | |
) | |
while instance.status != :running | |
puts "Launching instance #{instance.id}, status: #{instance.status}" | |
sleep 10 | |
end | |
### EIPのAllocateとAssociate | |
puts "create elastic_ip" | |
elastic_ip = ec2.elastic_ips.create(vpc: vpc) | |
# なんかエラーになるのでちょっとsleep | |
sleep 5 | |
puts "associate elastic_ip : #{elastic_ip.ip_address}" | |
instance.associate_elastic_ip(elastic_ip) | |
### タグ設定 | |
root_volume = instance.attachments['/dev/sda1'].volume | |
data_volume = instance.attachments[ebs_device].volume | |
ec2.tags.create(instance, 'Name', value: hostname) | |
ec2.tags.create(root_volume, 'Name', value: "#{hostname}_root") | |
ec2.tags.create(data_volume, 'Name', value: "#{hostname}_data") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# EC2のインスタンスを作成 | |
# VPC | |
# インスタンスはEBSタイプ | |
# 追加EBSをcreate & attach | |
# EIPをallocate & associate & mount | |
# インスタンス、EBSボリュームにはNameタグをつける | |
# 使用例 | |
# $ ruby run_instance.rb app01 m1.small 10.0.0.101 30 | |
require 'aws-sdk' | |
require 'pp' | |
aws_config = { | |
access_key_id: 'xxxxxx', | |
secret_access_key: 'xxxxxx', | |
region: 'ap-northeast-1' | |
} | |
if ARGV.size != 4 | |
puts "usage: #{$0} <hostname> <instance_type> <private_ip_address> <ebs_size>" | |
exit 1 | |
end | |
hostname, instance_type, private_ip_address, ebs_size = ARGV | |
ebs_size = ebs_size.to_i | |
key_name = 'mikeda_aws' | |
image_id = 'ami-5f840e5e' # Amazon Linux x86_64 | |
vpc = 'vpc-4c6f2825' | |
subnet = 'subnet-456f282c' | |
security_group_ids = [ 'sg-d65d42ba', 'sg-b13bdade' ] # only 'default' | |
ebs_device = '/dev/sdb' | |
user_data = <<EOS | |
#!/bin/bash | |
sed -i "s/HOSTNAME=.*/HOSTNAME=#{hostname}/" /etc/sysconfig/network | |
mkfs.ext4 #{ebs_device} | |
mkdir /data | |
echo '#{ebs_device} /data ext4 defaults 0 0' >> /etc/fstab | |
reboot | |
EOS | |
AWS.config(aws_config) | |
ec2 = AWS::EC2.new | |
### インスタンス作成 | |
puts "create instance" | |
instance = ec2.instances.create( | |
image_id: image_id, | |
instance_type: instance_type, | |
key_name: key_name, | |
user_data: user_data, | |
block_device_mappings: [ | |
{ | |
device_name: ebs_device, | |
ebs: { | |
volume_size: ebs_size, | |
} | |
} | |
], | |
network_interfaces: [ | |
{ | |
device_index: 0, | |
subnet_id: subnet, | |
private_ip_address: private_ip_address, | |
associate_public_ip_address: true, | |
groups: security_group_ids | |
} | |
] | |
) | |
while instance.status != :running | |
puts "Launching instance #{instance.id}, status: #{instance.status}" | |
sleep 10 | |
end | |
### タグ設定 | |
root_volume = instance.attachments['/dev/sda1'].volume | |
data_volume = instance.attachments[ebs_device].volume | |
ec2.tags.create(instance, 'Name', value: hostname) | |
ec2.tags.create(root_volume, 'Name', value: "#{hostname}_root") | |
ec2.tags.create(data_volume, 'Name', value: "#{hostname}_data") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment