Creating an Internet Gateway and Attaching It to a VPC in Amazon EC2 - AWS SDK for Ruby

Creating an Internet Gateway and Attaching It to a VPC in Amazon EC2

The following code example creates an internet gateway and then attaches it to a virtual private cloud (VPC) in Amazon Virtual Private Cloud (Amazon VPC).

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX - License - Identifier: Apache - 2.0 require 'aws-sdk-ec2' # Creates an internet gateway and then attaches it to a virtual private cloud # (VPC) in Amazon Virtual Private Cloud (Amazon VPC). # # 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-east-1'), # 'vpc-6713dfEX' # ) 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 # Full example call: def run_me vpc_id = '' tag_key = '' tag_value = '' region = '' # Print usage information and then stop. if ARGV[0] == '--help' || ARGV[0] == '-h' puts 'Usage: ruby ec2-ruby-example-attach-igw-vpc.rb ' \ 'VPC_ID TAG_KEY TAG_VALUE REGION' puts 'Example: ruby ec2-ruby-example-attach-igw-vpc.rb ' \ 'vpc-6713dfEX my-key my-value us-east-1' exit 1 # If no values are specified at the command prompt, use these default values. elsif ARGV.count.zero? vpc_id = 'vpc-6713dfEX' tag_key = 'my-key' tag_value = 'my-value' region = 'us-east-1' # Otherwise, use the values as specified at the command prompt. else vpc_id = ARGV[0] tag_key = ARGV[1] tag_value = ARGV[2] region = ARGV[3] end ec2_resource = Aws::EC2::Resource.new(region: region) if internet_gateway_created_and_attached?( ec2_resource, vpc_id, tag_key, tag_value ) puts "Created and attached internet gateway to VPC '#{vpc_id}'." else puts "Could not create or attach internet gateway to VPC '#{vpc_id}'." end end run_me if $PROGRAM_NAME == __FILE__