CreateNetworkInterface 搭配 CLI 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

CreateNetworkInterface 搭配 CLI 使用

下列程式碼範例示範如何使用 CreateNetworkInterface

CLI
AWS CLI

範例 1:指定網路介面的 IPv4 地址

下列create-network-interface範例會使用指定的主要 IPv4 地址為指定的子網路建立網路介面。

aws ec2 create-network-interface \ --subnet-id subnet-00a24d0d67acf6333 \ --description "my network interface" \ --groups sg-09dfba7ed20cda78b \ --private-ip-address 10.0.8.17

輸出:

{ "NetworkInterface": { "AvailabilityZone": "us-west-2a", "Description": "my network interface", "Groups": [ { "GroupName": "my-security-group", "GroupId": "sg-09dfba7ed20cda78b" } ], "InterfaceType": "interface", "Ipv6Addresses": [], "MacAddress": "06:6a:0f:9a:49:37", "NetworkInterfaceId": "eni-0492b355f0cf3b3f8", "OwnerId": "123456789012", "PrivateDnsName": "ip-10-0-8-18.us-west-2.compute.internal", "PrivateIpAddress": "10.0.8.17", "PrivateIpAddresses": [ { "Primary": true, "PrivateDnsName": "ip-10-0-8-17.us-west-2.compute.internal", "PrivateIpAddress": "10.0.8.17" } ], "RequesterId": "AIDA4Z3Y7GSXTMEXAMPLE", "RequesterManaged": false, "SourceDestCheck": true, "Status": "pending", "SubnetId": "subnet-00a24d0d67acf6333", "TagSet": [], "VpcId": "vpc-02723a0feeeb9d57b" } }

範例 2:使用 IPv4 地址和 IPv6 地址建立網路介面

下列create-network-interface範例會使用 Amazon EC2 選取的 IPv4 地址和 IPv6 地址,為指定的子網路建立網路介面。

aws ec2 create-network-interface \ --subnet-id subnet-00a24d0d67acf6333 \ --description "my dual stack network interface" \ --ipv6-address-count 1 \ --groups sg-09dfba7ed20cda78b

輸出:

{ "NetworkInterface": { "AvailabilityZone": "us-west-2a", "Description": "my dual stack network interface", "Groups": [ { "GroupName": "my-security-group", "GroupId": "sg-09dfba7ed20cda78b" } ], "InterfaceType": "interface", "Ipv6Addresses": [ { "Ipv6Address": "2600:1f13:cfe:3650:a1dc:237c:393a:4ba7", "IsPrimaryIpv6": false } ], "MacAddress": "06:b8:68:d2:b2:2d", "NetworkInterfaceId": "eni-05da417453f9a84bf", "OwnerId": "123456789012", "PrivateDnsName": "ip-10-0-8-18.us-west-2.compute.internal", "PrivateIpAddress": "10.0.8.18", "PrivateIpAddresses": [ { "Primary": true, "PrivateDnsName": "ip-10-0-8-18.us-west-2.compute.internal", "PrivateIpAddress": "10.0.8.18" } ], "RequesterId": "AIDA4Z3Y7GSXTMEXAMPLE", "RequesterManaged": false, "SourceDestCheck": true, "Status": "pending", "SubnetId": "subnet-00a24d0d67acf6333", "TagSet": [], "VpcId": "vpc-02723a0feeeb9d57b", "Ipv6Address": "2600:1f13:cfe:3650:a1dc:237c:393a:4ba7" } }

範例 3:使用連線追蹤組態選項建立網路介面

下列create-network-interface範例會建立網路介面並設定閒置連線追蹤逾時。

aws ec2 create-network-interface \ --subnet-id subnet-00a24d0d67acf6333 \ --groups sg-02e57dbcfe0331c1b \ --connection-tracking-specification TcpEstablishedTimeout=86400,UdpTimeout=60

輸出:

{ "NetworkInterface": { "AvailabilityZone": "us-west-2a", "ConnectionTrackingConfiguration": { "TcpEstablishedTimeout": 86400, "UdpTimeout": 60 }, "Description": "", "Groups": [ { "GroupName": "my-security-group", "GroupId": "sg-02e57dbcfe0331c1b" } ], "InterfaceType": "interface", "Ipv6Addresses": [], "MacAddress": "06:4c:53:de:6d:91", "NetworkInterfaceId": "eni-0c133586e08903d0b", "OwnerId": "123456789012", "PrivateDnsName": "ip-10-0-8-94.us-west-2.compute.internal", "PrivateIpAddress": "10.0.8.94", "PrivateIpAddresses": [ { "Primary": true, "PrivateDnsName": "ip-10-0-8-94.us-west-2.compute.internal", "PrivateIpAddress": "10.0.8.94" } ], "RequesterId": "AIDA4Z3Y7GSXTMEXAMPLE", "RequesterManaged": false, "SourceDestCheck": true, "Status": "pending", "SubnetId": "subnet-00a24d0d67acf6333", "TagSet": [], "VpcId": "vpc-02723a0feeeb9d57b" } }

範例 4:建立 Elastic Fabric Adapter

下列create-network-interface範例會建立 EFA。

aws ec2 create-network-interface \ --interface-type efa \ --subnet-id subnet-00a24d0d67acf6333 \ --description "my efa" \ --groups sg-02e57dbcfe0331c1b

輸出:

{ "NetworkInterface": { "AvailabilityZone": "us-west-2a", "Description": "my efa", "Groups": [ { "GroupName": "my-efa-sg", "GroupId": "sg-02e57dbcfe0331c1b" } ], "InterfaceType": "efa", "Ipv6Addresses": [], "MacAddress": "06:d7:a4:f7:4d:57", "NetworkInterfaceId": "eni-034acc2885e862b65", "OwnerId": "123456789012", "PrivateDnsName": "ip-10-0-8-180.us-west-2.compute.internal", "PrivateIpAddress": "10.0.8.180", "PrivateIpAddresses": [ { "Primary": true, "PrivateDnsName": "ip-10-0-8-180.us-west-2.compute.internal", "PrivateIpAddress": "10.0.8.180" } ], "RequesterId": "AIDA4Z3Y7GSXTMEXAMPLE", "RequesterManaged": false, "SourceDestCheck": true, "Status": "pending", "SubnetId": "subnet-00a24d0d67acf6333", "TagSet": [], "VpcId": "vpc-02723a0feeeb9d57b" } }

如需詳細資訊,請參閱 Amazon EC2 使用者指南中的彈性網路介面

PowerShell
for PowerShell 工具

範例 1:此範例會建立指定的網路介面。

New-EC2NetworkInterface -SubnetId subnet-1a2b3c4d -Description "my network interface" -Group sg-12345678 -PrivateIpAddress 10.0.0.17

輸出:

Association : Attachment : AvailabilityZone : us-west-2c Description : my network interface Groups : {my-security-group} MacAddress : 0a:72:bc:1a:cd:7f NetworkInterfaceId : eni-12345678 OwnerId : 123456789012 PrivateDnsName : ip-10-0-0-17.us-west-2.compute.internal PrivateIpAddress : 10.0.0.17 PrivateIpAddresses : {} RequesterId : RequesterManaged : False SourceDestCheck : True Status : pending SubnetId : subnet-1a2b3c4d TagSet : {} VpcId : vpc-12345678