Allow your IP in an AWS Security Group
Your public IP is detected below and dropped into every command, so you can open SSH, RDP or a database port to yourself — and only yourself — from the console, the AWS CLI or Terraform.
sg-0123456789abcdef0 with your group ID and 22 with your port.aws ec2 authorize-security-group-ingress \ --group-id sg-0123456789abcdef0 \ --protocol tcp \ --port 22 \ --cidr YOUR_IP/32
aws ec2 describe-security-group-rules \ --filters Name=group-id,Values=sg-0123456789abcdef0
aws ec2 revoke-security-group-ingress \ --group-id sg-0123456789abcdef0 \ --protocol tcp \ --port 22 \ --cidr YOUR_IP/32
\ only works in Git Bash or WSL. In PowerShell end each line with a backtick (`); in CMD use ^, or put the whole command on one line.What this rule actually does
A security group is a virtual firewall attached to an instance's network interface. Everything inbound is denied until a rule allows it, and each rule is a protocol, a port range and a source. The source 203.0.113.42/32 means "exactly this one IPv4 address": the /32 says that all 32 bits of the address must match, so nobody else on the internet gets in.
Security groups are stateful. When your SSH session is allowed in, the replies are allowed back out automatically, so you do not need an outbound rule for it — and a new group's default outbound rule already allows all traffic. That is why "allow my IP" is a single inbound rule.
Option 1: add it in the AWS console
- Open the EC2 console and choose Security Groups in the left navigation, under Network & Security.
- Select the group attached to your instance. If you are not sure which one it is, open the instance and look at its Security tab.
- On the Inbound rules tab, choose Edit inbound rules, then Add rule.
- For Type, pick the service (SSH, RDP, PostgreSQL…) or Custom TCP with your port.
- For Source, choose My IP. The console fills in the public IPv4 address of your computer.
- Add a description that says who and when — for example
fernando-laptop 2026-10-28— and choose Save rules.
The console's My IP and the address at the top of this page should match. If they don't, your browser and your SSH client may leave through different networks — a VPN with split tunnelling, a corporate proxy or two internet connections — and the rule has to use the address of the network your SSH client actually uses.
Option 2: add it with the AWS CLI
The command in the card above is the short form. If you don't know the group ID, ask for the groups attached to the instance:
aws ec2 describe-instance-attribute \ --instance-id i-0123456789abcdef0 \ --attribute groupSet
The short form cannot carry a description. To leave one — so that the next person knows whose address it is — use --ip-permissions:
aws ec2 authorize-security-group-ingress \ --group-id sg-0123456789abcdef0 \ --ip-permissions 'IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges=[{CidrIp=YOUR_IP/32,Description=fernando-laptop}]'
Two errors are worth recognising. InvalidPermission.Duplicate means the rule already exists, so there is nothing to add. RulesPerSecurityGroupLimitExceeded means the group is full: by default a security group holds 60 inbound rules for IPv4 and another 60 for IPv6. The quota can be raised, but a full group is usually a sign that old addresses were never removed.
Home and mobile connections change their public IP from time to time, and the old rule keeps pointing at an address that is no longer yours. When that happens, run the revoke command with the old address and the add command with the new one. This page always shows the address you have now.
Option 3: manage it with Terraform
For new code, the AWS provider recommends one aws_vpc_security_group_ingress_rule resource per rule, instead of the older aws_security_group_rule resource or ingress blocks inside aws_security_group. Mixing those styles on the same group can make Terraform overwrite rules or show changes on every plan.
resource "aws_vpc_security_group_ingress_rule" "ssh_from_my_ip" { security_group_id = aws_security_group.app.id cidr_ipv4 = "YOUR_IP/32" ip_protocol = "tcp" from_port = 22 to_port = 22 description = "SSH from my laptop" }
If your IP changes often, don't hard-code it: declare a my_ip variable, use cidr_ipv4 = "${var.my_ip}/32" and pass the current address with terraform apply -var="my_ip=YOUR_IP". The plan then shows a change to that one rule, which is easy to review.
Which port goes in the rule
Open only the port of the service you are reaching. Opening "All traffic" to your IP works, but it also exposes every port you forgot was listening.
| Port | Service | Notes |
|---|---|---|
| 22 | SSH / SFTP | Linux instances. Bots scan it constantly. |
| 3389 | RDP | Windows instances. Never open it to 0.0.0.0/0. |
| 5432 | PostgreSQL | For RDS, the rule goes in the security group attached to the DB instance, and the instance must be set as publicly accessible. |
| 3306 | MySQL / MariaDB / Aurora MySQL | Same as PostgreSQL: the database's own security group. |
| 27017 | MongoDB (self-hosted) | MongoDB Atlas uses its own IP access list, not an AWS security group. |
| 443 / 8080 | HTTPS / app servers | Only while the service is private or in testing; public web servers allow everyone on 80 and 443. |
Still can't connect? Work through this list
A security group that drops your traffic does it silently, so every failure looks the same: a timeout. Check these in order:
- The rule is in the wrong group. An instance can have several security groups; the rule must be in one of the groups attached to the network interface you connect to.
- Your address is not the one in the rule. Compare the rule with the IP at the top of this page. VPNs, mobile hotspots and office networks each have their own public address.
- You are connecting over IPv6. If your computer and the instance both have IPv6, the client may prefer it and an IPv4 rule won't match. Add an IPv6 rule with
/128(a single address) or connect to the IPv4 address. The IPv6 detector shows whether you have one. - The instance has no public route. It needs a public IPv4 address (or an Elastic IP) and a subnet whose route table sends
0.0.0.0/0to an internet gateway. - A network ACL blocks the way. Network ACLs are stateless and sit at the subnet level: they need an inbound rule for your port and an outbound rule for the return traffic on ephemeral ports.
- The server's own firewall drops it. UFW, firewalld or the Windows firewall on the instance still have the last word.
- Nothing listens on that port, or it listens only on localhost. Run
sudo ss -tlnpon the instance. Databases often bind to127.0.0.1until you change their configuration.
To test from your side, use nc -zv YOUR_INSTANCE_IP 22 or Test-NetConnection YOUR_INSTANCE_IP -Port 22 in PowerShell. Browsers can't test SSH at all; for HTTPS and most database ports the port checker works from the browser.
Keep it tight
- Never open SSH, RDP or database ports to
0.0.0.0/0or::/0. AWS warns that this lets every address on the internet try to connect. - Write who and when in every description, and delete rules for addresses you no longer use.
- For a team, keep the office and VPN addresses in a managed prefix list and reference the list from your security groups, instead of copying addresses into every group.
- If you don't want any inbound port at all, AWS Systems Manager Session Manager gives you a shell without inbound ports, bastion hosts or SSH keys, and EC2 Instance Connect Endpoint can reach instances that have no public IPv4 address.
The IP whitelist generator writes the same rule for MongoDB Atlas, Google Cloud, UFW, iptables and nginx, with your address already filled in.