Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
Quick Launch Amazon Linux
#!/usr/bin/env ruby
# Quickly launch Amazon Linux EC2 instance in Default VPC.
# Required to be set the below environment variables
# Usage : ruby launch.rb
require 'aws-sdk'
# security group id to be attached to the instance
secgroup_id = "sg-xxxxxxx"
# KeyPair name to be used
keypair_name = "keypair"
ec2 =
image_id = ""
AWS.memoize do
image_id = ec2.images.with_owner('amazon')
.filter("name", "amzn-ami-hvm-*-ebs")
.sort{|a, b| <=> }
resp = ec2.instances.create(image_id: image_id,
security_group_ids: [secgroup_id],
key_pair: ec2.key_pairs[keypair_name],
instance_type: "t2.micro",
associate_public_ip_address: true)
puts "Launched instance : #{}"
puts "waiting for acquiring Public IP Address..."
# wait until Public IP Address is assigned to the launched instance.
i = 1
while ec2.instances[].ip_address.nil?
if i > 10
raise RuntimeError
sleep 2 ** i
i += 1
puts "Got IP Address. You can connect via 'ssh -i ~/.ssh/#{keypair_name}.pem ec2-user@#{resp.ip_address}' in a few moments."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment