文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
CreateVpc
搭配 a AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 CreateVpc
。
- CLI
-
- AWS CLI
-
範例 1:建立 VPC
下列
create-vpc
範例會建立具有指定 VPC 區塊的 CIDR IPv4 和名稱標籤。aws ec2 create-vpc \ --cidr-block
10.0.0.0/16
\ --tag-specificationsResourceType=vpc,Tags=[{Key=Name,Value=MyVpc}]
輸出:
{ "Vpc": { "CidrBlock": "10.0.0.0/16", "DhcpOptionsId": "dopt-5EXAMPLE", "State": "pending", "VpcId": "vpc-0a60eb65b4EXAMPLE", "OwnerId": "123456789012", "InstanceTenancy": "default", "Ipv6CidrBlockAssociationSet": [], "CidrBlockAssociationSet": [ { "AssociationId": "vpc-cidr-assoc-07501b79ecEXAMPLE", "CidrBlock": "10.0.0.0/16", "CidrBlockState": { "State": "associated" } } ], "IsDefault": false, "Tags": [ { "Key": "Name", "Value": MyVpc" } ] } }
範例 2:建立具有專用租用的 VPC
下列
create-vpc
範例會建立具有指定 VPC 區塊和專用租用的 IPv4 CIDR。aws ec2 create-vpc \ --cidr-block
10.0.0.0/16
\ --instance-tenancydedicated
輸出:
{ "Vpc": { "CidrBlock": "10.0.0.0/16", "DhcpOptionsId": "dopt-19edf471", "State": "pending", "VpcId": "vpc-0a53287fa4EXAMPLE", "OwnerId": "111122223333", "InstanceTenancy": "dedicated", "Ipv6CidrBlockAssociationSet": [], "CidrBlockAssociationSet": [ { "AssociationId": "vpc-cidr-assoc-00b24cc1c2EXAMPLE", "CidrBlock": "10.0.0.0/16", "CidrBlockState": { "State": "associated" } } ], "IsDefault": false } }
範例 3:使用 VPC CIDR區塊建立 IPv6
下列
create-vpc
範例會使用 Amazon 提供的 VPC IPv6 區塊建立 CIDR。aws ec2 create-vpc \ --cidr-block
10.0.0.0/16
\ --amazon-provided-ipv6-cidr-block輸出:
{ "Vpc": { "CidrBlock": "10.0.0.0/16", "DhcpOptionsId": "dopt-dEXAMPLE", "State": "pending", "VpcId": "vpc-0fc5e3406bEXAMPLE", "OwnerId": "123456789012", "InstanceTenancy": "default", "Ipv6CidrBlockAssociationSet": [ { "AssociationId": "vpc-cidr-assoc-068432c60bEXAMPLE", "Ipv6CidrBlock": "", "Ipv6CidrBlockState": { "State": "associating" }, "Ipv6Pool": "Amazon", "NetworkBorderGroup": "us-west-2" } ], "CidrBlockAssociationSet": [ { "AssociationId": "vpc-cidr-assoc-0669f8f9f5EXAMPLE", "CidrBlock": "10.0.0.0/16", "CidrBlockState": { "State": "associated" } } ], "IsDefault": false } }
範例 4:從 VPC 集區使用 CIDR 建立 IPAM
下列
create-vpc
範例會從 Amazon VPC IP Address Manager (VPC) 集區建立具有 CIDR 的 IPAM。Linux 和 macOS:
aws ec2 create-vpc \ --ipv4-ipam-pool-id
ipam-pool-0533048da7d823723
\ --tag-specifications ResourceType=vpc,Tags='[{Key=Environment,Value="Preprod"},{Key=Owner,Value="Build Team"}]'Windows:
aws ec2 create-vpc
^
--ipv4-ipam-pool-idipam-pool-0533048da7d823723
^
--tag-specifications ResourceType=vpc,Tags=[{Key=Environment,Value="Preprod"},{Key=Owner,Value="Build Team"}]輸出:
{ "Vpc": { "CidrBlock": "10.0.1.0/24", "DhcpOptionsId": "dopt-2afccf50", "State": "pending", "VpcId": "vpc-010e1791024eb0af9", "OwnerId": "123456789012", "InstanceTenancy": "default", "Ipv6CidrBlockAssociationSet": [], "CidrBlockAssociationSet": [ { "AssociationId": "vpc-cidr-assoc-0a77de1d803226d4b", "CidrBlock": "10.0.1.0/24", "CidrBlockState": { "State": "associated" } } ], "IsDefault": false, "Tags": [ { "Key": "Environment", "Value": "Preprod" }, { "Key": "Owner", "Value": "Build Team" } ] } }
如需詳細資訊,請參閱 Amazon VPC 使用者指南CIDR中的建立使用 IPAM 集區 Word 的 Word。 VPC IPAM
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateVpc
。
-
- PHP
-
- 適用於 PHP 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /** * @param string $cidr * @return array */ public function createVpc(string $cidr): array { try { $result = $this->ec2Client->createVpc([ "CidrBlock" => $cidr, ]); return $result['Vpc']; }catch(Ec2Exception $caught){ echo "There was a problem creating the VPC: {$caught->getAwsErrorMessage()}\n"; throw $caught; } }
-
如需 API 詳細資訊,請參閱 CreateVpc AWS SDK for PHP 參考中的 API。
-
- PowerShell
-
- for PowerShell 工具
-
範例 1:此範例會建立具有指定 VPC 的 CIDR。Amazon VPC 也會為 VPC 建立下列項目:預設 DHCP 選項集、主要路由表和預設網路 ACL。
New-EC2VPC -CidrBlock 10.0.0.0/16
輸出:
CidrBlock : 10.0.0.0/16 DhcpOptionsId : dopt-1a2b3c4d InstanceTenancy : default IsDefault : False State : pending Tags : {} VpcId : vpc-12345678
-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 CreateVpc。
-
- Ruby
-
- Ruby 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 require 'aws-sdk-ec2' # Creates a virtual private cloud (VPC) in # Amazon Virtual Private Cloud (Amazon VPC) and then tags # the VPC. # # @param ec2_resource [Aws::EC2::Resource] An initialized # Amazon Elastic Compute Cloud (Amazon EC2) resource object. # @param cidr_block [String] The IPv4 CIDR block for the subnet. # @param tag_key [String] The key portion of the tag for the VPC. # @param tag_value [String] The value portion of the tag for the VPC. # @return [Boolean] true if the VPC was created and tagged; # otherwise, false. # @example # exit 1 unless vpc_created_and_tagged?( # Aws::EC2::Resource.new(region: 'us-west-2'), # '10.0.0.0/24', # 'my-key', # 'my-value' # ) def vpc_created_and_tagged?( ec2_resource, cidr_block, tag_key, tag_value ) vpc = ec2_resource.create_vpc(cidr_block: cidr_block) # Create a public DNS by enabling DNS support and DNS hostnames. vpc.modify_attribute(enable_dns_support: { value: true }) vpc.modify_attribute(enable_dns_hostnames: { value: true }) vpc.create_tags(tags: [{ key: tag_key, value: tag_value }]) puts "Created VPC with ID '#{vpc.id}' and tagged with key " \ "'#{tag_key}' and value '#{tag_value}'." true rescue StandardError => e puts e.message false end # Example usage: def run_me cidr_block = '' 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-vpc.rb ' \ 'CIDR_BLOCK 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-vpc.rb ' \ '10.0.0.0/24 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? cidr_block = '10.0.0.0/24' 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 cidr_block = ARGV[0] tag_key = ARGV[1] tag_value = ARGV[2] region = ARGV[3] end ec2_resource = Aws::EC2::Resource.new(region: region) if vpc_created_and_tagged?( ec2_resource, cidr_block, tag_key, tag_value ) puts 'VPC created and tagged.' else puts 'VPC not created or not tagged.' end end run_me if $PROGRAM_NAME == __FILE__
-
如需 API 詳細資訊,請參閱 CreateVpc AWS SDK for Ruby 參考中的 API。
-