View a markdown version of this page

Amazon EC2 examples using AWS CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Amazon EC2 examples using AWS CLI

The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with Amazon EC2.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use accept-address-transfer.

AWS CLI

To accept an Elastic IP address transferred to your account

The following accept-address-transfer example accepts the transfer of the specified Elastic IP address to your account.

aws ec2 accept-address-transfer \ --address 100.21.184.216

Output:

{ "AddressTransfer": { "PublicIp": "100.21.184.216", "AllocationId": "eipalloc-09ad461b0d03f6aaf", "TransferAccountId": "123456789012", "TransferOfferExpirationTimestamp": "2023-02-22T20:51:10.000Z", "TransferOfferAcceptedTimestamp": "2023-02-22T22:52:54.000Z", "AddressTransferStatus": "accepted" } }

For more information, see Transfer Elastic IP addresses in the Amazon VPC User Guide.

The following code example shows how to use accept-reserved-instances-exchange-quote.

AWS CLI

To perform a Convertible Reserved Instance exchange

This example performs an exchange of the specified Convertible Reserved Instances.

Command:

aws ec2 accept-reserved-instances-exchange-quote --reserved-instance-ids 7b8750c3-397e-4da4-bbcb-a45ebexample --target-configurations OfferingId=b747b472-423c-48f3-8cee-679bcexample

Output:

{ "ExchangeId": "riex-e68ed3c1-8bc8-4c17-af77-811afexample" }

The following code example shows how to use accept-transit-gateway-peering-attachment.

AWS CLI

To accept a transit gateway peering attachment

The following accept-transit-gateway-peering-attachment example accepts the specified transit gateway peering attachment. The --region parameter specifies the Region that the accepter transit gateway is located in.

aws ec2 accept-transit-gateway-peering-attachment \ --transit-gateway-attachment-id tgw-attach-4455667788aabbccd \ --region us-east-2

Output:

{ "TransitGatewayPeeringAttachment": { "TransitGatewayAttachmentId": "tgw-attach-4455667788aabbccd", "RequesterTgwInfo": { "TransitGatewayId": "tgw-123abc05e04123abc", "OwnerId": "123456789012", "Region": "us-west-2" }, "AccepterTgwInfo": { "TransitGatewayId": "tgw-11223344aabbcc112", "OwnerId": "123456789012", "Region": "us-east-2" }, "State": "pending", "CreationTime": "2019-12-09T11:38:31.000Z" } }

For more information, see Transit Gateway Peering Attachments in the Transit Gateways Guide.

The following code example shows how to use accept-transit-gateway-vpc-attachment.

AWS CLI

To accept a request to attach a VPC to a transit gateway.

The following accept-transit-gateway-vpc-attachment example accepts the request forte specified attachment.

aws ec2 accept-transit-gateway-vpc-attachment \ --transit-gateway-attachment-id tgw-attach-0a34fe6b4fEXAMPLE

Output:

{ "TransitGatewayVpcAttachment": { "TransitGatewayAttachmentId": "tgw-attach-0a34fe6b4fEXAMPLE", "TransitGatewayId": "tgw-0262a0e521EXAMPLE", "VpcId": "vpc-07e8ffd50fEXAMPLE", "VpcOwnerId": "123456789012", "State": "pending", "SubnetIds": [ "subnet-0752213d59EXAMPLE" ], "CreationTime": "2019-07-10T17:33:46.000Z", "Options": { "DnsSupport": "enable", "Ipv6Support": "disable" } } }

For more information, see Transit Gateway Attachments to a VPC in the Transit Gateways Guide.

The following code example shows how to use accept-vpc-endpoint-connections.

AWS CLI

To accept an interface endpoint connection request

This example accepts the specified endpoint connection request for the specified endpoint service.

Command:

aws ec2 accept-vpc-endpoint-connections --service-id vpce-svc-03d5ebb7d9579a2b3 --vpc-endpoint-ids vpce-0c1308d7312217abc

Output:

{ "Unsuccessful": [] }

The following code example shows how to use accept-vpc-peering-connection.

AWS CLI

To accept a VPC peering connection

This example accepts the specified VPC peering connection request.

Command:

aws ec2 accept-vpc-peering-connection --vpc-peering-connection-id pcx-1a2b3c4d

Output:

{ "VpcPeeringConnection": { "Status": { "Message": "Provisioning", "Code": "provisioning" }, "Tags": [], "AccepterVpcInfo": { "OwnerId": "444455556666", "VpcId": "vpc-44455566", "CidrBlock": "10.0.1.0/28" }, "VpcPeeringConnectionId": "pcx-1a2b3c4d", "RequesterVpcInfo": { "OwnerId": "444455556666", "VpcId": "vpc-111abc45", "CidrBlock": "10.0.0.0/28" } } }

The following code example shows how to use advertise-byoip-cidr.

AWS CLI

To advertise an address range

The following advertise-byoip-cidr example advertises the specified public IPv4 address range.

aws ec2 advertise-byoip-cidr \ --cidr 203.0.113.25/24

Output:

{ "ByoipCidr": { "Cidr": "203.0.113.25/24", "StatusMessage": "ipv4pool-ec2-1234567890abcdef0", "State": "provisioned" } }

The following code example shows how to use allocate-address.

AWS CLI

Example 1: To allocate an Elastic IP address from Amazon's address pool

The following allocate-address example allocates an Elastic IP address. Amazon EC2 selects the address from Amazon's address pool.

aws ec2 allocate-address

Output:

{ "PublicIp": "70.224.234.241", "AllocationId": "eipalloc-01435ba59eEXAMPLE", "PublicIpv4Pool": "amazon", "NetworkBorderGroup": "us-west-2", "Domain": "vpc" }

For more information, see Elastic IP addresses in the Amazon EC2 User Guide.

Example 2: To allocate an Elastic IP address and associate it with a network border group

The following allocate-address example allocates an Elastic IP address and associates it with the specified network border group.

aws ec2 allocate-address \ --network-border-group us-west-2-lax-1

Output:

{ "PublicIp": "70.224.234.241", "AllocationId": "eipalloc-e03dd489ceEXAMPLE", "PublicIpv4Pool": "amazon", "NetworkBorderGroup": "us-west-2-lax-1", "Domain": "vpc" }

For more information, see Elastic IP addresses in the Amazon EC2 User Guide.

Example 3: To allocate an Elastic IP address from an address pool that you own

The following allocate-address example allocates an Elastic IP address from an address pool that you have brought to your Amazon Web Services account. Amazon EC2 selects the address from the address pool.

aws ec2 allocate-address \ --public-ipv4-pool ipv4pool-ec2-1234567890abcdef0

Output:

{ "AllocationId": "eipalloc-02463d08ceEXAMPLE", "NetworkBorderGroup": "us-west-2", "CustomerOwnedIp": "18.218.95.81", "CustomerOwnedIpv4Pool": "ipv4pool-ec2-1234567890abcdef0", "Domain": "vpc" "NetworkBorderGroup": "us-west-2", }

For more information, see Elastic IP addresses in the Amazon EC2 User Guide.

Example 4: To allocate an Elastic IP address from an IPAM pool

The following allocate-address example allocates a specific /32 Elastic IP address from an Amazon VPC IP Address Manager (IPAM) pool.

aws ec2 allocate-address \ --region us-east-1 \ --ipam-pool-id ipam-pool-1234567890abcdef0 \ --address 192.0.2.0

Output:

{ "PublicIp": "192.0.2.0", "AllocationId": "eipalloc-abcdef01234567890", "PublicIpv4Pool": "ipam-pool-1234567890abcdef0", "NetworkBorderGroup": "us-east-1", "Domain": "vpc" }

For more information, see Allocate sequential Elastic IP addresses from an IPAM pool in the Amazon VPC IPAM User Guide.

The following code example shows how to use allocate-hosts.

AWS CLI

Example 1: To allocate a Dedicated Host

The following allocate-hosts example allocates a single Dedicated Host in the eu-west-1a Availability Zone, onto which you can launch m5.large instances. By default, the Dedicated Host accepts only target instance launches, and does not support host recovery.

aws ec2 allocate-hosts \ --instance-type m5.large \ --availability-zone eu-west-1a \ --quantity 1

Output:

{ "HostIds": [ "h-07879acf49EXAMPLE" ] }

Example 2: To allocate a Dedicated Host with auto-placement and host recovery enabled

The following allocate-hosts example allocates a single Dedicated Host in the eu-west-1a Availability Zone with auto-placement and host recovery enabled.

aws ec2 allocate-hosts \ --instance-type m5.large \ --availability-zone eu-west-1a \ --auto-placement on \ --host-recovery on \ --quantity 1

Output:

{ "HostIds": [ "h-07879acf49EXAMPLE" ] }

Example 3: To allocate a Dedicated Host with tags

The following allocate-hosts example allocates a single Dedicated Host and applies a tag with a key named purpose and a value of production.

aws ec2 allocate-hosts \ --instance-type m5.large \ --availability-zone eu-west-1a \ --quantity 1 \ --tag-specifications 'ResourceType=dedicated-host,Tags={Key=purpose,Value=production}'

Output:

{ "HostIds": [ "h-07879acf49EXAMPLE" ] }

For more information, see Allocate a Dedicated Host in the Amazon EC2 User Guide.

  • For API details, see AllocateHosts in AWS CLI Command Reference.

The following code example shows how to use allocate-ipam-pool-cidr.

AWS CLI

To allocate a CIDR from an IPAM pool

The following allocate-ipam-pool-cidr example allocates a CIDR from an IPAM pool.

(Linux):

aws ec2 allocate-ipam-pool-cidr \ --ipam-pool-id ipam-pool-0533048da7d823723 \ --netmask-length 24

(Windows):

aws ec2 allocate-ipam-pool-cidr ^ --ipam-pool-id ipam-pool-0533048da7d823723 ^ --netmask-length 24

Output:

{ "IpamPoolAllocation": { "Cidr": "10.0.0.0/24", "IpamPoolAllocationId": "ipam-pool-alloc-018ecc28043b54ba38e2cd99943cebfbd", "ResourceType": "custom", "ResourceOwner": "123456789012" } }

For more information, see Manually allocate a CIDR to a pool to reserve IP address space in the Amazon VPC IPAM User Guide.

The following code example shows how to use apply-security-groups-to-client-vpn-target-network.

AWS CLI

To apply security groups to a target network for a Client VPN endpoint

The following apply-security-groups-to-client-vpn-target-network example applies security group sg-01f6e627a89f4db32 to the association between the specified target network and Client VPN endpoint.

aws ec2 apply-security-groups-to-client-vpn-target-network \ --security-group-ids sg-01f6e627a89f4db32 \ --vpc-id vpc-0e2110c2f324332e0 \ --client-vpn-endpoint-id cvpn-endpoint-123456789123abcde

Output:

{ "SecurityGroupIds": [ "sg-01f6e627a89f4db32" ] }

For more information, see Target Networks in the AWS Client VPN Administrator Guide.

The following code example shows how to use assign-ipv6-addresses.

AWS CLI

To assign specific IPv6 addresses to a network interface

This example assigns the specified IPv6 addresses to the specified network interface.

Command:

aws ec2 assign-ipv6-addresses --network-interface-id eni-38664473 --ipv6-addresses 2001:db8:1234:1a00:3304:8879:34cf:4071 2001:db8:1234:1a00:9691:9503:25ad:1761

Output:

{ "AssignedIpv6Addresses": [ "2001:db8:1234:1a00:3304:8879:34cf:4071", "2001:db8:1234:1a00:9691:9503:25ad:1761" ], "NetworkInterfaceId": "eni-38664473" }

To assign IPv6 addresses that Amazon selects to a network interface

This example assigns two IPv6 addresses to the specified network interface. Amazon automatically assigns these IPv6 addresses from the available IPv6 addresses in the IPv6 CIDR block range of the subnet.

Command:

aws ec2 assign-ipv6-addresses --network-interface-id eni-38664473 --ipv6-address-count 2

Output:

{ "AssignedIpv6Addresses": [ "2001:db8:1234:1a00:3304:8879:34cf:4071", "2001:db8:1234:1a00:9691:9503:25ad:1761" ], "NetworkInterfaceId": "eni-38664473" }

The following code example shows how to use assign-private-ip-addresses.

AWS CLI

To assign a specific secondary private IP address a network interface

This example assigns the specified secondary private IP address to the specified network interface. If the command succeeds, no output is returned.

Command:

aws ec2 assign-private-ip-addresses --network-interface-id eni-e5aa89a3 --private-ip-addresses 10.0.0.82

To assign secondary private IP addresses that Amazon EC2 selects to a network interface

This example assigns two secondary private IP addresses to the specified network interface. Amazon EC2 automatically assigns these IP addresses from the available IP addresses in the CIDR block range of the subnet the network interface is associated with. If the command succeeds, no output is returned.

Command:

aws ec2 assign-private-ip-addresses --network-interface-id eni-e5aa89a3 --secondary-private-ip-address-count 2

The following code example shows how to use assign-private-nat-gateway-address.

AWS CLI

To assign private IP addresses to your private NAT gateway

The following assign-private-nat-gateway-address example assigns two private IP addresses to the specified private NAT gateway.

aws ec2 assign-private-nat-gateway-address \ --nat-gateway-id nat-1234567890abcdef0 \ --private-ip-address-count 2

Output:

{ "NatGatewayId": "nat-1234567890abcdef0", "NatGatewayAddresses": [ { "NetworkInterfaceId": "eni-0065a61b324d1897a", "IsPrimary": false, "Status": "assigning" }, { "NetworkInterfaceId": "eni-0065a61b324d1897a", "IsPrimary": false, "Status": "assigning" } ] }

For more information, see NAT gateways in the Amazon VPC User Guide.

The following code example shows how to use associate-address.

AWS CLI

Example 1: To associate an Elastic IP address with an instance

The following associate-address example associates an Elastic IP address with the specified EC2 instance.

aws ec2 associate-address \ --instance-id i-0b263919b6498b123 \ --allocation-id eipalloc-64d5890a

Output:

{ "AssociationId": "eipassoc-2bebb745" }

Example 2: To associate an Elastic IP address with a network interface

The following associate-address example associates the specified Elastic IP address with the specified network interface.

aws ec2 associate-address --allocation-id eipalloc-64d5890a \ --network-interface-id eni-1a2b3c4d

Output:

{ "AssociationId": "eipassoc-2bebb745" }

Example 3: To associate an Elastic IP address with a private IP address

The following associate-address example associates the specified Elastic IP address with the specified private IP address in the specified network interface.

aws ec2 associate-address \ --allocation-id eipalloc-64d5890a \ --network-interface-id eni-1a2b3c4d \ --private-ip-address 10.0.0.85

Output:

{ "AssociationId": "eipassoc-2bebb745" }

For more information, see Elastic IP addresses in the Amazon EC2 User Guide.

The following code example shows how to use associate-client-vpn-target-network.

AWS CLI

To associate a target network with a Client VPN endpoint

The following associate-client-vpn-target-network example associates a subnet with the specified Client VPN endpoint.

aws ec2 associate-client-vpn-target-network \ --subnet-id subnet-0123456789abcabca \ --client-vpn-endpoint-id cvpn-endpoint-123456789123abcde

Output:

{ "AssociationId": "cvpn-assoc-12312312312312312", "Status": { "Code": "associating" } }

For more information, see Target Networks in the AWS Client VPN Administrator Guide.

The following code example shows how to use associate-dhcp-options.

AWS CLI

To associate a DHCP options set with your VPC

This example associates the specified DHCP options set with the specified VPC. If the command succeeds, no output is returned.

Command:

aws ec2 associate-dhcp-options --dhcp-options-id dopt-d9070ebb --vpc-id vpc-a01106c2

To associate the default DHCP options set with your VPC

This example associates the default DHCP options set with the specified VPC. If the command succeeds, no output is returned.

Command:

aws ec2 associate-dhcp-options --dhcp-options-id default --vpc-id vpc-a01106c2

The following code example shows how to use associate-iam-instance-profile.

AWS CLI

To associate an IAM instance profile with an instance

This example associates an IAM instance profile named admin-role with instance i-123456789abcde123.

Command:

aws ec2 associate-iam-instance-profile --instance-id i-123456789abcde123 --iam-instance-profile Name=admin-role

Output:

{ "IamInstanceProfileAssociation": { "InstanceId": "i-123456789abcde123", "State": "associating", "AssociationId": "iip-assoc-0e7736511a163c209", "IamInstanceProfile": { "Id": "AIPAJBLK7RKJKWDXVHIEC", "Arn": "arn:aws:iam::123456789012:instance-profile/admin-role" } } }

The following code example shows how to use associate-instance-event-window.

AWS CLI

Example 1: To associate one or more instances with an event window

The following associate-instance-event-window example associates one or more instances with an event window.

aws ec2 associate-instance-event-window \ --region us-east-1 \ --instance-event-window-id iew-0abcdef1234567890 \ --association-target "InstanceIds=i-1234567890abcdef0,i-0598c7d356eba48d7"

Output:

{ "InstanceEventWindow": { "InstanceEventWindowId": "iew-0abcdef1234567890", "Name": "myEventWindowName", "CronExpression": "* 21-23 * * 2,3", "AssociationTarget": { "InstanceIds": [ "i-1234567890abcdef0", "i-0598c7d356eba48d7" ], "Tags": [], "DedicatedHostIds": [] }, "State": "creating" } }

For event window constraints, see Considerations in the Scheduled Events section of the Amazon EC2 User Guide.

Example 2: To associate instance tags with an event window

The following associate-instance-event-window example associates instance tags with an event window. Enter an instance-event-window-id parameter to specify the event window. To associate instance tags, specify the association-target parameter, and for the parameter value, specify one or more tags.

aws ec2 associate-instance-event-window \ --region us-east-1 \ --instance-event-window-id iew-0abcdef1234567890 \ --association-target "InstanceTags=[{Key=k2,Value=v2},{Key=k1,Value=v1}]"

Output:

{ "InstanceEventWindow": { "InstanceEventWindowId": "iew-0abcdef1234567890", "Name": "myEventWindowName", "CronExpression": "* 21-23 * * 2,3", "AssociationTarget": { "InstanceIds": [], "Tags": [ { "Key": "k2", "Value": "v2" }, { "Key": "k1", "Value": "v1" } ], "DedicatedHostIds": [] }, "State": "creating" } }

For event window constraints, see Considerations in the Scheduled Events section of the Amazon EC2 User Guide.

Example 3: To associate a Dedicated Host with an event window

The following associate-instance-event-window example associates a Dedicated Host with an event window. Enter an instance-event-window-id parameter to specify the event window. To associate a Dedicated Host, specify the --association-target parameter, and for the parameter values, specify one of more Dedicated Host IDs.

aws ec2 associate-instance-event-window \ --region us-east-1 \ --instance-event-window-id iew-0abcdef1234567890 \ --association-target "DedicatedHostIds=h-029fa35a02b99801d"

Output:

{ "InstanceEventWindow": { "InstanceEventWindowId": "iew-0abcdef1234567890", "Name": "myEventWindowName", "CronExpression": "* 21-23 * * 2,3", "AssociationTarget": { "InstanceIds": [], "Tags": [], "DedicatedHostIds": [ "h-029fa35a02b99801d" ] }, "State": "creating" } }

For event window constraints, see Considerations in the Scheduled Events section of the Amazon EC2 User Guide.

The following code example shows how to use associate-ipam-resource-discovery.

AWS CLI

To associate a resource discovery with an IPAM

In this example, you are an IPAM delegated admin and a resource discovery has been created and shared with you by another AWS account so that you can use IPAM to manage and monitor resource CIDRs owned by the other account.

Note

To complete this request, you'll need the resource discovery ID which you can get with describe-ipam-resource-discoveries and the IPAM ID which you can get with describe-ipams.The resource discovery that you are associating must have first been shared with your account using AWS RAM.The --region you enter must match the home Region of the IPAM you are associating it with.

The following associate-ipam-resource-discovery example associates a resource discovery with an IPAM.

aws ec2 associate-ipam-resource-discovery \ --ipam-id ipam-005f921c17ebd5107 \ --ipam-resource-discovery-id ipam-res-disco-03e0406de76a044ee \ --tag-specifications 'ResourceType=ipam-resource-discovery,Tags=[{Key=cost-center,Value=cc123}]' \ --region us-east-1

Output:

{ { "IpamResourceDiscoveryAssociation": { "OwnerId": "320805250157", "IpamResourceDiscoveryAssociationId": "ipam-res-disco-assoc-04382a6346357cf82", "IpamResourceDiscoveryAssociationArn": "arn:aws:ec2::320805250157:ipam-resource-discovery-association/ipam-res-disco-assoc-04382a6346357cf82", "IpamResourceDiscoveryId": "ipam-res-disco-0365d2977fc1672fe", "IpamId": "ipam-005f921c17ebd5107", "IpamArn": "arn:aws:ec2::320805250157:ipam/ipam-005f921c17ebd5107", "IpamRegion": "us-east-1", "IsDefault": false, "ResourceDiscoveryStatus": "active", "State": "associate-in-progress", "Tags": [] } } }

Once you associate a resource discovery, you can monitor and/or manage the IP addresses of resources created by the other accounts. For more information, see Integrate IPAM with accounts outside of your organization in the Amazon VPC IPAM User Guide.

The following code example shows how to use associate-nat-gateway-address.

AWS CLI

To associate an Elastic IP address with a public NAT gateway

The following associate-nat-gateway-address example associates the specified Elastic IP address with the specified public NAT gateway. AWS automatically assigns a secondary private IPv4 address.

aws ec2 associate-nat-gateway-address \ --nat-gateway-id nat-1234567890abcdef0 \ --allocation-ids eipalloc-0be6ecac95EXAMPLE

Output:

{ "NatGatewayId": "nat-1234567890abcdef0", "NatGatewayAddresses": [ { "AllocationId": "eipalloc-0be6ecac95EXAMPLE", "NetworkInterfaceId": "eni-09cc4b2558794f7f9", "IsPrimary": false, "Status": "associating" } ] }

For more information, see NAT gateways in the Amazon VPC User Guide.

The following code example shows how to use associate-route-table.

AWS CLI

To associate a route table with a subnet

This example associates the specified route table with the specified subnet.

Command:

aws ec2 associate-route-table --route-table-id rtb-22574640 --subnet-id subnet-9d4a7b6c

Output:

{ "AssociationId": "rtbassoc-781d0d1a" }

The following code example shows how to use associate-security-group-vpc.

AWS CLI

To associate a security group with another VPC

The following associate-security-group-vpc example associates the specified security group with the specified VPC.

aws ec2 associate-security-group-vpc \ --group-id sg-04dbb43907d3f8a78 \ --vpc-id vpc-0bf4c2739bc05a694

Output:

{ "State": "associating" }

For more information, see Associate security groups with multiple VPCs in the Amazon VPC User Guide.

The following code example shows how to use associate-subnet-cidr-block.

AWS CLI

To associate an IPv6 CIDR block with a subnet

This example associates an IPv6 CIDR block with the specified subnet.

Command:

aws ec2 associate-subnet-cidr-block --subnet-id subnet-5f46ec3b --ipv6-cidr-block 2001:db8:1234:1a00::/64

Output:

{ "SubnetId": "subnet-5f46ec3b", "Ipv6CidrBlockAssociation": { "Ipv6CidrBlock": "2001:db8:1234:1a00::/64", "AssociationId": "subnet-cidr-assoc-3aa54053", "Ipv6CidrBlockState": { "State": "associating" } } }

The following code example shows how to use associate-transit-gateway-multicast-domain.

AWS CLI

To associate a transit gateway with a multicast domain

The following associate-transit-gateway-multicast-domain example associates the specified subnet and attachment with the specified multicast domain.

aws ec2 associate-transit-gateway-multicast-domain \ --transit-gateway-multicast-domain-id tgw-mcast-domain-0c4905cef79d6e597 \ --transit-gateway-attachment-id tgw-attach-028c1dd0f8f5cbe8e \ --subnet-ids subnet-000de86e3b49c932a \ --transit-gateway-multicast-domain-id tgw-mcast-domain-0c4905cef7EXAMPLE

Output:

{ "Associations": { "TransitGatewayMulticastDomainId": "tgw-mcast-domain-0c4905cef79d6e597", "TransitGatewayAttachmentId": "tgw-attach-028c1dd0f8f5cbe8e", "ResourceId": "vpc-01128d2c240c09bd5", "ResourceType": "vpc", "Subnets": [ { "SubnetId": "subnet-000de86e3b49c932a", "State": "associating" } ] } }

For more information, see Multicast domains in the Transit Gateways Guide.

The following code example shows how to use associate-transit-gateway-route-table.

AWS CLI

To associate a transit gateway route table with a transit gateway attachment

The following example associates the specified transit gateway route table with the specified VPC attachment.

aws ec2 associate-transit-gateway-route-table \ --transit-gateway-route-table-id tgw-rtb-002573ed1eEXAMPLE \ --transit-gateway-attachment-id tgw-attach-0b5968d3b6EXAMPLE

Output:

{ "Association": { "TransitGatewayRouteTableId": "tgw-rtb-002573ed1eEXAMPLE", "TransitGatewayAttachmentId": "tgw-attach-0b5968d3b6EXAMPLE", "ResourceId": "vpc-0065acced4EXAMPLE", "ResourceType": "vpc", "State": "associating" } }

For more information, see Associate a Transit Gateway Route Table in the AWS Transit Gateways Guide.

The following code example shows how to use associate-vpc-cidr-block.

AWS CLI

Example 1: To associate an Amazon-provided IPv6 CIDR block with a VPC

The following associate-vpc-cidr-block example associates an IPv6 CIDR block with the specified VPC.:

aws ec2 associate-vpc-cidr-block \ --amazon-provided-ipv6-cidr-block \ --ipv6-cidr-block-network-border-group us-west-2-lax-1 \ --vpc-id vpc-8EXAMPLE

Output:

{ "Ipv6CidrBlockAssociation": { "AssociationId": "vpc-cidr-assoc-0838ce7d9dEXAMPLE", "Ipv6CidrBlockState": { "State": "associating" }, "NetworkBorderGroup": "us-west-2-lax-1" }, "VpcId": "vpc-8EXAMPLE" }

Example 2:To associate an additional IPv4 CIDR block with a VPC

The following associate-vpc-cidr-block example associates the IPv4 CIDR block 10.2.0.0/16 with the specified VPC.

aws ec2 associate-vpc-cidr-block \ --vpc-id vpc-1EXAMPLE \ --cidr-block 10.2.0.0/16

Output:

{ "CidrBlockAssociation": { "AssociationId": "vpc-cidr-assoc-2EXAMPLE", "CidrBlock": "10.2.0.0/16", "CidrBlockState": { "State": "associating" } }, "VpcId": "vpc-1EXAMPLE" }

The following code example shows how to use attach-classic-link-vpc.

AWS CLI

To link (attach) an EC2-Classic instance to a VPC

This example links instance i-1234567890abcdef0 to VPC vpc-88888888 through the VPC security group sg-12312312.

Command:

aws ec2 attach-classic-link-vpc --instance-id i-1234567890abcdef0 --vpc-id vpc-88888888 --groups sg-12312312

Output:

{ "Return": true }

The following code example shows how to use attach-internet-gateway.

AWS CLI

To attach an internet gateway to your VPC

The following attach-internet-gateway example attaches the specified internet gateway to the specific VPC.

aws ec2 attach-internet-gateway \ --internet-gateway-id igw-0d0fb496b3EXAMPLE \ --vpc-id vpc-0a60eb65b4EXAMPLE

This command produces no output.

For more information, see Internet gateways in the Amazon VPC User Guide.

The following code example shows how to use attach-network-interface.

AWS CLI

Example 1: To attach a network interface to an instance

The following attach-network-interface example attaches the specified network interface to the specified instance.

aws ec2 attach-network-interface \ --network-interface-id eni-0dc56a8d4640ad10a \ --instance-id i-1234567890abcdef0 \ --device-index 1

Output:

{ "AttachmentId": "eni-attach-01a8fc87363f07cf9" }

For more information, see Elastic network interfaces in the Amazon EC2 User Guide.

Example 2: To attach a network interface to an instance with multiple network cards

The following attach-network-interface example attaches the specified network interface to the specified instance and network card.

aws ec2 attach-network-interface \ --network-interface-id eni-07483b1897541ad83 \ --instance-id i-01234567890abcdef \ --network-card-index 1 \ --device-index 1

Output:

{ "AttachmentId": "eni-attach-0fbd7ee87a88cd06c" }

For more information, see Elastic network interfaces in the Amazon EC2 User Guide.

The following code example shows how to use attach-verified-access-trust-provider.

AWS CLI

To attach a trust provider to an instance

The following attach-verified-access-trust-provider example attaches the specified Verified Access trust provider to the specified Verified Access instance.

aws ec2 attach-verified-access-trust-provider \ --verified-access-instance-id vai-0ce000c0b7643abea \ --verified-access-trust-provider-id vatp-0bb32de759a3e19e7

Output:

{ "VerifiedAccessTrustProvider": { "VerifiedAccessTrustProviderId": "vatp-0bb32de759a3e19e7", "Description": "", "TrustProviderType": "user", "UserTrustProviderType": "iam-identity-center", "PolicyReferenceName": "idc", "CreationTime": "2023-08-25T19:00:38", "LastUpdatedTime": "2023-08-25T19:00:38" }, "VerifiedAccessInstance": { "VerifiedAccessInstanceId": "vai-0ce000c0b7643abea", "Description": "", "VerifiedAccessTrustProviders": [ { "VerifiedAccessTrustProviderId": "vatp-0bb32de759a3e19e7", "TrustProviderType": "user", "UserTrustProviderType": "iam-identity-center" } ], "CreationTime": "2023-08-25T18:27:56", "LastUpdatedTime": "2023-08-25T18:27:56" } }

For more information, see Verified Access instances in the AWS Verified Access User Guide.

The following code example shows how to use attach-volume.