在 Amazon Virtual Private Cloud(Amazon VPC)中创建和标记子网
以下代码示例显示如何创建 Amazon EC2 子网。
- Ruby
-
- SDK for Ruby
-
注意
在 GitHub 上查看更多内容。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 require "aws-sdk-ec2" # 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' # ) 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 # Full example call: def run_me vpc_id = "" cidr_block = "" availability_zone = "" tag_key = "" tag_value = "" region = "" # Print usage information and then stop. if ARGV[0] == "--help" || ARGV[0] == "-h" puts "Usage: ruby ec2-ruby-example-create-subnet.rb " \ "VPC_ID CIDR_BLOCK AVAILABILITY_ZONE TAG_KEY TAG_VALUE REGION" # Replace us-west-2 with the AWS Region you're using for Amazon EC2. puts "Example: ruby ec2-ruby-example-create-subnet.rb " \ "vpc-6713dfEX 10.0.0.0/24 us-west-2a my-key my-value us-west-2" exit 1 # If no values are specified at the command prompt, use these default values. elsif ARGV.count.zero? vpc_id = "vpc-6713dfEX" cidr_block = "10.0.0.0/24" availability_zone = "us-west-2a" tag_key = "my-key" tag_value = "my-value" # Replace us-west-2 with the AWS Region you're using for Amazon EC2. region = "us-west-2" # Otherwise, use the values as specified at the command prompt. else vpc_id = ARGV[0] cidr_block = ARGV[1] availability_zone = ARGV[2] tag_key = ARGV[3] tag_value = ARGV[4] region = ARGV[5] end ec2_resource = Aws::EC2::Resource.new(region: region) if subnet_created_and_tagged?( ec2_resource, vpc_id, cidr_block, availability_zone, tag_key, tag_value ) puts "Subnet created and tagged." else puts "Subnet not created or not tagged." end end run_me if $PROGRAM_NAME == __FILE__
-
有关 API 详细信息,请参阅《AWS SDK for Ruby API 参考》中的 CreatePolicy。
-
有关 AWS 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 Amazon EC2 与 AWS SDK 结合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。
创建安全密钥对
创建并运行实例