Attack Surface Reduction
Layered controls that shrink what an attacker can reach, from security group chaining to CloudFront and Shield Advanced.
Exam tasks: 3.2 (secure administrative access with Session Manager and EC2 Instance Connect)
The decision: how do administrators reach instances without a standing inbound door, and how do you shrink everything else an attacker can reach?
The attack surface is every path an attacker could use to reach your workload. Reduce it by exposing fewer resources, opening fewer ports, and putting a scalable front door in front of the origin.
Levers
| Layer | Action | Why |
|---|---|---|
| Security groups | One group per tier. Web tier allows inbound only from the load balancer's security group, not from a CIDR | No direct path from the internet to the instances |
| Network ACLs | Allow only needed ports and ranges. Add deny rules for known bad sources | SGs cannot deny. A NACL deny stops floods from known addresses. Denying all UDP stops a UDP flood against a TCP-only app |
| Edge | Put the origin behind CloudFront | Traffic is absorbed across many edge locations, so a DDoS against the origin is far harder |
| Shield Advanced | Register protected resources (Elastic IP, CloudFront, ALB, Route 53 zone, Global Accelerator) | Enhanced detection and mitigation for the listed resource types |
| Placement | Keep instances in private subnets with no public IP | Nothing to scan |
Shield Advanced does not protect anything automatically. You add each resource explicitly or through a Firewall Manager policy. Protecting an Elastic IP also covers the EC2 instance or Network Load Balancer attached to it.
Exam signal
"Instances must only accept traffic from the load balancer" means reference the ALB security group as the source. "Block a known attacking CIDR" means a NACL deny rule.
Exam trap
Sharing one security group between the ALB and the instances, or opening instances to 0.0.0.0/0, defeats the design. Also, Shield Standard is free and automatic, but Shield Advanced needs a subscription and explicit resource protection.
Administrative access without SSH
| Session Manager | EC2 Instance Connect | EC2 Instance Connect Endpoint | |
|---|---|---|---|
| Inbound ports | None. The SSM Agent calls out | 22 from the client (public IP or VPN) | 22 or 3389 only from the endpoint |
| Auth | IAM (ssm:StartSession), with tag-based conditions | IAM pushes a one-time public key (SendSSHPublicKey), valid 60 seconds | IAM (ec2-instance-connect:OpenTunnel), then SSH key or pushed key |
| Needs on instance | SSM Agent + instance profile or Default Host Management Configuration | EC2 Instance Connect package (for pushed keys) | Nothing extra if you use your own key |
| Session logging | Full transcript to S3 or CloudWatch Logs, encrypted with KMS | Only the API call in CloudTrail | Tunnel open in CloudTrail |
| Private subnet | Yes, with ssm, ssmmessages, ec2messages VPC endpoints | No, needs a network path | Yes, no IGW or bastion needed |
- Session Manager also does port forwarding (for example to an RDS database through an instance) and runs
sessions as a configured OS user (
Run As). - A session preferences document sets logging, KMS encryption of the session stream, idle timeout and shell
profile. Deny
ssm:StartSessionon the default document so users can't bypass it. - The EC2 Instance Connect Endpoint is an identity-aware TCP proxy: up to 20 concurrent connections, sessions up to 1 hour, and one endpoint per VPC.
Exam signal
"No inbound ports, no bastion, and every command must be logged" is Session Manager with S3 or CloudWatch Logs session logging. "Engineers must use their normal SSH client to reach private instances without a bastion or public IP" is the EC2 Instance Connect Endpoint.
Removing SSH keys and handling a leaked one
- Deleting a key pair in the EC2 console doesn't remove it from instances. The public key stays in
~/.ssh/authorized_keysuntil you remove it. - To remove a compromised key across a fleet: use Run Command (or State Manager, to keep it removed) with a
script that deletes that key from
authorized_keyson every instance, then rotate any other credentials the key holder could reach. - Lost the key for a single instance: connect with Session Manager and add a new public key, or use the
AWSSupport-ResetAccessautomation. - Then remove SSH altogether: close port 22 in security groups, move admins to Session Manager, and alert on
security groups that open 22 or 3389 to
0.0.0.0/0with AWS Config. - For forensic steps on a compromised host, see Compromised workloads.
Scenarios
Session Manager needs no inbound rules because the agent connects out through the VPC endpoints, and it records full session transcripts, encrypted with the KMS key you choose. An Instance Connect Endpoint requires an inbound rule for SSH from the endpoint, and CloudTrail records only that a tunnel opened, not the commands. Client VPN needs inbound SSH and doesn't log commands. Plain EC2 Instance Connect needs a network path to port 22.
Related
Further reading
Instance Profiles and Role Switching
How EC2 gets role credentials through an instance profile, how to change permissions, and how the CLI switches roles by profile.
IDS, IPS and Host-Based Security
Where network controls stop and host agents, Network Firewall and IMDSv2 take over. Source/destination check, file integrity, metadata hardening.