Last active
February 8, 2016 03:46
-
-
Save akira345/8a35499288639b067c31 to your computer and use it in GitHub Desktop.
AWS-SDKでAMIからインスタンスを起動してEIP、セキュリティグループ割り当て等ひと通りやるメモ。AWS SDK for Ruby V1使用。動くRubyスクリプトではないので注意。
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
# | |
# このあたりも参照。 | |
# https://gist.github.com/taka328w/1615935 | |
# | |
#展開するインスタンスのベースになるAMI | |
base_image_id = "" | |
#インスタンスタイプ | |
instance_type = "" | |
#VPCに所属するサブネットID | |
subnet_id = "" | |
#VPCのID | |
vpc_id = "" | |
#インスタンスに割り当てるプライベートIP | |
private_ip = "" | |
#インスタンスに割り当てるEIP | |
public_ip = "" | |
#マウントポイント | |
device = "" | |
#ボリュームサイズ | |
volume_size = 30 | |
#ゾーン | |
availability_zone = "ap-northeast-1c" | |
#SSHキーペア名 | |
ssh_keypair_name = "" | |
#割り当てるセキュリティグループ名 | |
ary_group = ["default","web"] | |
#リージョン | |
ec2_region = "ec2.ap-northeast-1.amazonaws.com" | |
#もろもろ省略 | |
ec2 = AWS::EC2.new( | |
:ec2_endpoint => ec2_region | |
) | |
#インスタンス作成 | |
new_instance = ec2.instances.create({ | |
:image_id => base_image_id, | |
:instance_type => instance_type, | |
:count =>1, | |
:subnet => subnet_id, | |
:private_ip_address => private_ip, | |
:block_device_mappings => [{ | |
:device_name => device, | |
:ebs => { | |
:volume_size => volume_size, | |
:delete_on_termination => true, | |
:volume_type => "standard" | |
} | |
}], | |
:security_group_ids => sg(ec2,vpc_id,ary_group), | |
:availability_zone => availability_zone, | |
:key_pair => ec2.key_pairs[ssh_keypair_name], | |
:disable_api_termination => true | |
}) | |
pp "wait..." | |
sleep 10 while new_instance.status == :pending | |
# EIP付与 | |
new_instance.associate_elastic_ip(eip(ec2,public_ip)) | |
pp "OK" | |
def sg(ec2,vpc_id, *names) | |
names = [names].flatten | |
tmp = [] | |
ec2.security_groups.select {|s| s.vpc? && s.vpc.id == vpc_id && names.include?(s.name) }.each {|d| | |
tmp.push(d.id) | |
} | |
return tmp.to_a | |
end | |
def eip(ec2,public_ip) | |
tmp = "" | |
ec2.elastic_ips.select {|i| i.public_ip.include?(public_ip) }.each {|d| | |
tmp = d.allocation_id | |
} | |
return tmp.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment