ruby/example_code/ec2/ec2-ruby-example-create-subnet.rb
# Creates a subnet within a virtual private cloud (VPC) in
# Amazon Virtual Private Cloud (Amazon VPC) and then tags
# the subnet.
#
# Prerequisites:
#
# - A VPC in Amazon VPC.
#
# @param ec2_resource [Aws::EC2::Resource] An initialized
#   Amazon Elastic Compute Cloud (Amazon EC2) resource object.
# @param vpc_id [String] The ID of the VPC for the subnet.
# @param cidr_block [String] The IPv4 CIDR block for the subnet.
# @param availability_zone [String] The ID of the Availability Zone
#   for the subnet.
# @param tag_key [String] The key portion of the tag for the subnet.
# @param tag_vlue [String] The value portion of the tag for the subnet.
# @return [Boolean] true if the subnet was created and tagged;
#   otherwise, false.
# @example
#   exit 1 unless subnet_created_and_tagged?(
#     Aws::EC2::Resource.new(region: 'us-west-2'),
#     'vpc-6713dfEX',
#     '10.0.0.0/24',
#     'us-west-2a',
#     'my-key',
#     'my-value'
#   )

Creates a subnet within a virtual private cloud (VPC) in
Amazon Virtual Private Cloud (Amazon VPC) and then tags
the subnet.



A VPC in Amazon VPC.
Creates a subnet within a virtual private cloud ( VPC ) in Amazon Virtual Private Cloud ( Amazon VPC ) and then tags the subnet . A VPC in Amazon VPC .
def subnet_created_and_tagged?(
  ec2_resource,
  vpc_id,
  cidr_block,
  availability_zone,
  tag_key,
  tag_value
)
  subnet = ec2_resource.create_subnet(
    vpc_id: vpc_id,
    cidr_block: cidr_block,
    availability_zone: availability_zone
  )
  subnet.create_tags(
    tags: [
      {
        key: tag_key,
        value: tag_value
      }
    ]
  )
  puts "Subnet created with ID '#{subnet.id}' in VPC with ID '#{vpc_id}' " \
    "and CIDR block '#{cidr_block}' in availability zone " \
    "'#{availability_zone}' and tagged with key '#{tag_key}' and " \
    "value '#{tag_value}'."
  return true
rescue StandardError => e
  puts "Error creating or tagging subnet: #{e.message}"
  return false
end
def subnet_created_and_tagged? ( ec2_resource , vpc_id , cidr_block , availability_zone , tag_key , tag_value ) subnet = ec2_resource . create_subnet ( vpc_id : vpc_id , cidr_block : cidr_block , availability_zone : availability_zone ) subnet . create_tags ( tags : [ { key : tag_key , value : tag_value } ] ) puts "Subnet created with ID '#{subnet.id}' in VPC with ID '#{vpc_id}' " "and CIDR block '#{cidr_block}' in availability zone " "'#{availability_zone}' and tagged with key '#{tag_key}' and " "value '#{tag_value}'." return true rescue StandardError => e puts "Error creating or tagging subnet: #{e.message}" return false end
Creates a subnet within a virtual private cloud (VPC) in
Amazon Virtual Private Cloud (Amazon VPC) and then tags
the subnet.
Creates a subnet within a virtual private cloud ( VPC ) in Amazon Virtual Private Cloud ( Amazon VPC ) and then tags the subnet .
true if the subnet was created and tagged; otherwise, false.
true if the subnet was created and tagged ; otherwise false .
Boolean
ec2_resource
An initialized Amazon Elastic Compute Cloud (Amazon EC2) resource object.
An initialized Amazon Elastic Compute Cloud ( Amazon EC2 ) resource object .
vpc_id
The ID of the VPC for the subnet.
The ID of the VPC for the subnet .
cidr_block
The IPv4 CIDR block for the subnet.
The IPv4 CIDR block for the subnet .
availability_zone
The ID of the Availability Zone for the subnet.
The ID of the Availability Zone for the subnet .
tag_key
The key portion of the tag for the subnet.
The key portion of the tag for the subnet .
tag_value
tag_vlue
The value portion of the tag for the subnet.
The value portion of the tag for the subnet .
example
None