You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.

Class: AWS::EC2::SubnetCollection

Inherits:
Collection show all
Includes:
Core::Collection::Simple, TaggedCollection
Defined in:
lib/aws/ec2/subnet_collection.rb

Overview

Represents a collection of VPC subnets. You can get a subnet collection two ways. You can get a collection that represents ALL subnets (across all your VPCs). You can also get a subnet collection that represents subnets within a single vpc.

# represents all subnets
subnets = ec2.subnets

# represents subnets within the named vpc
subnets = ec2.vpcs['vpc-12345'].subnets

Creating a Subnet

To create a subnet, call #create on a subnet collection, passing in a suitable CIDR block.

subnet = subnets.create('10.0.0.0/20')

You can optionally pass the availability zone you want the subnet created in.

Getting a Subnet

If you know the subnet id, you can get a subnet using #[].

subnet = subnets['subnet-id-here']

You can filter subnets as well. See the EC2 API documentation (http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSubnets.html) for a complete list of accepted filters.

subnet = subnets.filter('state', 'available').first

Instance Method Summary collapse

Methods included from Core::Collection

#each, #each_batch, #enum, #first, #in_groups_of, #page

Methods included from TaggedCollection

#tagged, #tagged_values, #with_tag

Methods included from FilteredCollection

#filter, #initialize

Instance Method Details

#[](subnet_id) ⇒ Subnet

Returns a subnet with the given id.

Parameters:

  • subnet_id (String)

Returns:

  • (Subnet)

    Returns a subnet with the given id.



93
94
95
# File 'lib/aws/ec2/subnet_collection.rb', line 93

def [] subnet_id
  Subnet.new(subnet_id, :config => config)
end

#create(cidr_block, options = {}) ⇒ Subnet

Creates a Subnet. Subnets require a valid CIDR block and are created inside an existing VPC. If you do not set an AvailabilityZone, then Amazon EC2 will select one for you (this is recommended).

For complete information about creating subnets, see CreateSubnet in the Amazon EC2 API Reference.

Parameters:

  • cidr_block (String)

    The CIDR block you want the subnet to cover (e.g., 10.0.0.0/24).

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :vpc (VPC, String)

    The VPC (or VPC id string) to create the subnet in.

  • :availability_zone (String, AvailabilityZone)

    The Availability Zone you want the subnet in. AWS selects a default zone for you (recommended).

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/aws/ec2/subnet_collection.rb', line 76

def create cidr_block, options = {}

  client_opts = {}
  client_opts[:vpc_id] = vpc_id_option(options)
  client_opts[:cidr_block] = cidr_block
  client_opts[:availability_zone] = az_option(options) if
    options[:availability_zone]

  resp = client.create_subnet(client_opts)

  Subnet.new_from(:create_subnet, resp.subnet,
    resp.subnet.subnet_id, :config => config)

end