Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Amazon EC2 Cookbook

You're reading from  Amazon EC2 Cookbook

Product type Book
Published in Nov 2015
Publisher Packt
ISBN-13 9781785280047
Pages 194 pages
Edition 1st Edition
Languages

Table of Contents (15) Chapters

Amazon EC2 Cookbook
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Selecting and Configuring Amazon EC2 Instances Configuring and Securing a Virtual Private Cloud Managing AWS Resources Using AWS CloudFormation Securing Access to Amazon EC2 Instances Monitoring Amazon EC2 Instances Using AWS Data Services Accessing Other AWS Services Deploying AWS Applications Index

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:

  1. 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 addresses

      Syntax:

      PrivateIpAddress=string,Primary=boolean 
      
  2. Next, run the following command to create the ENI with private IP addresses 10.0.0.26 and 10.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

You have been reading a chapter from
Amazon EC2 Cookbook
Published in: Nov 2015 Publisher: Packt ISBN-13: 9781785280047
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}