AWS EC2 – How to Connect to AWS EC2 When Public IP Address Keeps Changing

amazon ec2amazon-web-services

My public IP address changes every 24 hours which is normal, but my question is: how can I make it static or what setting do I need to change so I don't have to re-allow and recreate a new security group for my new public IP address in AWS EC2?

Best Answer

A dynamic IP address is fairly common in many countries. So is carrier grade NAT, which means many people share the same IP address.

You don't need to create a new security group; you just need to add your new IP address to the existing security group and ideally need to remove all others from the security group. There are scripts you can find online that automate this for you, such as this one. You could extend it to remove old IP addresses using the AWS CLI / SDK. An EC2 instance can have five security groups, I tend to have one dedicated to home IP on various ports, one for my CDN's IPs, and then a "misc" SG for other things. This is useful as SGs have a limit of around 50 rules per group, and keeping things together makes them easier to organise.

Alternately, if your IP addresses are all in a given CIDR range or set of ranges, you could add those to your security group.

Another option is to remove the IP address restriction, which decreases security somewhat, but if you keep your private key private maybe that's good enough. That would open you up to brute force login attempts, and if any vulnerabilities are found in SSH or SSH is poorly configured that could let people into your server, so I wouldn't recommend it but it is an option.