Created
December 7, 2012 03:27
-
-
Save studio3104/4230516 to your computer and use it in GitHub Desktop.
for AWS&Ruby Beginners
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
#!/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" |
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
#!/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" |
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
access_key_id: YOUR ACCESS KEY | |
secret_access_key: YOUR SECRET ACCESS KEY | |
ec2_endpoint : ec2.ap-southeast-1.amazonaws.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment