AWS-Terraform Public, Private IPs, and Elastic IPs
4 min readJul 7, 2024
Public and Private IPs
IP Versions:
- IPv4: Common format, four numbers separated by dots, provides 3.7 billion unique addresses.
- IPv6: Less common, longer alphanumeric string, supports more addresses, used for IoT.
Public IP:
- Used for identifying machines on the internet, must be unique globally.
- Allows communication over the internet.
- Associated with instances like EC2 when they need direct internet access.
Private IP:
- Used within private networks (e.g. within a company).
- Allows communication only within the network, not accessible from the internet.
- Can have identical IPs across different private networks.
NAT Devices and Gateways:
- Machines in a private network connect to the internet through NAT devices and internet gateways acting as proxies.
Elastic IPs:
- Public IPv4 addresses that you can attach to instances.
- Provide a fixed public IP to mask instances or software failures.
- Limited to five per AWS account by default, considered poor architectural practice due to potential scalability issues.
Best Practices:
- Avoid using Elastic IPs where possible; prefer DNS names or load balancers for scalable and manageable solutions.
- DNS (e.g., Route 53) provides scalable and controlled domain name resolution.
Behavior of EC2 Instances:
- By default, EC2 instances have a private IP for internal AWS network and a public IP for internet access.
- Public IPs may change if instances are stopped and started.
- SSH into EC2 instances typically uses the public IP unless connected via VPN.
Public and Private IP Usage:
- Public IPv4 is used to SSH into the instance from the internet.
- Once logged in, the private IP can be used for internal communication.
- SSH via private IP does not work unless connected to the same private network (e.g. via VPN).
Behavior on Start/Stop:
- Stopping and starting an instance changes its public IPv4 address.
- The private IP remains the same across starts and stops.
- Rebooting does not change the public IP, but stopping and starting does.
Elastic IPs:
- Elastic IPs provide a fixed public IPv4 address that persists across instance stop/start cycles.
- An Elastic IP can be allocated from Amazon’s pool and associated with an instance.
- There is a small hourly charge for Elastic IPs if they are not in use.
Cost Considerations:
- Using Elastic IPs incurs a cost, of around $0.005 per hour or $3.50 per month.
- AWS offers 750 hours per month of free public IPv4 addresses, so managing instances and Elastic IPs efficiently helps avoid unnecessary costs.
Elastic IP Association:
- An Elastic IP can be associated with an instance, ensuring it retains the same public IPv4 address even when stopped and started.
- Disassociating and releasing the Elastic IP prevents ongoing charges.
Hands-On Example:
- Demonstrated stopping, starting, and associating Elastic IPs with an instance.
- Showed that Elastic IP remains consistent even after instance stop/start.
- Go to Elastic IP address >> Allocate Elastic IP address
Once the Elastic IP address is created attach it to the Instance >> Associate Elastic IP address
Even the instance is stopped and started again the IP will not change
- Highlighted the importance of releasing unused Elastic IPs to avoid charges.
How to Attach Elastic IP to EC2 Instance using Terraform?
#Creating EC2
resource "aws_instance" "server" {
ami = data.aws_ami.aws_ami.id
instance_type = "t2.micro"
tags = {
Name = "fluffy-server"
}
}
# Data block
data "aws_ami" "aws_ami" {
most_recent = true
owners = ["amazon"] # Specify the owner (e.g., "amazon" for official AMIs)
filter {
name = "name"
values = ["amzn2-ami-hvm-*"] # Replace with the desired AMI name
}
}
#Creating elastic IP
resource "aws_eip" "lb" {
vpc = true
tags = {
Name = "fluffy-eip"
}
}
#Attaching Elatic IP to Instance
resource "aws_eip_association" "eip_assoc" {
instance_id = aws_instance.server.id
allocation_id = aws_eip.lb.id
}
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!