Skip to content

Instantly share code, notes, and snippets.

@bortels
Created March 6, 2013 00:44
Show Gist options
  • Save bortels/5095772 to your computer and use it in GitHub Desktop.
Save bortels/5095772 to your computer and use it in GitHub Desktop.
example script to create AWS ec2 instance using ruby sdk
#!/usr/bin/ruby
require 'rubygems'
require 'aws-sdk'
require 'erb'
aws_email="email address here"
aws_name="My Test Instance"
# below is IAM credentials for my account
AWS.config(YAML.load(File.read('/Users/tom/aws/pcc')))
template = ERB.new File.read('/Users/tom/aws/userdata')
userdata = template.result(binding)
ec2 = AWS::EC2.new.regions['us-west-1']
inst = ec2.instances.create(
# :image_id => ec2.images.with_owner("508971454034").tagged('default').first.id, # our custom AMI
:image_id => 'ami-b63210f3', # latest AMI as of this writing
:instance_type => 'm1.small',
:key_name => 'my-aws-keypair',
:iam_instance_profile => 'iam-role', # delete this line if no role assigned to this instance
:user_data => userdata # delete this line of no userdata
)
puts "Waiting for new instance with id #{inst.id} to become available..."
sleep 1 while inst.status == :pending
puts "#{inst.dns_name} is ready"
inst.tags.Name = aws_name
inst.tags.email = aws_email
# TODO - add CNAME for new instance to my domain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment