

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Amazon Direct Connect CLI
<a name="using-cli"></a>

您可以通过 Amazon CLI 创建和使用 Amazon Direct Connect 资源。

以下示例使用 Amazon CLI 命令创建 Amazon Direct Connect 连接。您也可以下载《授权证书和连接设备分配 (LOA-CFA)》或预置一个私有或公有虚拟接口。

在开始之前，请确保您已经安装并配置 Amazon CLI。有关更多信息，请参阅[《Amazon Command Line Interface 用户指南》](https://docs.amazonaws.cn/cli/latest/userguide/)。

**Topics**
+ [步骤 1：创建连接](#using-cli-create-connection)
+ [步骤 2：下载 LOA-CFA](#using-cli-loa-cfa)
+ [步骤 3：创建虚拟接口，获取路由器配置](#using-cli-create-vif)

## 步骤 1：创建连接
<a name="using-cli-create-connection"></a>

第一步是提交连接请求。确保您知道所需的端口速度和 Amazon Direct Connect 位置。有关更多信息，请参阅 [专用和托管连接](WorkingWithConnections.md)。

**创建连接请求**

1. 描述您当前区域中的 Amazon Direct Connect 位置。在返回的输出中，记录您要建立连接的位置的位置代码。

   ```
   aws directconnect describe-locations
   ```

   ```
   {
       "locations": [
           {
               "locationName": "City 1, United States",
               "locationCode": "Example Location 1"
           },
           {
               "locationName": "City 2, United States",
               "locationCode": "Example location"
           }
       ]
   }
   ```

1. 创建连接并指定名称、端口速度和位置代码。在返回的输出中，记录连接 ID。您需要该 ID 在下一步获取 LOA-CFA。

   ```
   aws directconnect create-connection --location Example location --bandwidth 1Gbps --connection-name "Connection to AWS"
   ```

   ```
   {
       "ownerAccount": "123456789012",
       "connectionId": "dxcon-EXAMPLE",
       "connectionState": "requested",
       "bandwidth": "1Gbps",
       "location": "Example location",
       "connectionName": "Connection to AWS",
       "region": "sa-east-1"
   }
   ```

## 步骤 2：下载 LOA-CFA
<a name="using-cli-loa-cfa"></a>

在请求连接后，您就可以使用 `describe-loa` 命令获取 LOA-CFA。输出为 base64 编码。您必须提取相关的 LOA 内容、进行解码并创建 PDF 文件。

**使用 Linux 或 macOS 获取 LOA-CFA**  
在此示例中，命令的最后一部分使用 base64 实用工具解码内容并将输出发送到 PDF 文件。

```
aws directconnect describe-loa --connection-id dxcon-fg31dyv6 --output text --query loaContent|base64 --decode > myLoaCfa.pdf
```

**使用 Windows 获取 LOA-CFA**  
在本示例中，输出将提取到名为 myLoaCfa.base64 的文件。第二个命令使用 `certutil` 实用工具解码文件并将输出发送到 PDF 文件。

```
aws directconneawsct describe-loa --connection-id dxcon-fg31dyv6 --output text --query loaContent > myLoaCfa.base64
```

```
certutil -decode myLoaCfa.base64 myLoaCfa.pdf
```

下载 LOA-CFA 之后，将其发送到网络提供商或主机托管提供商。

## 步骤 3：创建虚拟接口，获取路由器配置
<a name="using-cli-create-vif"></a>

订购 Amazon Direct Connect 连接以后，您必须创建虚拟接口以开始使用。您可以创建一个私有虚拟接口来连接到您的 VPC。或者，您也可以创建一个公有虚拟接口，以连接到不在 VPC 中的 Amazon 服务。您可以创建支持 IPv4 或 IPv6 流量的接口。

在开始之前，请您务必阅读 [虚拟接口的先决条件](WorkingWithVirtualInterfaces.md#vif-prerequisites) 中的先决条件。

使用 Amazon CLI 创建虚拟接口时，输出包括通用路由器配置信息。要创建特定于您的设备的路由器配置，请使用 Amazon Direct Connect 控制台。有关更多信息，请参阅 [下载路由器配置文件](vif-router-config.md)。

**创建私有虚拟接口**

1. 获取附加到您 VPC 的虚拟私有网关的 ID (vgw-*xxxxxxxx*)。您需要该 ID 在下一步创建虚拟接口。

   ```
   aws ec2 describe-vpn-gateways
   ```

   ```
   {
       "VpnGateways": [
           {
               "State": "available", 
               "Tags": [
                   {
                       "Value": "DX_VGW", 
                       "Key": "Name"
                   }
               ], 
               "Type": "ipsec.1", 
               "VpnGatewayId": "vgw-ebaa27db", 
               "VpcAttachments": [
                   {
                       "State": "attached", 
                       "VpcId": "vpc-24f33d4d"
                   }
               ]
           }
       ]
   }
   ```

1. 创建私有虚拟接口。您必须指定名称、VLAN ID 和 BGP 自治系统编号 (ASN)。

   对于 IPv4 流量，您需要为 BGP 对等会话的每一端都指定私有 IPv4 地址。您可以指定自己的 IPv4 地址，也可以让 Amazon 为您生成地址。在以下示例中，将为您生成 IPv4 地址。

   ```
   aws directconnect create-private-virtual-interface --connection-id dxcon-fg31dyv6 --new-private-virtual-interface virtualInterfaceName=PrivateVirtualInterface,vlan=101,asn=65000,virtualGatewayId=vgw-ebaa27db,addressFamily=ipv4
   ```

   ```
   {
       "virtualInterfaceState": "pending",
       "asn": 65000,
       "vlan": 101,
       "customerAddress": "192.168.1.2/30",
       "ownerAccount": "123456789012",
       "connectionId": "dxcon-fg31dyv6",
       "addressFamily": "ipv4", 
       "virtualGatewayId": "vgw-ebaa27db",
       "virtualInterfaceId": "dxvif-ffhhk74f",
       "authKey": "asdf34example",
       "routeFilterPrefixes": [],
       "location": "Example location",
       "bgpPeers": [
           {
               "bgpStatus": "down", 
               "customerAddress": "192.168.1.2/30", 
               "addressFamily": "ipv4", 
               "authKey": "asdf34example", 
               "bgpPeerState": "pending", 
               "amazonAddress": "192.168.1.1/30", 
               "asn": 65000
           }
       "customerRouterConfig": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<logical_connection id=\"dxvif-ffhhk74f\">\n  <vlan>101</vlan>\n  <customer_address>192.168.1.2/30</customer_address>\n  <amazon_address>192.168.1.1/30</amazon_address>\n  <bgp_asn>65000</bgp_asn>\n  <bgp_auth_key>asdf34example</bgp_auth_key>\n  <amazon_bgp_asn>7224</amazon_bgp_asn>\n  <connection_type>private</connection_type>\n</logical_connection>\n",
       "amazonAddress": "192.168.1.1/30",
       "virtualInterfaceType": "private",
       "virtualInterfaceName": "PrivateVirtualInterface"
   }
   ```

   要创建支持 IPv6 流量的私有虚拟接口，请使用上述命令并为 `ipv6` 参数指定 `addressFamily`。您不能为 BGP 对等会话指定自己的 IPv6 地址；Amazon 向您分配 IPv6 地址。

1. 要查看 XML 格式的路由器配置信息，请描述您创建的虚拟接口。使用 `--query` 参数可提取 `customerRouterConfig` 信息，使用 `--output` 参数可将文本排列到以制表符分隔的行中。

   ```
   aws directconnect describe-virtual-interfaces --virtual-interface-id dxvif-ffhhk74f --query virtualInterfaces[*].customerRouterConfig --output text
   ```

   ```
   <?xml version="1.0" encoding="UTF-8"?>
   <logical_connection id="dxvif-ffhhk74f">
     <vlan>101</vlan>
     <customer_address>192.168.1.2/30</customer_address>
     <amazon_address>192.168.1.1/30</amazon_address>
     <bgp_asn>65000</bgp_asn>
     <bgp_auth_key>asdf34example</bgp_auth_key>
     <amazon_bgp_asn>7224</amazon_bgp_asn>
     <connection_type>private</connection_type>
   </logical_connection>
   ```

**创建公有虚拟接口**

1. 要创建公有虚拟接口，您必须指定名称、VLAN ID 和 BGP 自治系统编号 (ASN)。

   对于 IPv4 流量，您还必须为 BGP 对等会话的每一端都指定公有 IPv4 地址，以及您通过 BGP 公布的公有 IPv4 路由。以下示例为 IPv4 流量创建公有虚拟接口。

   ```
   aws directconnect create-public-virtual-interface --connection-id dxcon-fg31dyv6 --new-public-virtual-interface virtualInterfaceName=PublicVirtualInterface,vlan=2000,asn=65000,amazonAddress=203.0.113.1/30,customerAddress=203.0.113.2/30,addressFamily=ipv4,routeFilterPrefixes=[{cidr=203.0.113.0/30},{cidr=203.0.113.4/30}]
   ```

   ```
   {
       "virtualInterfaceState": "verifying",
       "asn": 65000,
       "vlan": 2000,
       "customerAddress": "203.0.113.2/30",
       "ownerAccount": "123456789012",
       "connectionId": "dxcon-fg31dyv6",
       "addressFamily": "ipv4",
       "virtualGatewayId": "",
       "virtualInterfaceId": "dxvif-fgh0hcrk",
       "authKey": "asdf34example",
       "routeFilterPrefixes": [
           {
               "cidr": "203.0.113.0/30"
           },
           {
               "cidr": "203.0.113.4/30"
           }
       ],
       "location": "Example location",
       "bgpPeers": [
           {
               "bgpStatus": "down", 
               "customerAddress": "203.0.113.2/30", 
               "addressFamily": "ipv4", 
               "authKey": "asdf34example", 
               "bgpPeerState": "verifying", 
               "amazonAddress": "203.0.113.1/30", 
               "asn": 65000
           }
       ],
       "customerRouterConfig": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<logical_connection id=\"dxvif-fgh0hcrk\">\n  <vlan>2000</vlan>\n  <customer_address>203.0.113.2/30</customer_address>\n  <amazon_address>203.0.113.1/30</amazon_address>\n  <bgp_asn>65000</bgp_asn>\n  <bgp_auth_key>asdf34example</bgp_auth_key>\n  <amazon_bgp_asn>7224</amazon_bgp_asn>\n  <connection_type>public</connection_type>\n</logical_connection>\n",
       "amazonAddress": "203.0.113.1/30",
       "virtualInterfaceType": "public",
       "virtualInterfaceName": "PublicVirtualInterface"
   }
   ```

   要创建支持 IPv6 流量的公有虚拟接口，您可以指定将通过 BGP 公布的 IPv6 路由。您不能为对等会话指定 IPv6 地址；Amazon 向您分配 IPv6 地址。以下示例为 IPv6 流量创建公有虚拟接口。

   ```
   aws directconnect create-public-virtual-interface --connection-id dxcon-fg31dyv6 --new-public-virtual-interface virtualInterfaceName=PublicVirtualInterface,vlan=2000,asn=65000,addressFamily=ipv6,routeFilterPrefixes=[{cidr=2001:db8:64ce:ba00::/64},{cidr=2001:db8:64ce:ba01::/64}]
   ```

1. 要查看 XML 格式的路由器配置信息，请描述您创建的虚拟接口。使用 `--query` 参数可提取 `customerRouterConfig` 信息，使用 `--output` 参数可将文本排列到以制表符分隔的行中。

   ```
   aws directconnect describe-virtual-interfaces --virtual-interface-id dxvif-fgh0hcrk --query virtualInterfaces[*].customerRouterConfig --output text
   ```

    

   ```
   <?xml version="1.0" encoding="UTF-8"?>
   <logical_connection id="dxvif-fgh0hcrk">
     <vlan>2000</vlan>
     <customer_address>203.0.113.2/30</customer_address>
     <amazon_address>203.0.113.1/30</amazon_address>
     <bgp_asn>65000</bgp_asn>
     <bgp_auth_key>asdf34example</bgp_auth_key>
     <amazon_bgp_asn>7224</amazon_bgp_asn>
     <connection_type>public</connection_type>
   </logical_connection>
   ```