Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 70 ยท Compute with EC2

Connecting to Instances and Security Groups

Learn how to reach EC2 instances with SSH, EC2 Instance Connect and Session Manager, and how stateful security groups differ from NACLs, with runnable matchers.

You launched an instance. How do you log in, and who is allowed to talk to it? Two ideas answer both questions. The first is the connection method: SSH with a key pair, EC2 Instance Connect, or Session Manager. The second is the security group, a virtual firewall that decides which network traffic can reach the instance at all. Most "I can't connect to my server" problems are a security group or routing problem, so it pays to understand them well.

This lesson follows EC2 Instances. The networking pieces it touches (subnets, route tables, internet gateways) are explained in VPC Fundamentals.

You cannot connect to a real instance from this site, so the commands below are read-only. The runnable examples are Python stand-ins: a security-group rule matcher and a comparison of stateful and stateless filtering.

Three ways to log in

1. SSH with a key pair. The classic way for Linux. At launch you choose a key pair, and AWS installs the public key for the default user (ec2-user on Amazon Linux, ubuntu on Ubuntu). You connect with the private key:

Terminal
chmod 400 my-key.pem
ssh -i my-key.pem ec2-user@203.0.113.25

This needs: a public IP address (or a VPN or other private route), a route from the internet to the subnet, a security group that allows TCP port 22 from your address, and the private key. Never open port 22 to the whole internet (0.0.0.0/0). Restrict it to your office or home address, or avoid exposing it at all.

2. EC2 Instance Connect. Instead of a long-lived key, this method pushes a temporary public key to the instance for a short time (about a minute) and then connects. You can use it from the console ("Connect" button) or the CLI. It still uses SSH and so still needs port 22 reachable, but you do not have to manage key files, and access is controlled with IAM policies:

Terminal
aws ec2-instance-connect ssh --instance-id i-0abc123

3. Session Manager (AWS Systems Manager). A shell in your browser or terminal that needs no inbound ports, no public IP and no SSH key. The SSM agent on the instance opens an outbound HTTPS connection to the Systems Manager service, and your session travels over it. Requirements:

  • The SSM agent is installed (it is preinstalled on Amazon Linux and many other AWS-provided AMIs).
  • The instance has an IAM role that allows Systems Manager, such as the managed policy AmazonSSMManagedInstanceCore.
  • The instance can reach the Systems Manager endpoints over HTTPS, through a NAT gateway, an internet gateway, or VPC endpoints (see NAT Gateways and VPC Endpoints).
  • Your IAM user or role is allowed to start sessions.
Terminal
aws ssm start-session --target i-0abc123

Session Manager is usually the best choice: access is controlled by IAM, every session can be logged to CloudTrail and CloudWatch or S3, and port 22 can stay closed. This is the "no bastion host" approach that you will use in the three-tier project. You will see more in Systems Manager and Day-Two Operations.

flowchart LR
    subgraph SSH["SSH / Instance Connect"]
        U1["You"] -- "TCP 22 inbound" --> I1["Instance<br/>public IP"]
    end
    subgraph SM["Session Manager"]
        U2["You"] -- "HTTPS, IAM" --> S["Systems Manager"]
        I2["Instance<br/>no inbound ports"] -- "outbound HTTPS" --> S
    end

SSH needs an open inbound port, whereas Session Manager needs only outbound HTTPS from the instance.

Security groups

A security group is a set of rules that acts as a virtual firewall for the network interface of an instance (and for other resources such as RDS databases, load balancers and Lambda functions in a VPC). The essential facts:

  • Allow rules only. You can say what is permitted; you cannot write deny rules. Anything not allowed is denied.
  • Stateful. If an inbound connection is allowed, the reply traffic is automatically allowed out, whatever the outbound rules say. The same is true the other way round for connections the instance starts.
  • Rules are combined. If an instance has several security groups, the rules of all of them are merged. Traffic is allowed if any rule from any group permits it.
  • Separate inbound and outbound rules. A new security group has no inbound rules (so nothing can connect) and one outbound rule that allows all traffic. The default security group of a VPC allows inbound traffic from members of the same group and all outbound traffic.
  • Changes apply at once to running instances, with no restart.
  • A rule has a protocol (TCP, UDP, ICMP or all), a port or range, and a source (inbound) or destination (outbound): a CIDR block such as 203.0.113.0/24, or another security group.

Referencing another security group is the pattern that makes layered architectures easy. The database accepts port 5432 only from instances in the web tier's group, so it works whatever IP addresses the web servers get:

Terminal
aws ec2 authorize-security-group-ingress \
  --group-id sg-db \
  --protocol tcp --port 5432 \
  --source-group sg-web

A typical pair of groups for a small web app (read-only):

Group Inbound rule Source
sg-web TCP 443 0.0.0.0/0 (the public)
sg-web TCP 22 203.0.113.0/24 (your office)
sg-db TCP 5432 sg-web

Security groups are the main tool, but they work together with route tables and network ACLs. Reachability needs all three layers to agree: the route table must send traffic somewhere, the NACL must permit it, and a security group must allow it. How to troubleshoot this is covered in NACLs, Flow Logs and Network Security.

A security-group rule matcher

This stand-in models the allow-only logic. A connection arrives with a protocol, port, source IP and the security groups the source belongs to. It is allowed if any rule in any of the destination's groups matches. It uses Python's ipaddress module for the CIDR checks, just as you would in a quick script.

Python Matching traffic against security-group rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only the traffic that some rule allows gets through, and the database accepts the web tier by group membership rather than by IP address. Try adding "sg-web" to the sg-db target list in a test to see how several groups combine: a match in any group is enough.

Security groups versus network ACLs

A network ACL (NACL) is a second firewall at the subnet boundary. The differences matter:

Security group Network ACL
Attached to Network interface (instance level) Subnet
Rules Allow only Allow and deny
State Stateful: replies are automatic Stateless: replies need their own rule
Evaluation All rules together Numbered rules in order; first match wins
Default Deny inbound, allow outbound Default NACL allows all

Because a NACL is stateless, you must also allow the return traffic. A client connects to your port 443 from a random high port (an ephemeral port, typically 1024 to 65535), and the reply goes back to that port, so an outbound NACL rule has to allow that range.

The final stand-in compares both models for one HTTPS request and its reply.

Python Stateful security group versus stateless NACL

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The security group lets the reply through without any outbound rule, because it tracks the connection. The NACL blocks the reply until a rule for the ephemeral ports exists, and in the last test the lower-numbered deny wins over the allow. In real AWS the stateful tracking is a connection-tracking table, and idle or untracked flows have a few exceptions. The stand-in shows only the basic behavior.

Troubleshooting a connection

When SSH or HTTPS does not work, check in order:

  1. Instance state is running and passes its status checks.
  2. Address. Does the instance have a public IP (or are you inside the VPC)? Is the subnet's route table pointing 0.0.0.0/0 at an internet gateway?
  3. Security group has an inbound rule for your port and source IP. Your home IP changes, so check it.
  4. NACL does not deny the traffic or the reply.
  5. On the instance: the service is listening, and an operating-system firewall is not blocking it.
  6. Credentials: the right key file, the right user name, and correct file permissions on the key.

A timeout usually means a network block (security group, NACL or route). "Connection refused" means the packet arrived but nothing listens on that port. "Permission denied (publickey)" means the network is fine and the key or user is wrong.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which statement about security groups is true?

    Security groups have only allow rules, attach to network interfaces and apply immediately. Connection tracking lets the reply flow without a matching rule.
  2. 2. What is the main advantage of Session Manager over SSH?

    The agent connects outbound over HTTPS, so port 22 can stay closed. The instance still needs an instance profile that allows Systems Manager.
  3. 3. Why does a network ACL need a rule for ephemeral ports?

    A stateless filter evaluates each packet on its own. The reply travels to the client's ephemeral port, so a rule must allow that range.
  4. 4. The database security group should accept connections only from the web servers, whose IP addresses change. What is the best source for the rule?

    Referencing a security group as the source allows any member of that group, whatever its IP, which suits instances that scale or get replaced.
  5. 5. You get "Connection timed out" when you try to SSH to an instance. What is the most likely cause?

    A timeout means packets never get an answer. A wrong key or user name shows as "Permission denied", and "Connection refused" means nothing is listening on the port.

Summary

  • You can reach an instance with SSH and a key pair, EC2 Instance Connect (temporary keys), or Session Manager (no inbound ports, IAM controlled and logged).
  • Security groups are stateful, allow-only firewalls on network interfaces. Rules from all groups on an instance combine, and a rule's source can be another security group.
  • New security groups allow no inbound traffic and all outbound traffic.
  • NACLs work at the subnet level, are stateless, have numbered allow and deny rules and need rules for return traffic on ephemeral ports.
  • Troubleshoot in layers: instance state, address and route, security group, NACL, then the service and credentials on the instance.

Next: AMIs, User Data and Instance Metadata