Creating an instance with multiple NIC cards and a static private IP address
With multiple NICs, you can better manage your network traffic. Multiple NICs is one of the prerequisite for high availability. The number of NICs attached to the EC2 instance will depend on the type of EC2 instance. ENI's and multiple private IP addresses are only available for instances running in a VPC. In cases of instance failure, we can detach and then re-attach the ENI to a standby instance, where DNS changes are not required for achieving business continuity. We can attach multiple ENIs from different subnets to an instance, but they both should be in the same availability zone. This enables us to separate the public-facing traffic from the management traffic.
We can have one primary address and one or more secondary addresses for an NIC. We can detach and then attach NIC from one instance to another. We can attach one Elastic IP to each private address. When you launch an instance, a public IP address can be autoassigned to the network interface for eth0. This is possible only when you create a network interface for eth0 instead of using an existing network interface. You can detach secondary NIC (ethN) when an instance is running or stopped. However, you can't detach the primary (eth0) interface. In addition, you can attach security groups to NIC. If you set the instance termination policy to delete on termination, then the NIC will automatically be deleted, if you delete the EC2 instance.
How to do it…
Creating an instance with multiple NIC cards requires us to create a network interface, attach it to an instance, and finally associate the EIP to the ENI.
Creating a network interface
Use the following steps to create a network interface:
- Run the following command to create the ENI. You will need to provide the subnet ID, security group IDs, and one or more private IP addresses.
$ aws ec2 create-network-interface --subnet-id [SubnetId] --groups [SecurityGroupIds] --private-ip-addresses [PrivateIpAddressList]
The parameters used in this command are described as follows:
[SubnetId]
: This gives the ID of the subnet to associate with the network interface[SecurityGroupIds]
: This parameter provides IDs of one or more security groups[PrivateIpAddressList]
: This is used to show list of private IP addressesSyntax:
PrivateIpAddress=string,Primary=boolean
- Next, run the following command to create the ENI with private IP addresses
10.0.0.26
and10.0.0.27
:$ aws ec2 create-network-interface --subnet-id subnet-aed11acb --groups sg-ad70b8c8 --private-ip-addresses PrivateIpAddress=10.0.0.26,Primary=true PrivateIpAddress=10.0.0.27,Primary=false
In the next step, we attach the network interface to the instance.
Attaching the network interface to an instance
By running the following command, we can attach the ENI to an EC2 instance. You will need to provide the ENI ID, EC2 instance ID, and the device index.
$ aws ec2 attach-network-interface --network-interface-id [NetworkInterfaceId] --instance-id [InstanceId] --device-index [DeviceIndex]
The parameters used in this command are described as follows:
[NetworkInterfaceId]
: This parameter provides the network interface ID to attach to an EC2 instance[InstanceId]
: This one provides an EC2 instance ID[DeviceIndex]
: This parameter provides the index of the device for the network interface attachment
Then, run the following command to attach the ENI to the EC2 instance:
$ aws ec2 attach-network-interface --network-interface-id eni-5c88f739 --instance-id i-2e7dace3 --device-index 1
Associating the EIP to the ENI
By running the following command, we can associate the EIP to the ENI. You have to provide the ENI ID, EIP allocation ID, and the private address.
$ aws ec2 associate-address --network-interface-id [NetworkInterfaceId] --allocation-id [AllocationId] --private-ip-address [PrivateIpAddress]
The parameters used in this command are described as follows:
[NetworkInterfaceId]
: This parameter provides the network interface ID to attach to an EC2 instance[AllocationId]
: This gives the allocation ID of EIP, which is required for EC2-VPC[PrivateIpAddress]
: If no private IP address is specified, the Elastic IP address is associated with the primary private IP address
Next, run the following command to associate the EIP to 10.0.0.26 (the private IP address of the ENI):
$ aws ec2 associate-address --network-interface-id eni-5c88f739 --allocation-id eipalloc-d59f80b7 --private-ip-address 10.0.0.26
See also
- The Configuring security groups recipe