ruby/example_code/ec2/ec2-ruby-example-attach-igw-vpc.rb
# 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 to attach the internet gateway.
# @param tag_key [String] The key of the tag to attach to the internet gateway.
# @param tag_value [String] The value of the tag to attach to the
#   internet gateway.
# @return [Boolean] true if the internet gateway was created and attached;
#   otherwise, false.
# @example
#   exit 1 unless internet_gateway_created_and_attached?(
#     Aws::EC2::Resource.new(region: 'us-west-2'),
#     'vpc-6713dfEX'
#   )

A VPC in Amazon VPC.
A VPC in Amazon VPC .
def internet_gateway_created_and_attached?(
  ec2_resource,
  vpc_id,
  tag_key,
  tag_value
)
  igw = ec2_resource.create_internet_gateway
  puts "The internet gateway's ID is '#{igw.id}'."
  igw.attach_to_vpc(vpc_id: vpc_id)
  igw.create_tags(
    tags: [
      {
        key: tag_key,
        value: tag_value
      }
    ]
  )
  return true
rescue StandardError => e
  puts "Error creating or attaching internet gateway: #{e.message}"
  puts 'If the internet gateway was created but not attached, you should ' \
    'clean up by deleting the internet gateway.'
  return false
end
def internet_gateway_created_and_attached? ( ec2_resource , vpc_id , tag_key , tag_value ) igw = ec2_resource . create_internet_gateway puts "The internet gateway's ID is '#{igw.id}'." igw . attach_to_vpc ( vpc_id : vpc_id ) igw . create_tags ( tags : [ { key : tag_key , value : tag_value } ] ) return true rescue StandardError => e puts "Error creating or attaching internet gateway: #{e.message}" puts 'If the internet gateway was created but not attached, you should ' 'clean up by deleting the internet gateway.' return false end
Prerequisites:
 A VPC in Amazon VPC.
Prerequisites : A VPC in Amazon VPC .
true if the internet gateway was created and attached; otherwise, false.
true if the internet gateway was created and attached ; 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 to attach the internet gateway.
The ID of the VPC to attach the internet gateway .
tag_key
The key of the tag to attach to the internet gateway.
The key of the tag to attach to the internet gateway .
tag_value
The value of the tag to attach to the internet gateway.
The value of the tag to attach to the internet gateway .
example
exit 1 unless internet_gateway_created_and_attached?( Aws::EC2::Resource.new(region: 'us-west-2'), 'vpc-6713dfEX' )
exit 1 unless internet_gateway_created_and_attached? ( Aws :: EC2 :: Resource . new ( region : ' us - west - 2 ' ) ' vpc - 6713dfEX ' )