DEV Community

Animesh Bhadra 🎯
Animesh Bhadra 🎯

Posted on • Originally published at archerimagine.com on

Doing Elastic Ips the Right Way

AWS Made Easy | AWS Elastic IP

Introduction

You are in the middle of understanding each of the steps in creating an EC2 instance. You have till now learned about Instance types, AMIs, CLIs and SDKs, and the pricing of AWS EC2 instance. One important step in this is to communicate with the AWS EC2 instance. Launching an AWS EC2 instance is of no use. There should be a medium to communicate with it. The communication may be external or internal.

Whichever type of communication you try to make, IPs play an important role. IP is like a unique address for your AWS EC2 instance, which can help you to connect with the instance. We have 3 types of IPs associated with an AWS EC2 instance.

  • Private IP
  • Public IP
  • Elastic IP

Let's jump right into learning what these different types of IPs are and what are its benefits.

Private IP

You might have already guessed by the name, this is a private IP. This is the default IP address associated with an AWS EC2 instance. This is an IP address which disassociated with an AWS EC2 instance based on these 2 parameters.

  • The VPC in which it is present.
  • The Subnet in within the VPC it is present.

AWS EC2 instance within the VPC can communicate with each other based on the private IP.

Each AWS EC2 instance, will have a private IP associate with it. This private IP is visible only within the VPC and not outside it.

If you want to connect to this AWS EC2 instance from your Laptop, it will not be possible as for this communication we need a Public IP.

Your laptop should have a Public IP for connecting to the AWS EC2 instance.

AWS Made Easy | AWS IP | Private IP

You should keep these things in mind while using private IP.

  • Private IP on an AWS EC2 instance does not change on reboot.
  • It also does not change when the AWS EC2 instance is in stopping state.
  • No two AWS EC2 instance, can have the same private IP in the same VPC.
  • You will need an AWS EC2 with private IP to launch a database system.
    • This provides inherit protection from attach outside the AWS environment.

Public IP

AWS EC2 instance cannot function in isolation. There are certain use case which needs only private IPs, like for database machine.

A web server should be open to all. If you launch a web server in an AWS EC2 instance it should be accessible from outside. You will need a public IP on this AWS EC2 instance to achieve this.

Your AWS EC2 instance communicates over open Internet using a public IP. An AWS EC2 instance, can have both private and public IP associated with it.

When you stop an AWS EC2 instance, it releases the public IP associated with it. Public IP are a scarce resource in AWS, it's use should demand justification.

You can still keep the public IP address on the AWS EC2 instance if it reboots.

All AWS EC2 instance having a public IP does not connect to the Internet. You should enable the relevant security groups.

When you launch an AWS EC2 instance, in the default VPC, it has a Public IP. This is not the case of non default VPC.

AWS Made Easy | AWS IP | Public IP

Elastic IP

You might be wondering, Private IP and Public IP both solve a specific problem.

What is the need of Elastic IP?

If you want to access your AWS EC2 instance with a fixed IP every time, How do you do it? The AWS EC2 instance releases the public IP when it is in stopped state.

How do you assign a Static Public IP to your AWS EC2 Instance.

AWS Elastic IP belongs to an account and not to an Instance. Once you create an Elastic IP, you can assign it to any AWS EC2 instance.

The Elastic IP and the AWS EC2 has a symbiotic relationship. Once the AWS EC2 instance terminates, it frees the Elastic IP.

You can remove the Elastic IP attached to current AWS EC2 instance once the purpose finishes.

Cost of AWS Elastic IP

AWS Elastic IP has a weird pricing policy. It is free to use, till it's used.

If the elastic IP is not used, then it is chargeable.

These conditions will determine if your Elastic IP is in use.

  • AWS EC2 instance has an Elastic IP associated with itself.
  • The AWS EC2 instance is in running state.
  • Only one Elastic IP attached with this AWS EC2 instance.
  • The Elastic IP associated with an attached network interface.

AWS Elastic IP limits

AWS limits only 5 Elastic IP address per region. You can alter the limit from the console.

Is AWS Elastic IP free?

AWS elastic IP is free if it is in use. It's charged when not in use.

What is an Elastic IP in AWS?

It is a Static public IP associated with an instance.

In case of an instance failure, Elastic IP added to a new instance, without any impact on incoming traffic.

Public IP Vs Elastic IP

Public IP Elastic IP
It is associated with the AWS EC2 Instance It is associated with the AWS Account, and not the instance.
Public IP cannot be manually attached from one instance to another. It can be removed from one instance and attached to another.
Public IP is released when the instance is stopped. Elastic IP is not released, when the instance is stopped.
Public IP is free by default. Elastic IP is charged if it is Idle.
An instance, launched in default VPC will have a public IP associated with it. Elastic IP is manually assigned, it is not attached by default.
IPv4 and IPv6 support. Only IPv6 support.

AWS Made Easy | AWS IP | Elastic IP Vs Public IP

Static IP Vs Elastic IP

AWS Elastic IP is a static public IP for an AWS account.

There is a benefit in using Elastic IP

  • You can associate a new EC2 instance when old EC2 instance fails.
  • This happens through code, and no manual attention required.

Assign Public IP to an AWS EC2 Instance after launch

If you have an AWS EC2 instance, in running state having a private IP. How do you attach a public IP to this running instance. This is a million dollar question for you.

The only option to attach a public IP to an already running AWS EC2 instance is AWS Elastic IP. You can create a new Elastic IP. Once created you can associate this Elastic IP with the running instance. The running instance picks the new Elastic Public IP.

AWS Made Easy | AWS IP | Attach Public IP to a running AWS Instance

Conclusion

You have got a significant insight into the 3 different IP provided by AWS.

Private IP secures the AWS EC2 instance from outside traffic. Like, in database system, you do not need any outside traffic reaching it. AWS EC2 always have a private IP.

No communication inside a VPC is possible without these private IP. It's attached to the AWS EC2 instance throughout the life cycle of the instance. It's released only when the instance terminated.

When the AWS EC2 instance need communication with outside world, Public IP is the medium. Web Server needs public communication. When AWS EC2 instance launched in default VPC, its comes with Public IP.

It's assigned before creation or restart of the instance. The public IP is only associated with the AWS EC2 instance till instance is in running or reboot state. In any other state it release the Public IP.

Elastic IP associated with an AWS account and not AWS EC2 instance. The Elastic IP switched between instances. This only supports IPv4 unlike others which supports IPv6.

Elastic IP comes in helpful, when the EC2 instance fails. You can use Elastic IP reassigned to another AWS EC2. This is not a good architecture, but you should remember this is an option though not used.

Now the idea of AWS elastic IP, public IP, private IP might be clear. If you want to launch an EC2 instance. Checkout, the free tier EC2 instance launch article. See Step by Step guide to create an EC2 instance.

Reference

Top comments (0)