创建 Internet 网关并将其附加到 Amazon EC2 中的 VPC - 适用于 Ruby 的 AWS 开发工具包
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

创建 Internet 网关并将其附加到 Amazon EC2 中的 VPC

以下代码示例创建一个 Internet 网关,然后将其附加到 Amazon Virtual Private Cloud (Amazon VPC) 中的 Virtual Private Cloud (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__