从 CLI 管理 AWS Transit Gateway 和附件的 24 个示例
AWS Transit 网关充当连接多个 VPC 和本地网络的枢纽。除了将 VPC 附加到中转枢纽和路由流量之外,您还可以将 VPN 连接或 Direct Connect 网关附加到中转网关。您还可以对等两个中转网关并在它们之间路由流量。
在多账户环境中,您可以在中央网络账户中创建 Transit 网关,并与外部账户或组织内的账户共享它们。
本教程介绍如何使用 CLI 命令管理 AWS Transit 网关和附件。
- Transit Gateway:示例 1 到 6 说明了如何使用 CLI 命令创建、查看和删除 Transit Gateway。
- Transit Gateway Attachments to a VPC:示例 7 到 14 说明了如何创建、修改、查看和删除到 VPC 的 Transit Gateway 附件。
- Transit Gateway Attachments to a VPN:示例 15 说明了如何使用 create-vpn-connection CLI 命令完成连接到 VPN 的中转网关。
- Sharing a Transit Gateway:示例 16 到 18 说明如何使用资源访问管理器跨账户共享中转网关
- Peer two Transit Gateways:示例 19 到 22 说明了如何对等两个中转网关并使用 CLI 在它们之间路由流量。
- Add Routes:示例 23 和 24 说明如何将路由添加到指向 Transit Gateway 的 VPC 路由表,以及如何将路由添加到中转网关路由表
1. 使用所有默认值创建 Transit Gateway
如下所示,使用 aws ec2 create-transit-gateway 创建中转网关。这将使用所有默认选项创建中转网关。
aws ec2 create-transit-gateway --description prodTGW
当您不指定任何选项时,以下默认值将用于中转网关选项:
- Amazon ASN: 64512
- Auto accept shared attachments: disable
- Default association route table: enable
- Default propagation route table: enable
- VPN ECMP support: enable
- DNS support: enable
以下是上述命令的输出:
{
"TransitGateway": {
"Description": "prodTGW",
"TransitGatewayArn": "arn:aws:ec2:us-east-1:111111111111:transit-gateway/tgw-000aaabbbccdddeee",
"CreationTime": "2020-06-13T00:31:03.000Z",
"State": "pending",
"TransitGatewayId": "tgw-000aaabbbccdddeee",
"OwnerId": "111111111111",
"Options": {
"DefaultRouteTableAssociation": "enable",
"DnsSupport": "enable",
"AutoAcceptSharedAttachments": "disable",
"AssociationDefaultRouteTableId": "tgw-rtb-000aaabbbcccdddee",
"PropagationDefaultRouteTableId": "tgw-rtb-000aaabbbcccdddee",
"AmazonSideAsn": 64512,
"DefaultRouteTablePropagation": "enable",
"VpnEcmpSupport": "enable"
}
}
}
2. 使用自定义选项创建 Transit Gateway – 更改 AmazonSideASN 和 AutoAcceptSharedAttachments
如果您想为中转网关的亚马逊端指定您自己的 ASN,请使用 – 选项,如下所示。此示例还启用 AutoAcceptSharedAttachments 选项。
aws ec2 create-transit-gateway --description prodTGW \
--options=AmazonSideAsn=64516,AutoAcceptSharedAttachments=enable
以下是上述命令的部分输出。
{
"TransitGateway": {
..
..
"Options": {
"DefaultRouteTableAssociation": "enable",
"DnsSupport": "enable",
"AutoAcceptSharedAttachments": "enable",
"AssociationDefaultRouteTableId": "tgw-rtb-000aaabbbcccdddee",
"PropagationDefaultRouteTableId": "tgw-rtb-000aaabbbcccdddee",
"AmazonSideAsn": 64516,
"DefaultRouteTablePropagation": "enable",
"VpnEcmpSupport": "enable"
}
}
}
3. 通过更改所有可用的自定义选项来创建中转网关
以下命令显示了在创建中转网关时可以更改的所有可能选项
aws ec2 create-transit-gateway --description prodTGW \
--options=AmazonSideAsn=64516,AutoAcceptSharedAttachments=enable,DefaultRouteTableAssociation=enable,DefaultRouteTablePropagation=enable,VpnEcmpSupport=enable,DnsSupport=enable
AmazonSideAsn 选项采用数字 long 值。其余选项的值可以是启用或禁用。上面的示例通过用逗号分隔选项来使用选项的速记语法格式。您还可以对选项使用以下 JSON 语法格式:
{
"AmazonSideAsn": long,
"AutoAcceptSharedAttachments": "enable"|"disable",
"DefaultRouteTableAssociation": "enable"|"disable",
"DefaultRouteTablePropagation": "enable"|"disable",
"VpnEcmpSupport": "enable"|"disable",
"DnsSupport": "enable"|"disable",
"MulticastSupport": "enable"|"disable"
}
4. 创建带有名称标签和描述的中转网关
您还可以在创建中转网关时指定标签。以下命令使用自定义选项创建中转网关并为名称标签分配一个值。
aws ec2 create-transit-gateway --description prodTGW \
--tag-specifications "ResourceType=transit-gateway,Tags=[{Key=Name,Value=prodTGW}]" \
--options=AmazonSideAsn=64516,AutoAcceptSharedAttachments=enable,DefaultRouteTableAssociation=enable,DefaultRouteTablePropagation=enable,VpnEcmpSupport=enable,DnsSupport=enable
以下是上述命令的输出:
{
"TransitGateway": {
"Description": "prodTGW",
"TransitGatewayArn": "arn:aws:ec2:us-east-1:111111111111:transit-gateway/tgw-000aaabbbccdddeee",
"Tags": [
{
"Value": "prodTGW",
"Key": "Name"
}
],
"CreationTime": "2020-06-13T16:50:26.000Z",
"State": "pending",
"TransitGatewayId": "tgw-000aaabbbccdddeee",
"OwnerId": "111111111111",
"Options": {
"DefaultRouteTableAssociation": "enable",
"DnsSupport": "enable",
"AutoAcceptSharedAttachments": "enable",
"AssociationDefaultRouteTableId": "tgw-rtb-000aaabbbcccdddee",
"PropagationDefaultRouteTableId": "tgw-rtb-000aaabbbcccdddee",
"AmazonSideAsn": 64516,
"DefaultRouteTablePropagation": "enable",
"VpnEcmpSupport": "enable"
}
}
}
5. 显示现有的中转网关
以下命令将显示所有可用的中转网关:
aws ec2 describe-transit-gateways
要仅查看特定中转网关的详细信息,请指定中转网关 ID,如下所示。
TGW_ID=tgw-000aabbbccdddeee
aws ec2 describe-transit-gateways --transit-gateway-ids ${TGW_ID}
6. 删除中转网关
通过提供中转网关 ID 使用如下所示的删除中转网关
TGW_ID=tgw-000aabbbccdddeee
aws ec2 delete-transit-gateway --transit-gateway-id ${TGW_ID}
您无法删除具有附件的中转网关。您将收到以下错误消息:
注意:调用 DeleteTransitGateway 操作时发生错误 (IncorrectState):tgw-000aaabbbccdddeee 具有未删除的 VPC 附件:tgw-attach-000aaabbbcccdddee。
注意:如果您的子网/vpc 的可路由表中有指向已删除中转网关的路由,它们将具有黑洞状态。因此,请务必在删除中转网关后删除路由。
7. 使用必填字段创建 Transit Gateway VPC 附件
拥有中转网关后,您可以创建以下三种类型的附件:
- 中转网关 VPC 附件
- 中转网关 VPN 附件
- 与另一个中转网关对等的中转网关对等连接
以下示例显示如何创建中转网关 VPC 连接。
TGW_ID=tgw-000aaabbbccdddeee
VPC1=vpc-000111aaabbbcccdd
VPC1_PUBLIC_SUBNET1=subnet-111222333aaabbbcc
VPC1_PUBLIC_SUBNET2=subnet-000222aaabbbcccdd
aws ec2 create-transit-gateway-vpc-attachment \
--transit-gateway-id ${TGW_ID} \
--vpc-id ${VPC1} \
--subnet-ids ${VPC1_PUBLIC_SUBNET1} ${VPC1_PUBLIC_SUBNET2}
创建 vpc 附件时,强制选项是 vpc-id 和该 vpc 中的子网 ID。例如,您可以跨多个 AZ 为该 VPC 中的所有公有子网创建中转网关 vpc 连接。
以下是上述命令的输出:
{
"TransitGatewayVpcAttachment": {
"VpcId": "vpc-000111aaabbbcccdd",
"VpcOwnerId": "111111111111",
"SubnetIds": [
"subnet-000222aaabbbcccdd",
"subnet-111222333aaabbbcc"
],
"TransitGatewayAttachmentId": "tgw-attach-000aaabbbcccdddee",
"CreationTime": "2020-06-13T00:48:13.000Z",
"State": "pending",
"TransitGatewayId": "tgw-000aaabbbccdddeee",
"Options": {
"DnsSupport": "enable",
"Ipv6Support": "disable"
}
}
}
8. 使用名称标签创建 Transit Gateway VPC 附件
以下示例显示如何创建带有名称标签的 Transit Gateway VPC 附件。您还可以通过将另一个键/值对添加到标签选项来将多个标签附加到附件。
TGW_ID=tgw-000aaabbbccdddeee
VPC1=vpc-000111aaabbbcccdd
VPC1_PUBLIC_SUBNET1=subnet-111222333aaabbbcc
VPC1_PUBLIC_SUBNET2=subnet-000222aaabbbcccdd
aws ec2 create-transit-gateway-vpc-attachment \
--tag-specifications "ResourceType=transit-gateway-attachment,Tags=[{Key=Name,Value=appOnPremAccess}]" \
--transit-gateway-id ${TGW_ID} \
--vpc-id ${VPC1} \
--subnet-ids ${VPC1_PUBLIC_SUBNET1} ${VPC1_PUBLIC_SUBNET2}
以下是上述命令的部分输出。
{
"TransitGatewayVpcAttachment": {
"Tags": [
{
"Value": "appOnPremAccess",
"Key": "Name"
}
],
..
..
}
}
当您不指定任何选项时,将使用启用 DNS 支持和禁用 IPv6 支持来创建附件。
注意:调用 CreateTransitGatewayVpcAttachment 操作时发生错误 (DuplicateTransitGatewayAttachment):tgw-000aaabbbccdddeee 具有未删除的具有相同 VPC ID 的中转网关附件。
9. 创建支持 IPV6 的 Transit Gateway VPC 附件
默认情况下,中转网关是在不支持 IPv6 的情况下创建的。要启用对 IPv6 的支持,请使用 Ipv6Support 选项,如下所示。
aws ec2 create-transit-gateway-vpc-attachment \
--tag-specifications "ResourceType=transit-gateway-attachment,Tags=[{Key=Name,Value=appOnPremAccess}]" \
--options "Ipv6Support=enable" \
--transit-gateway-id ${TGW_ID} \
--vpc-id ${VPC1} \
--subnet-ids ${VPC1_PUBLIC_SUBNET1} ${VPC1_PUBLIC_SUBNET2}
如果您没有与子网关联的 IPv6 CIDR 块,您将收到以下错误消息:
注意:调用 CreateTransitGatewayVpcAttachment 操作时发生错误 (InvalidParameterCombination):subnet-000222aaabbbcccdd has no IPv6 CidrBlocks associated
10. 使用所有可用的自定义选项创建 Transit Gateway VPC 附件
aws ec2 create-transit-gateway-vpc-attachment \
--tag-specifications "ResourceType=transit-gateway-attachment,Tags=[{Key=Name,Value=appOnPremAccess}]" \
--options "DnsSupport=disable,Ipv6Support=disable" \
--transit-gateway-id ${TGW_ID} \
--vpc-id ${VPC1} \
--subnet-ids ${VPC1_PUBLIC_SUBNET1} ${VPC1_PUBLIC_SUBNET2}
上面的示例通过用逗号分隔选项来使用选项的速记语法格式。您还可以对选项使用以下 JSON 语法格式:
{
"DnsSupport": "enable"|"disable",
"Ipv6Support": "enable"|"disable"
}
11. 修改 Transit Gateway VPC 附件 – 添加或删除子网
创建中转网关 VPC 连接后,您可以添加或远程子网,如下所示。
VPC1_TGW_ATTACHMENT_ID=tgw-attach-000aaabbbcccdddee
aws ec2 modify-transit-gateway-vpc-attachment \
--transit-gateway-attachment-id ${VPC1_TGW_ATTACHMENT_ID} \
--remove-subnet-ids subnet-111222333aaabbbcc \
--add-subnet-ids subnet-222111000aaabbbcc
以下是上述命令的输出:
{
"TransitGatewayVpcAttachment": {
"TransitGatewayAttachmentId": "tgw-attach-000aaabbbcccdddee",
"TransitGatewayId": "tgw-000aaabbbccdddeee",
"VpcId": "vpc-d4ef7eaf",
"VpcOwnerId": "222222222222",
"State": "modifying",
"SubnetIds": [
"subnet-222111000aaabbbcc",
"subnet-111222333aaabbbcc",
"subnet-000222aaabbbcccdd"
],
"CreationTime": "2020-06-13T19:31:19+00:00",
"Options": {
"DnsSupport": "enable",
"Ipv6Support": "disable"
}
}
}
正如您从上面的输出中看到的,当状态为“修改”时,您仍然会看到当前正在被删除的子网。几秒钟后,当状态变为“可用”时,您应该只看到此中转网关连接的 2 个子网。
调用 ModifyTransitGatewayVpcAttachment 操作时出错 (InvalidSubnetID.NotFound):子网 ID 'subnet-111222333aaabbbcc' 不存在
12. 修改 Transit Gateway VPC 附件 – 更改选项
您还可以在创建附件后更改默认选项。以下示例显示如何更改中转网关连接上的 DNS 支持和 IPv6 支持选项。
VPC1_TGW_ATTACHMENT_ID=tgw-attach-000aaabbbcccdddee
aws ec2 modify-transit-gateway-vpc-attachment \
--transit-gateway-attachment-id ${VPC1_TGW_ATTACHMENT_ID} \
--options DnsSupport=disable,Ipv6Support=disable
在修改附件的同时,您还可以同时修改选项和添加/删除子网,如下图所示。
VPC1_TGW_ATTACHMENT_ID=tgw-attach-000aaabbbcccdddee
aws ec2 modify-transit-gateway-vpc-attachment \
--transit-gateway-attachment-id ${VPC1_TGW_ATTACHMENT_ID} \
--options DnsSupport=enable,Ipv6Support=disable \
--remove-subnet-ids subnet-222111000aaabbbcc \
--add-subnet-ids subnet-111222333aaabbbcc
13. 显示现有的中转网关附件
以下示例将显示您账户中所有可用的中转网关附件:
aws ec2 describe-transit-gateway-attachments
您还可以通过指定transit-gateway-attachment-ids 来查看特定附件的详细信息,如下所示。
VPC1_TGW_ATTACHMENT_ID=tgw-attach-000aaabbbcccdddee
aws ec2 describe-transit-gateway-attachments --transit-gateway-attachment-ids ${VPC1_TGW_ATTACHMENT_ID}
以下是上述命令的输出:
{
"TransitGatewayAttachments": [
{
"ResourceOwnerId": "111111111111",
"TransitGatewayAttachmentId": "tgw-attach-000aaabbbcccdddee",
"ResourceType": "vpc",
"ResourceId": "vpc-000111aaabbbcccdd",
"Tags": [
{
"Value": "appOnPremAccess",
"Key": "Name"
}
],
"CreationTime": "2020-06-13T01:10:17.000Z",
"State": "available",
"TransitGatewayId": "tgw-000aaabbbccdddeee",
"TransitGatewayOwnerId": "111111111111",
"Association": {
"State": "associated",
"TransitGatewayRouteTableId": "tgw-rtb-000aaabbbcccdddee"
}
}
]
}
14. 删除 Transit Gateway VPC 附件
以下示例显示如何删除现有的中转网关 vpc 连接。
VPC1_TGW_ATTACHMENT_ID=tgw-attach-000aaabbbcccdddee
aws ec2 delete-transit-gateway-vpc-attachment \
--transit-gateway-attachment-id ${VPC1_TGW_ATTACHMENT_ID}
上述命令的输出将显示附件当前处于删除状态。
{
"TransitGatewayVpcAttachment": {
"VpcId": "vpc-000111aaabbbcccdd",
"VpcOwnerId": "111111111111",
"TransitGatewayAttachmentId": "tgw-attach-000aaabbbcccdddee",
"CreationTime": "2020-06-13T00:58:31.000Z",
"State": "deleting",
"TransitGatewayId": "tgw-000aaabbbccdddeee"
}
}
15. 创建一个到 VPN 的 Transit Gateway 附件
与创建到 VPC 的中转网关连接类似,您也可以创建到 VPN 的连接。
为此,您将通过指定 transit-gateway-id 和 customer-gateway-id 来创建 VPN 连接,如下所示。
CGW_ID=cgw-000111333aaabbbcc
TGW_ID=tgw-000aaabbbccdddeee
aws ec2 create-vpn-connection \
--customer-gateway-id ${CGW_ID} \
--type ipsec.1 \
--transit-gateway-id ${TGW_ID}
注意:不要忘记将 VPN 类型指定为 ipsec.1
以下是上述命令的部分输出。
{
"VpnConnection": {
"CustomerGatewayConfiguration": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<vpn_connection id=\"vpn-
..
..
</vpn_connection>",
"CustomerGatewayId": "cgw-000111333aaabbbcc",
"Category": "VPN",
"State": "pending",
"VpnConnectionId": "vpn-000111222333aaabb",
"TransitGatewayId": "tgw-000aaabbbccdddeee",
"Options": {
"EnableAcceleration": false,
"StaticRoutesOnly": false,
"TunnelOptions": [
..
..
}
在创建 VPN 连接时,您还可以指定 JSON 格式的各种 TunnelOptions。
例如,要仅在 VPN 连接中使用静态路由,请在上述命令中设置如下所示的选项。
--options "{\"StaticRoutesOnly\":true}"
创建 VPN 中转网关连接后,您将在描述命令输出中看到 ResourceType 为“vpn”。
以下示例显示有两个附件。一个是vpc,另一个是vpn。
$ aws ec2 describe-transit-gateway-attachments
{
"TransitGatewayAttachments": [
{
"TransitGatewayAttachmentId": "tgw-attach-000aaabbbcccdddee",
"ResourceType": "vpc",
..
..
},
{
"TransitGatewayAttachmentId": "tgw-attach-03210321aaabbbccc",
"ResourceType": "vpn",
..
..
},
...
16. 在第一个帐户中为 Transit Gateway 创建资源访问共享
要跨账户共享中转网关,请使用 AWS Resource Access Manager。
以下示例显示如何创建资源共享并将中转网关与其关联。
在此示例中,中转网关是在 111111111111 账户中创建的。从这个帐户,我们正在创建一个资源共享并与 222222222222 帐户共享。
aws ram create-resource-share \
--name tgwDevShares \
--resource-arns arn:aws:ec2:us-east-1:111111111111:transit-gateway/tgw-000aaabbbccdddeee \
--principals 222222222222 \
--tags "key=Name,value=devShare"
这是上述命令的输出。
{
"resourceShare": {
"status": "ACTIVE",
"owningAccountId": "111111111111",
"allowExternalPrincipals": true,
"name": "tgwDevShares",
"tags": [
{
"value": "devShare",
"key": "Name"
}
],
"creationTime": 1641722024.078,
"resourceShareArn": "arn:aws:ram:us-east-1:111111111111:resource-share/11111111-2222-aaaa-bbbb-cccccccccccc",
"lastUpdatedTime": 1522722024.064
}
}
17. 接受来自第二个帐户的中转网关资源共享
确保 AWS CLI 现在已连接到第二个账户 222222222222。
执行以下命令查看第二个账号的所有资源共享邀请。
aws ram get-resource-share-invitations
输出表明此邀请仍处于待处理状态。
{
"resourceShareInvitations": [
{
"resourceShareInvitationArn": "arn:aws:ram:us-east-1:111111111111:resource-share-invitation/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
"resourceShareName": "tgwDevShares",
"resourceShareArn": "arn:aws:ram:us-east-1:111111111111:resource-share/11111111-2222-aaaa-bbbb-cccccccccccc",
"senderAccountId": "111111111111",
"receiverAccountId": "222222222222",
"invitationTimestamp": "2020-06-13T10:00:24.249000-07:00",
"status": "PENDING"
}
]
}
从上述输出中获取 resourceShareInvitationArn。
注意:资源共享邀请的 ARN 与资源共享 ARN 不同。
使用 resourceShareInvitationArn 接受邀请,如下所示:
aws ram accept-resource-share-invitation \
--resource-share-invitation-arn arn:aws:ram:us-east-1:111111111111:resource-share-invitation/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
接受邀请后,此资源共享邀请的状态将更改为已接受,如下所示。
{
"resourceShareInvitation": {
"resourceShareInvitationArn": "arn:aws:ram:us-east-1:111111111111:resource-share-invitation/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
"resourceShareName": "tgwDevShares",
"resourceShareArn": "arn:aws:ram:us-east-1:111111111111:resource-share/11111111-2222-aaaa-bbbb-cccccccccccc",
"senderAccountId": "111111111111",
"receiverAccountId": "222222222222",
"invitationTimestamp": "2020-06-13T10:57:03.509000-07:00",
"status": "ACCEPTED"
}
}
18. 从第一个帐户接受在第二个帐户中创建的中转网关附件
现在中转网关已从第一个账户共享到第二个账户,您可以在第二个账户上创建中转网关连接。
之后,如果在中转网关上禁用了 AutoAcceptSharedAttachments,您应该手动接受来自第一个账户的附件。
首先,执行以下命令,查看是否需要接受任何附件。
aws ec2 describe-transit-gateway-vpc-attachments
如下所示,如果附件仍未被接受,则状态将显示为 pendingAcceptance。
{
"TransitGatewayVpcAttachments": [
{
"TransitGatewayAttachmentId": "tgw-attach-000aaabbbcccdddee",
..
..
"State": "pendingAcceptance",
}
]
}
从上述输出中获取 TransitGatewayAttachmentId 并接受附件,如下所示:
aws ec2 accept-transit-gateway-vpc-attachment \
--transit-gateway-attachment-id tgw-attach-000aaabbbcccdddee
状态将从等待接受变为等待变为可用。
19. 为来自第一个帐户的 TGW 对等创建中转网关附件
当您有两个中转网关时,您可以对等这些网关并在它们之间路由流量。您可以从另一个区域甚至另一个账户对等中转网关。
为此,您应该首先从您的第一个 TGW 所在的第一个账户创建一个中转网关对等连接。
从第一个帐户,执行以下命令来创建对等连接:
TGW_ID=tgw-000aabbbccdddeee
PEER_TGW_ID=tgw-222333444aaabbbcc
PEER_ACCOUNT_ID=222222222222
PEER_REGION=us-east-2
aws ec2 create-transit-gateway-peering-attachment \
--transit-gateway-id ${TGW_ID} \
--peer-transit-gateway-id ${PEER_TGW_ID} \
--peer-account-id ${PEER_ACCOUNT_ID} \
--peer-region ${PEER_REGION}
在上面:
TGW_ID – 这是您账户(第一个账户)
中的第一个中转网关 PEER_TGW_ID – 这是由 PEER_ACCOUNT_ID 引用的另一个账户(第二个账户)中的第二个中转网关 PEER_REGION
PEER_TGW_ID 所在的区域存在
以下是上述命令的输出。
{
"TransitGatewayPeeringAttachment": {
"TransitGatewayAttachmentId": "tgw-attach-111222333aaabbbcc",
"RequesterTgwInfo": {
"TransitGatewayId": "tgw-000aaabbbccdddeee",
"OwnerId": "111111111111",
"Region": "us-east-1"
},
"AccepterTgwInfo": {
"TransitGatewayId": "tgw-222333444aaabbbcc",
"OwnerId": "111111111111",
"Region": "us-east-2"
},
"State": "initiatingRequest",
"CreationTime": "2020-06-13T22:15:54+00:00"
}
}
最初状态将是在发起请求,然后变为等待接受
20. 接受来自第二个帐户的 Transit Gateway 对等连接请求
现在登录到上面命令中由 PEER_ACCOUNT_ID 引用的第二个帐户并接受对等连接请求。
PEER_TGW_ATTACHMENT_ID=tgw-attach-111222333aaabbbcc
PEER_REGION=us-east-2
aws ec2 accept-transit-gateway-peering-attachment \
--transit-gateway-attachment-id ${PEER_TGW_ATTACHMENT_ID} \
--region ${PEER_REGION}
以下是上述命令的输出:
{
"TransitGatewayPeeringAttachment": {
"TransitGatewayAttachmentId": "tgw-attach-111222333aaabbbcc",
"RequesterTgwInfo": {
"TransitGatewayId": "tgw-000aaabbbccdddeee",
"OwnerId": "111111111111",
"Region": "us-east-1"
},
"AccepterTgwInfo": {
"TransitGatewayId": "tgw-222333444aaabbbcc",
"OwnerId": "222222222222",
"Region": "us-east-2"
},
"State": "pending",
"CreationTime": "2020-06-13T22:16:17+00:00"
}
}
创建对等连接后,修改中转网关路由表并添加静态路由以指向此对等连接。
21. 列出所有 Transit gateway 对等连接附件
执行以下命令以查看现有中转网关对等连接的详细信息。
aws ec2 describe-transit-gateway-peering-attachments
{
"TransitGatewayPeeringAttachments": [
{
"TransitGatewayAttachmentId": "tgw-attach-111222333aaabbbcc",
"RequesterTgwInfo": {
"TransitGatewayId": "tgw-000aaabbbccdddeee",
"OwnerId": "111111111111",
"Region": "us-east-1"
},
"AccepterTgwInfo": {
"TransitGatewayId": "tgw-222333444aaabbbcc",
"OwnerId": "111111111111",
"Region": "us-east-2"
},
"Status": {
"Code": "available",
"Message": "Available"
},
"State": "available",
"CreationTime": "2020-06-13T22:15:54+00:00",
"Tags": []
}
]
}
22. 删除中转网关对等连接
使用以下 delete-transit-gateway-peering-attachment 删除对等连接。
PEER_TGW_ATTACHMENT_ID=tgw-attach-111222333aaabbbcc
aws ec2 delete-transit-gateway-peering-attachment \
--transit-gateway-attachment-id ${PEER_TGW_ATTACHMENT_ID}
以下是上述命令的输出:
{
"TransitGatewayPeeringAttachment": {
"TransitGatewayAttachmentId": "tgw-attach-111222333aaabbbcc",
"RequesterTgwInfo": {
"TransitGatewayId": "tgw-000aaabbbccdddeee",
"OwnerId": "111111111111",
"Region": "us-east-1"
},
"AccepterTgwInfo": {
"TransitGatewayId": "tgw-222333444aaabbbcc",
"OwnerId": "111111111111",
"Region": "us-east-2"
},
"State": "deleting",
"CreationTime": "2020-06-13T22:15:54+00:00"
}
}
请注意,您不能使用 vpc-attachment 命令删除它。您将收到如下所示的错误。
$ aws ec2 delete-transit-gateway-vpc-attachment \
--transit-gateway-attachment-id ${PEER_TGW_ATTACHMENT_ID}
An error occurred (InvalidTransitGatewayAttachmentID.NotFound) when calling the DeleteTransitGatewayVpcAttachment operation: Transit Gateway Attachment tgw-attach-111222333aaabbbcc was deleted or does not exist.
23. 将路由添加到 VPC 路由表,条目指向 Transit Gateway
创建中转网关后,您可以将路由添加到子网路由表,将其指向中转网关,如下所示。
VPC1_PUBLIC_SUBNET_ROUTETABLE=rtb-111222333444555
aws ec2 create-route \
--route-table-id ${VPC1_PUBLIC_SUBNET_ROUTETABLE} \
--destination-cidr-block 10.0.0.0/8 \
--transit-gateway-id ${TGW_ID}
如果路由已正确添加,您将收到以下消息:
{
"Return": true
}
注意:调用 CreateRoute 操作时发生错误(MissingParameter):请求必须包含 gatewayId、natGatewayId、networkInterfaceId、vpcPeeringConnectionId、egressOnlyInternetGatewayId、transitGatewayId、localGatewayId 或 instanceId 中的一个
24. 将路由添加到中转网关路由表
如果要将路由直接添加到中转网关路由表,请使用 create-transit-gateway-route 选项,如下所示。
在这个例子中,给定的静态路由被添加到 TGW_ROUTE_TABLE_ID 路由表中。在此路由中,任何前往 CIDR 的流量都将使用 TGW_ATTACHMENT_ID。
CIDR=10.10.10.0/32
TGW_ROUTE_TABLE_ID=tgw-rtb-555444333222aaabb
TGW_ATTACHMENT_ID=tgw-attach-03210321aaabbbccc
aws ec2 create-transit-gateway-route \
--destination-cidr-block ${CIDR} \
--transit-gateway-route-table-id ${TGW_ROUTE_TABLE_ID} \
--transit-gateway-attachment-id ${TGW_ATTACHMENT_ID}
以下是上述命令的输出。这表明静态路由添加成功并且处于活动状态。
{
"Route": {
"DestinationCidrBlock": "10.10.10.0/32",
"TransitGatewayAttachments": [
{
"ResourceId": "vpn-000aaacccddd66655",
"TransitGatewayAttachmentId": "tgw-attach-03210321aaabbbccc",
"ResourceType": "vpn"
}
],
"Type": "static",
"State": "active"
}
}
- 点赞
- 收藏
- 关注作者
评论(0)