Studio3104::BLOG.new

uninitialized constant Studio3104 (NameError)

(AWS|Ruby)初学者がSDKでインスタンスを作ってみる


最近はちょっとした検証のたびにAWSインスタンスを作って、終わったらTerminateするというバイキンマンメソッドをふんだんに活用しております。
でも毎回マネジメントコンソールにログインしてぽちぽちするのはcoolじゃないよねー。
ってことでAWS SDK for Rubyを使ってさくっとインスタンスを作っちゃったりしてます。

サンプル

access keyなどを設定しましょう。YAMLでこんな感じで。
この例ではシンガポールリージョンを指定していますが、別のリージョンを使う場合はココを参照して別のエンドポイント名を指定します。

access_key_id: YOUR ACCESS KEY
secret_access_key: YOUR SECRET ACCESS KEY
ec2_endpoint : ec2.ap-southeast-1.amazonaws.com
view raw keys.yml hosted with ❤ by GitHub

設定が出来たら実際にインスタンスを作ってみます。
セキュリティグループはdefault、キーペアは割り当てられないシンプル構成。

#!/usr/bin/env ruby
require 'aws-sdk'
# 環境変数$http_proxyをスクリプト内で有効にする
AWS.config(:proxy_uri => ENV['http_proxy'])
# 同一ディレクトリにあるYAMLを読み込む。
config_path = File.expand_path(File.dirname(__FILE__)+"/keys.yml")
AWS.config(YAML.load(File.read(config_path)))
# 最新のAmazonLinuxのAMI IDを取得
image = AWS.memoize do
amazon_linux = ec2.images.with_owner("amazon").
filter("root-device-type", "ebs").
filter("architecture", "x86_64").
filter("name", "amzn-ami*")
amazon_linux.to_a.sort_by(&:name).last
end
# t1.microのインスタンスを作って、’test'という名前を付ける
ec2 = AWS::EC2.new
instance = ec2.instances.create(
:image_id => image.id,
:instance_type => 't1.micro'
)
instance.tags.Name = "test"

かんたんですね!!
あとは、VPCの中に作りたい場合はこんな感じで。
セキュリティグループとキーペアはなければ作りますが、VPCは予め作っておいてVPC IDなどをハードコーディングしなければならないイケてない実装ですが、まぁ参考になれば是非。。
冗長なのであくまで参考程度にご覧ください!!

#!/usr/bin/env ruby
require 'aws-sdk'
AWS.config(:proxy_uri => ENV['http_proxy'])
config_path = File.expand_path(File.dirname(__FILE__)+"/keys.yml")
AWS.config(YAML.load(File.read(config_path)))
ec2 = AWS::EC2.new
key_name = security_group_name = 'develEnv'
allow_from = YOUR NETWORK CIDR
vpc_info = {
:vpc_id => YOUR VPC ID,
:public_subnet_id => YOUR PUBLIC SUBNET ID,
:private_subnet_id => YOUR PRIVATE SUBNET ID,
}
# get ID of latest AmazonLinux AMI
image = AWS.memoize do
amazon_linux = ec2.images.with_owner("amazon").
filter("root-device-type", "ebs").
filter("architecture", "x86_64").
filter("name", "amzn-ami*")
amazon_linux.to_a.sort_by(&:name).last
end
# Created if key pair does not exist
if AWS::EC2::KeyPair.new(key_name).exists?
puts "key pair already exists, \"#{key_name}\""
else
kp = ec2.key_pairs.create(key_name)
private_key_file = open("./id_rsa_#{key_name}", 'w+')
private_key_file.write(kp.private_key)
private_key_file.close
puts "created key pair, \"#{key_name}\""
end
private_key_file = open("./id_rsa_#{key_name}", 'r')
p private_key_file.read
# Created if security group does not exist
if ec2.security_groups.filter('group-name', security_group_name).first
puts "security group already exists, \"#{security_group_name}\""
else
group = ec2.security_groups.create(security_group_name, {:vpc_id => vpc_info[:vpc_id]})
group.authorize_ingress(:tcp, 22, allow_from)
group.authorize_ingress(:tcp, 80, allow_from)
puts "created security group, \"#{security_group_name}\""
end
instance = ec2.instances.create(
# :count => 2,
:image_id => image.id,
:key_name => key_name,
:subnet_id => vpc_info[:private_subnet_id],
:security_groups => security_group_name,
:private_ip_address => '10.0.1.5',
:instance_type => 't1.micro'
)
instance.tags.Name = "#{security_group_name}_template"

ドキュメントとソースを頑張って読みましょう!

もっと色々なことをやる場合はドキュメントは当然のことながら、ソースコードも頑張って読みましょう。
初学者には大変ツライですが頑張りましょう。
AWS SDK for Ruby



Enjoy your (AWS|Ruby) life!!