There are more AWS SDK examples available in the AWS Doc SDK Examples
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-transferexample accepts the transfer of the specified Elastic IP address to your account.aws ec2 accept-address-transfer \ --address100.21.184.216Output:
{ "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.
-
For API details, see AcceptAddressTransfer
in AWS CLI Command Reference.
-
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-ids7b8750c3-397e-4da4-bbcb-a45ebexample--target-configurationsOfferingId=b747b472-423c-48f3-8cee-679bcexampleOutput:
{ "ExchangeId": "riex-e68ed3c1-8bc8-4c17-af77-811afexample" }-
For API details, see AcceptReservedInstancesExchangeQuote
in AWS CLI Command Reference.
-
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-attachmentexample accepts the specified transit gateway peering attachment. The--regionparameter specifies the Region that the accepter transit gateway is located in.aws ec2 accept-transit-gateway-peering-attachment \ --transit-gateway-attachment-idtgw-attach-4455667788aabbccd\ --regionus-east-2Output:
{ "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.
-
For API details, see AcceptTransitGatewayPeeringAttachment
in AWS CLI Command Reference.
-
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-attachmentexample accepts the request forte specified attachment.aws ec2 accept-transit-gateway-vpc-attachment \ --transit-gateway-attachment-idtgw-attach-0a34fe6b4fEXAMPLEOutput:
{ "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.
-
For API details, see AcceptTransitGatewayVpcAttachment
in AWS CLI Command Reference.
-
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-idvpce-svc-03d5ebb7d9579a2b3--vpc-endpoint-idsvpce-0c1308d7312217abcOutput:
{ "Unsuccessful": [] }-
For API details, see AcceptVpcEndpointConnections
in AWS CLI Command Reference.
-
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-idpcx-1a2b3c4dOutput:
{ "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" } } }-
For API details, see AcceptVpcPeeringConnection
in AWS CLI Command Reference.
-
The following code example shows how to use advertise-byoip-cidr.
- AWS CLI
-
To advertise an address range
The following
advertise-byoip-cidrexample advertises the specified public IPv4 address range.aws ec2 advertise-byoip-cidr \ --cidr203.0.113.25/24Output:
{ "ByoipCidr": { "Cidr": "203.0.113.25/24", "StatusMessage": "ipv4pool-ec2-1234567890abcdef0", "State": "provisioned" } }-
For API details, see AdvertiseByoipCidr
in AWS CLI Command Reference.
-
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-addressexample allocates an Elastic IP address. Amazon EC2 selects the address from Amazon's address pool.aws ec2 allocate-addressOutput:
{ "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-addressexample allocates an Elastic IP address and associates it with the specified network border group.aws ec2 allocate-address \ --network-border-groupus-west-2-lax-1Output:
{ "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-addressexample 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-poolipv4pool-ec2-1234567890abcdef0Output:
{ "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-addressexample allocates a specific /32 Elastic IP address from an Amazon VPC IP Address Manager (IPAM) pool.aws ec2 allocate-address \ --regionus-east-1\ --ipam-pool-idipam-pool-1234567890abcdef0\ --address192.0.2.0Output:
{ "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.
-
For API details, see AllocateAddress
in AWS CLI Command Reference.
-
The following code example shows how to use allocate-hosts.
- AWS CLI
-
Example 1: To allocate a Dedicated Host
The following
allocate-hostsexample allocates a single Dedicated Host in theeu-west-1aAvailability Zone, onto which you can launchm5.largeinstances. By default, the Dedicated Host accepts only target instance launches, and does not support host recovery.aws ec2 allocate-hosts \ --instance-typem5.large\ --availability-zoneeu-west-1a\ --quantity1Output:
{ "HostIds": [ "h-07879acf49EXAMPLE" ] }Example 2: To allocate a Dedicated Host with auto-placement and host recovery enabled
The following
allocate-hostsexample allocates a single Dedicated Host in theeu-west-1aAvailability Zone with auto-placement and host recovery enabled.aws ec2 allocate-hosts \ --instance-typem5.large\ --availability-zoneeu-west-1a\ --auto-placementon\ --host-recoveryon\ --quantity1Output:
{ "HostIds": [ "h-07879acf49EXAMPLE" ] }Example 3: To allocate a Dedicated Host with tags
The following
allocate-hostsexample allocates a single Dedicated Host and applies a tag with a key namedpurposeand a value ofproduction.aws ec2 allocate-hosts \ --instance-typem5.large\ --availability-zoneeu-west-1a\ --quantity1\ --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-cidrexample allocates a CIDR from an IPAM pool.(Linux):
aws ec2 allocate-ipam-pool-cidr \ --ipam-pool-idipam-pool-0533048da7d823723\ --netmask-length24(Windows):
aws ec2 allocate-ipam-pool-cidr^--ipam-pool-idipam-pool-0533048da7d823723^--netmask-length24Output:
{ "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.
-
For API details, see AllocateIpamPoolCidr
in AWS CLI Command Reference.
-
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-networkexample applies security groupsg-01f6e627a89f4db32to the association between the specified target network and Client VPN endpoint.aws ec2 apply-security-groups-to-client-vpn-target-network \ --security-group-idssg-01f6e627a89f4db32\ --vpc-idvpc-0e2110c2f324332e0\ --client-vpn-endpoint-idcvpn-endpoint-123456789123abcdeOutput:
{ "SecurityGroupIds": [ "sg-01f6e627a89f4db32" ] }For more information, see Target Networks in the AWS Client VPN Administrator Guide.
-
For API details, see ApplySecurityGroupsToClientVpnTargetNetwork
in AWS CLI Command Reference.
-
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-ideni-38664473--ipv6-addresses2001:db8:1234:1a00:3304:8879:34cf:40712001:db8:1234:1a00:9691:9503:25ad:1761Output:
{ "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-ideni-38664473--ipv6-address-count2Output:
{ "AssignedIpv6Addresses": [ "2001:db8:1234:1a00:3304:8879:34cf:4071", "2001:db8:1234:1a00:9691:9503:25ad:1761" ], "NetworkInterfaceId": "eni-38664473" }-
For API details, see AssignIpv6Addresses
in AWS CLI Command Reference.
-
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-ideni-e5aa89a3--private-ip-addresses10.0.0.82To 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-ideni-e5aa89a3--secondary-private-ip-address-count2-
For API details, see AssignPrivateIpAddresses
in AWS CLI Command Reference.
-
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-addressexample assigns two private IP addresses to the specified private NAT gateway.aws ec2 assign-private-nat-gateway-address \ --nat-gateway-idnat-1234567890abcdef0\ --private-ip-address-count2Output:
{ "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.
-
For API details, see AssignPrivateNatGatewayAddress
in AWS CLI Command Reference.
-
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-addressexample associates an Elastic IP address with the specified EC2 instance.aws ec2 associate-address \ --instance-idi-0b263919b6498b123\ --allocation-ideipalloc-64d5890aOutput:
{ "AssociationId": "eipassoc-2bebb745" }Example 2: To associate an Elastic IP address with a network interface
The following
associate-addressexample associates the specified Elastic IP address with the specified network interface.aws ec2 associate-address --allocation-ideipalloc-64d5890a\ --network-interface-ideni-1a2b3c4dOutput:
{ "AssociationId": "eipassoc-2bebb745" }Example 3: To associate an Elastic IP address with a private IP address
The following
associate-addressexample associates the specified Elastic IP address with the specified private IP address in the specified network interface.aws ec2 associate-address \ --allocation-ideipalloc-64d5890a\ --network-interface-ideni-1a2b3c4d\ --private-ip-address10.0.0.85Output:
{ "AssociationId": "eipassoc-2bebb745" }For more information, see Elastic IP addresses in the Amazon EC2 User Guide.
-
For API details, see AssociateAddress
in AWS CLI Command Reference.
-
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-networkexample associates a subnet with the specified Client VPN endpoint.aws ec2 associate-client-vpn-target-network \ --subnet-idsubnet-0123456789abcabca\ --client-vpn-endpoint-idcvpn-endpoint-123456789123abcdeOutput:
{ "AssociationId": "cvpn-assoc-12312312312312312", "Status": { "Code": "associating" } }For more information, see Target Networks in the AWS Client VPN Administrator Guide.
-
For API details, see AssociateClientVpnTargetNetwork
in AWS CLI Command Reference.
-
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-iddopt-d9070ebb--vpc-idvpc-a01106c2To 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-iddefault--vpc-idvpc-a01106c2-
For API details, see AssociateDhcpOptions
in AWS CLI Command Reference.
-
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-rolewith instancei-123456789abcde123.Command:
aws ec2 associate-iam-instance-profile --instance-idi-123456789abcde123--iam-instance-profileName=admin-roleOutput:
{ "IamInstanceProfileAssociation": { "InstanceId": "i-123456789abcde123", "State": "associating", "AssociationId": "iip-assoc-0e7736511a163c209", "IamInstanceProfile": { "Id": "AIPAJBLK7RKJKWDXVHIEC", "Arn": "arn:aws:iam::123456789012:instance-profile/admin-role" } } }-
For API details, see AssociateIamInstanceProfile
in AWS CLI Command Reference.
-
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-windowexample associates one or more instances with an event window.aws ec2 associate-instance-event-window \ --regionus-east-1\ --instance-event-window-idiew-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-windowexample associates instance tags with an event window. Enter aninstance-event-window-idparameter to specify the event window. To associate instance tags, specify theassociation-targetparameter, and for the parameter value, specify one or more tags.aws ec2 associate-instance-event-window \ --regionus-east-1\ --instance-event-window-idiew-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-windowexample associates a Dedicated Host with an event window. Enter aninstance-event-window-idparameter to specify the event window. To associate a Dedicated Host, specify the--association-targetparameter, and for the parameter values, specify one of more Dedicated Host IDs.aws ec2 associate-instance-event-window \ --regionus-east-1\ --instance-event-window-idiew-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.
-
For API details, see AssociateInstanceEventWindow
in AWS CLI Command Reference.
-
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
--regionyou enter must match the home Region of the IPAM you are associating it with.The following
associate-ipam-resource-discoveryexample associates a resource discovery with an IPAM.aws ec2 associate-ipam-resource-discovery \ --ipam-idipam-005f921c17ebd5107\ --ipam-resource-discovery-idipam-res-disco-03e0406de76a044ee\ --tag-specifications 'ResourceType=ipam-resource-discovery,Tags=[{Key=cost-center,Value=cc123}]' \ --regionus-east-1Output:
{ { "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.
-
For API details, see AssociateIpamResourceDiscovery
in AWS CLI Command Reference.
-
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-addressexample 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-idnat-1234567890abcdef0\ --allocation-idseipalloc-0be6ecac95EXAMPLEOutput:
{ "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.
-
For API details, see AssociateNatGatewayAddress
in AWS CLI Command Reference.
-
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-idrtb-22574640--subnet-idsubnet-9d4a7b6cOutput:
{ "AssociationId": "rtbassoc-781d0d1a" }-
For API details, see AssociateRouteTable
in AWS CLI Command Reference.
-
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-vpcexample associates the specified security group with the specified VPC.aws ec2 associate-security-group-vpc \ --group-idsg-04dbb43907d3f8a78\ --vpc-idvpc-0bf4c2739bc05a694Output:
{ "State": "associating" }For more information, see Associate security groups with multiple VPCs in the Amazon VPC User Guide.
-
For API details, see AssociateSecurityGroupVpc
in AWS CLI Command Reference.
-
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-idsubnet-5f46ec3b--ipv6-cidr-block2001:db8:1234:1a00::/64Output:
{ "SubnetId": "subnet-5f46ec3b", "Ipv6CidrBlockAssociation": { "Ipv6CidrBlock": "2001:db8:1234:1a00::/64", "AssociationId": "subnet-cidr-assoc-3aa54053", "Ipv6CidrBlockState": { "State": "associating" } } }-
For API details, see AssociateSubnetCidrBlock
in AWS CLI Command Reference.
-
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-domainexample associates the specified subnet and attachment with the specified multicast domain.aws ec2 associate-transit-gateway-multicast-domain \ --transit-gateway-multicast-domain-idtgw-mcast-domain-0c4905cef79d6e597\ --transit-gateway-attachment-idtgw-attach-028c1dd0f8f5cbe8e\ --subnet-idssubnet-000de86e3b49c932a\ --transit-gateway-multicast-domain-idtgw-mcast-domain-0c4905cef7EXAMPLEOutput:
{ "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.
-
For API details, see AssociateTransitGatewayMulticastDomain
in AWS CLI Command Reference.
-
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-idtgw-rtb-002573ed1eEXAMPLE\ --transit-gateway-attachment-idtgw-attach-0b5968d3b6EXAMPLEOutput:
{ "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.
-
For API details, see AssociateTransitGatewayRouteTable
in AWS CLI Command Reference.
-
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-blockexample 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-groupus-west-2-lax-1\ --vpc-idvpc-8EXAMPLEOutput:
{ "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-blockexample associates the IPv4 CIDR block10.2.0.0/16with the specified VPC.aws ec2 associate-vpc-cidr-block \ --vpc-idvpc-1EXAMPLE\ --cidr-block10.2.0.0/16Output:
{ "CidrBlockAssociation": { "AssociationId": "vpc-cidr-assoc-2EXAMPLE", "CidrBlock": "10.2.0.0/16", "CidrBlockState": { "State": "associating" } }, "VpcId": "vpc-1EXAMPLE" }-
For API details, see AssociateVpcCidrBlock
in AWS CLI Command Reference.
-
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-idi-1234567890abcdef0--vpc-idvpc-88888888--groupssg-12312312Output:
{ "Return": true }-
For API details, see AttachClassicLinkVpc
in AWS CLI Command Reference.
-
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-gatewayexample attaches the specified internet gateway to the specific VPC.aws ec2 attach-internet-gateway \ --internet-gateway-idigw-0d0fb496b3EXAMPLE\ --vpc-idvpc-0a60eb65b4EXAMPLEThis command produces no output.
For more information, see Internet gateways in the Amazon VPC User Guide.
-
For API details, see AttachInternetGateway
in AWS CLI Command Reference.
-
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-interfaceexample attaches the specified network interface to the specified instance.aws ec2 attach-network-interface \ --network-interface-ideni-0dc56a8d4640ad10a\ --instance-idi-1234567890abcdef0\ --device-index1Output:
{ "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-interfaceexample attaches the specified network interface to the specified instance and network card.aws ec2 attach-network-interface \ --network-interface-ideni-07483b1897541ad83\ --instance-idi-01234567890abcdef\ --network-card-index1\ --device-index1Output:
{ "AttachmentId": "eni-attach-0fbd7ee87a88cd06c" }For more information, see Elastic network interfaces in the Amazon EC2 User Guide.
-
For API details, see AttachNetworkInterface
in AWS CLI Command Reference.
-
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-providerexample attaches the specified Verified Access trust provider to the specified Verified Access instance.aws ec2 attach-verified-access-trust-provider \ --verified-access-instance-idvai-0ce000c0b7643abea\ --verified-access-trust-provider-idvatp-0bb32de759a3e19e7Output:
{ "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.
-
For API details, see AttachVerifiedAccessTrustProvider
in AWS CLI Command Reference.
-
The following code example shows how to use attach-volume.