class AWS::EC2
:name => “my-image”)
ec2.images.create(:image_location => “mybucket/manifest.xml”,
You can also use the images collection to create new images:
ec2.images.with_owner(“amazon”).map(&:name)
the images available to your account. For example:
You can use the {#images} collection to get information about
application).
to act as a Hadoop node (e.g., Linux, Hadoop, and a custom
Apache, and your web site) or it might contain all the software
contain all the software to act as a web server (e.g., Linux,
to boot instances of your software. For example, an AMI might
An Amazon Machine Image (AMI) contains all information necessary
== Images
ec2.availability_zones.map(&:name) # => [“us-east-1a”, …]
example:
about the available zones available to your account. For
You can use the {#availability_zones} collection to get information
failure of a single location.
Availability Zones, you can protect your applications from the
zones in the same Region. By launching instances in separate
provide inexpensive, low-latency network connectivity to other
isolated from failures in other Availability zones and to
Availability Zones. Each Availability Zone is engineered to be
Each Region contains multiple distinct locations called
== Availability Zones
“ec2.us-west-1.amazonaws.com”)
ec2 = AWS::EC2.new(:ec2_endpoint =>
like this:
different endpoint without making a call to EC2, you can do it
endpoint for “us-west-1” – if you just want to configure a
This makes a call to EC2’s DescribeRegions API to find the
ec2_us_west.instances.create(:image_id => ‘ami-3bc9997e’)
# starts an instance in eu-west-1
ec2_us_west = ec2.regions[“us-west-1”]
like this:
The default region is us-east-1
; you can access other regions
ec2.regions.map(&:name) # => [“us-east-1”, …]
account:
the Ruby SDK to see which regions are available for your
Pricing page}[aws.amazon.com/ec2/pricing]). You can use
more information about pricing by Region, go to the {Amazon EC2
requirements. Prices for Amazon EC2 usage vary by Region (for
to specific customers or to meet legal or other
separate Regions, you can design your application to be closer
available to use in different Regions. By launching instances in
North America, Europe, Asia, etc.). Correspondingly, EC2 is
Amazon has data centers in different areas of the world (e.g.,
== Regions
ec2.images.tagged(“myapp”) # will include ami-123<br>ec2.images.tags << “myapp”
filter on that metadata. For example:
snapshots, and volumes with free-form key-value metadata and
Similarly, you can tag images, instances, security groups,
filter(“description”, “linux”)
filter(“platform”, “windows”).
ec2.images.with_owner(“amazon”).
the string “linux”, you can do this:
windows images owned by amazon where the description includes
number of different parameters. For example, to get all the
Any of the collections in the interface may be filtered by a
== Filtering and Tagging
end
f.write(key_pair.private_key)
File.open(“~/.ssh/ec2”, “w”) do |f|
key_pair = ec2.key_pairs.create(“mykey”)
example:
You can also ask EC2 to generate a key pair for you. For
ec2.key_pairs.import(“mykey”, File.read(“~/.ssh/identity.pub”))
key_pair =
part to EC2 using {KeyPairCollection#import}. For example:
You can generate a key pair yourself and then send the public
the private key to log in securely without a password.
of this pair is embedded in your instance, allowing you to use
public/private key pair to log in to them. The public key half
Public Amazon Machine Image (AMI) instances have no password, and you need a
== Key Pairs
ec2.select{|ip| !ip.associated? }.each(&:release)
not currently associated with an instance:
In the following example we release all elastic IP addresses that are
When you are done with an elastic IP address you should release it.
instance.ip_address # 1.1.1.1
instance.disassociate_elastic_ip
instance.ip_address # 2.2.2.2
instance.associate_elastic_ip(ip)
ip.ip_address # 2.2.2.2
instance.ip_address # 1.1.1.1
ip = ec2.elastic_ips.allocate
instance = ec2.instances[‘i-12345678’]
You can associate those elastic IP addresses with EC2 instances:
You can allocate up to 5 elastic IP addresses for each account.
== Elastic IPs
You can specify other protocols than :tcp
, like :udp and :icmp.
{SecurityGroup#revoke_ingress} and {SecurityGroup#disallow_ping}.
You can also use the same parameters from the examples above to
of CIDR IP addresses to allow ping to limit where you can ping from.
Just like with authorize_ingress you can pass a security group or a list
wbsvrs.allow_ping
There are a few handy shortcuts for allowing pings:
dbsvrs.authorize_ingress(:tcp, 3306, websvrs)
# to instances in the ‘db-servers’ security group over tcp port 3306
# allow instances in the ‘web-servers’ security group to connect
websvrs = ec2.security_groups.filter(‘group-name’, ‘web-servers’).first
dbsvrs = ec2.security_groups.filter(‘group-name’, ‘db-servers’).first
# get two existing security groups
group(s).
This allows incoming traffic from EC2 instances in the given security
You can also provide another security group instead of CIDR IP addresses.
websrvr.authorize_ingress(:tcp, 22, ‘1.1.1.1/0’, ‘2.2.2.2/0’)
# ssh access
list of address, just add them to the #authorize_ingress call.
If you want to limit an authorization to a particular CIDR IP address or
websvr.authorize_ingress(:tcp, 20..21)
# ftp traffic
You can also specify a port range. Here we are opening FTP traffic:
websvr.authorize_ingress(:tcp, 80)
# web traffic
we add a rule that allows web traffic from the entire internet.
Then you can add ingress authorizations. In the following example
websvr = ec2.security_groups.create(‘webservers’)
To create a security group:
delivered to your instance. All other ingress traffic will be discarded.
rules specify which ingress (i.e., incoming) network traffic should be
A security group is a named collection of access rules. These access
== Security Groups
# => { “i-12345678” => :running, “i-87654321” => :shutting_down }
ec2.instances.inject({}) { |m, i| m = i.status; m }
To get a list of instances:
i.exists?
i = ec2.instances[“i-12345678”]
To get an instance by ID:
ec2.instances.create(:image_id => “ami-8c1fece5”)
To run an instance:
EC2 uses instances to run your software.
== Instances
:secret_access_key => ‘YOUR_SECRET_ACCESS_KEY’)
:access_key_id => ‘YOUR_ACCESS_KEY_ID’,
ec2 = AWS::EC2.new(
Or you can set them directly on the EC2 interface:
:secret_access_key => ‘YOUR_SECRET_ACCESS_KEY’)
:access_key_id => ‘YOUR_ACCESS_KEY_ID’,
AWS.config(
AWS.config:
You can setup default credentials for all AWS services via
== Credentials
Provides an expressive, object-oriented interface to Amazon EC2.
def availability_zones
-
(AvailabilityZoneCollection)
- A collection representing
def availability_zones AvailabilityZoneCollection.new(:config => config) end
def elastic_ips
-
(ElasticIpCollection)
- A collection representing all
def elastic_ips ElasticIpCollection.new(:config => config) end
def images
-
(ImageCollection)
- A collection representing
def images ImageCollection.new(:config => config) end
def instances
-
(InstanceCollection)
- A collection representing all instances
def instances InstanceCollection.new(:config => config) end
def key_pairs
-
(KeyPairCollection)
- A collection representing all key pairs.
def key_pairs KeyPairCollection.new(:config => config) end
def regions
-
(RegionCollection)
- A collection representing all EC2
def regions RegionCollection.new(:config => config) end
def reserved_instances
-
(ReservedInstancesCollection)
- A collection representing all
def reserved_instances ReservedInstancesCollection.new(:config => config) end
def reserved_instances_offerings
-
(ReservedInstancesOfferingCollection)
- A collection representing all
def reserved_instances_offerings ReservedInstancesOfferingCollection.new(:config => config) end
def security_groups
-
(SecurityGroupCollection)
- A collection representing all security
def security_groups SecurityGroupCollection.new(:config => config) end
def snapshots
-
(SnapshotCollection)
- A collection representing
def snapshots SnapshotCollection.new(:config => config) end
def tags
-
(TagCollection)
- A collection representing all EC2 tags for
def tags TagCollection.new(:config => config) end
def volumes
-
(VolumeCollection)
- A collection representing
def volumes VolumeCollection.new(:config => config) end