Sono disponibili altri esempi di AWS SDK nel repository AWS Doc SDK Examples. GitHub
Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Utilizzare con un CreateRouteTable AWS SDK o CLI
Gli esempi di codice seguenti mostrano come utilizzare CreateRouteTable.
Gli esempi di operazioni sono estratti di codice da programmi più grandi e devono essere eseguiti nel contesto. È possibile visualizzare questa operazione nel contesto nei seguenti esempi di codice:
- CLI
-
- AWS CLI
-
Come creare una tabella di routing
Nell’esempio seguente viene creata una tabella di routing per il VPC specificato.
Comando:
aws ec2 create-route-table --vpc-id vpc-a01106c2
Output:
{
"RouteTable": {
"Associations": [],
"RouteTableId": "rtb-22574640",
"VpcId": "vpc-a01106c2",
"PropagatingVgws": [],
"Tags": [],
"Routes": [
{
"GatewayId": "local",
"DestinationCidrBlock": "10.0.0.0/16",
"State": "active"
}
]
}
}
- PowerShell
-
- Strumenti per PowerShell V4
-
Esempio 1: questo esempio crea una tabella di routing per il VPC specificato.
New-EC2RouteTable -VpcId vpc-12345678
Output:
Associations : {}
PropagatingVgws : {}
Routes : {}
RouteTableId : rtb-1a2b3c4d
Tags : {}
VpcId : vpc-12345678
- Strumenti per V5 PowerShell
-
Esempio 1: questo esempio crea una tabella di routing per il VPC specificato.
New-EC2RouteTable -VpcId vpc-12345678
Output:
Associations : {}
PropagatingVgws : {}
Routes : {}
RouteTableId : rtb-1a2b3c4d
Tags : {}
VpcId : vpc-12345678
- Ruby
-
- SDK per Ruby
-
require 'aws-sdk-ec2'
# Prerequisites:
#
# - A VPC in Amazon VPC.
# - A subnet in that VPC.
# - A gateway attached to that subnet.
#
# @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 route table.
# @param subnet_id [String] The ID of the subnet for the route table.
# @param gateway_id [String] The ID of the gateway for the route.
# @param destination_cidr_block [String] The destination CIDR block
# for the route.
# @param tag_key [String] The key portion of the tag for the route table.
# @param tag_value [String] The value portion of the tag for the route table.
# @return [Boolean] true if the route table was created and associated;
# otherwise, false.
# @example
# exit 1 unless route_table_created_and_associated?(
# Aws::EC2::Resource.new(region: 'us-west-2'),
# 'vpc-0b6f769731EXAMPLE',
# 'subnet-03d9303b57EXAMPLE',
# 'igw-06ca90c011EXAMPLE',
# '0.0.0.0/0',
# 'my-key',
# 'my-value'
# )
def route_table_created_and_associated?(
ec2_resource,
vpc_id,
subnet_id,
gateway_id,
destination_cidr_block,
tag_key,
tag_value
)
route_table = ec2_resource.create_route_table(vpc_id: vpc_id)
puts "Created route table with ID '#{route_table.id}'."
route_table.create_tags(
tags: [
{
key: tag_key,
value: tag_value
}
]
)
puts 'Added tags to route table.'
route_table.create_route(
destination_cidr_block: destination_cidr_block,
gateway_id: gateway_id
)
puts 'Created route with destination CIDR block ' \
"'#{destination_cidr_block}' and associated with gateway " \
"with ID '#{gateway_id}'."
route_table.associate_with_subnet(subnet_id: subnet_id)
puts "Associated route table with subnet with ID '#{subnet_id}'."
true
rescue StandardError => e
puts "Error creating or associating route table: #{e.message}"
puts 'If the route table was created but not associated, you should ' \
'clean up by deleting the route table.'
false
end
# Example usage:
def run_me
vpc_id = ''
subnet_id = ''
gateway_id = ''
destination_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-route-table.rb ' \
'VPC_ID SUBNET_ID GATEWAY_ID DESTINATION_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-route-table.rb ' \
'vpc-0b6f769731EXAMPLE subnet-03d9303b57EXAMPLE igw-06ca90c011EXAMPLE ' \
"'0.0.0.0/0' 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-0b6f769731EXAMPLE'
subnet_id = 'subnet-03d9303b57EXAMPLE'
gateway_id = 'igw-06ca90c011EXAMPLE'
destination_cidr_block = '0.0.0.0/0'
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]
subnet_id = ARGV[1]
gateway_id = ARGV[2]
destination_cidr_block = ARGV[3]
tag_key = ARGV[4]
tag_value = ARGV[5]
region = ARGV[6]
end
ec2_resource = Aws::EC2::Resource.new(region: region)
if route_table_created_and_associated?(
ec2_resource,
vpc_id,
subnet_id,
gateway_id,
destination_cidr_block,
tag_key,
tag_value
)
puts 'Route table created and associated.'
else
puts 'Route table not created or not associated.'
end
end
run_me if $PROGRAM_NAME == __FILE__