À utiliser CreateVpc avec un AWS SDK ou CLI - Exemples de code de l'AWS SDK

D'autres AWS SDK exemples sont disponibles dans le GitHub dépôt AWS Doc SDK Examples.

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

À utiliser CreateVpc avec un AWS SDK ou CLI

Les exemples de code suivants montrent comment utiliserCreateVpc.

CLI
AWS CLI

Exemple 1 : pour créer un VPC

L'create-vpcexemple suivant crée un VPC avec le IPv4 CIDR bloc spécifié et une balise Name.

aws ec2 create-vpc \ --cidr-block 10.0.0.0/16 \ --tag-specifications ResourceType=vpc,Tags=[{Key=Name,Value=MyVpc}]

Sortie :

{ "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" } ] } }

Exemple 2 : pour créer VPC une location dédiée

L'create-vpcexemple suivant crée un VPC avec le IPv4 CIDR bloc spécifié et une location dédiée.

aws ec2 create-vpc \ --cidr-block 10.0.0.0/16 \ --instance-tenancy dedicated

Sortie :

{ "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 } }

Exemple 3 : Pour créer un VPC avec un IPv6 CIDR bloc

L'create-vpcexemple suivant crée un VPC avec un bloc fourni par Amazon IPv6CIDR.

aws ec2 create-vpc \ --cidr-block 10.0.0.0/16 \ --amazon-provided-ipv6-cidr-block

Sortie :

{ "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 } }

Exemple 4 : Pour créer un VPC avec un CIDR à partir d'un IPAM pool

L'create-vpcexemple suivant crée un VPC with a CIDR à partir d'un pool Amazon VPC IP Address Manager (IPAM).

Linux et 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-id ipam-pool-0533048da7d823723 ^ --tag-specifications ResourceType=vpc,Tags=[{Key=Environment,Value="Preprod"},{Key=Owner,Value="Build Team"}]

Sortie :

{ "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" } ] } }

Pour plus d'informations, consultez la section Créer un VPC IPAM pool CIDR dans le guide de VPC IPAM l'utilisateur Amazon.

  • Pour API plus de détails, voir CreateVpcla section Référence des AWS CLI commandes.

PowerShell
Outils pour PowerShell

Exemple 1 : Cet exemple crée un VPC avec le paramètre spécifiéCIDR. Amazon crée VPC également les éléments suivants pour VPC : un ensemble d'DHCPoptions par défaut, une table de routage principale et un réseau par défautACL.

New-EC2VPC -CidrBlock 10.0.0.0/16

Sortie :

CidrBlock : 10.0.0.0/16 DhcpOptionsId : dopt-1a2b3c4d InstanceTenancy : default IsDefault : False State : pending Tags : {} VpcId : vpc-12345678
  • Pour API plus de détails, consultez la section CreateVpcRéférence des AWS Tools for PowerShell applets de commande.

Ruby
SDKpour Ruby
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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}'." return true rescue StandardError => e puts "#{e.message}" return 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__
  • Pour API plus de détails, voir CreateVpcla section AWS SDK for Ruby APIRéférence.