Skip to Content
Asterrr's Handbook
3 · Infrastructure security

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.

VPCPublic subnetPrivate subnetHTTPSfiltersDDoSsource = SG-albALB (SG-alb)Web tier(SG-web)InternetCloudFrontAWS WAFShield Advanced
Only the edge and the load balancer are reachable from the internet. The web tier accepts traffic solely from the load balancer's security group.
60 s
How long a key pushed with EC2 Instance Connect stays valid.
1 hour
Maximum tunnel duration through an EC2 Instance Connect Endpoint.

Levers

LayerActionWhy
Security groupsOne group per tier. Web tier allows inbound only from the load balancer's security group, not from a CIDRNo direct path from the internet to the instances
Network ACLsAllow only needed ports and ranges. Add deny rules for known bad sourcesSGs cannot deny. A NACL deny stops floods from known addresses. Denying all UDP stops a UDP flood against a TCP-only app
EdgePut the origin behind CloudFrontTraffic is absorbed across many edge locations, so a DDoS against the origin is far harder
Shield AdvancedRegister protected resources (Elastic IP, CloudFront, ALB, Route 53 zone, Global Accelerator)Enhanced detection and mitigation for the listed resource types
PlacementKeep instances in private subnets with no public IPNothing 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 ManagerEC2 Instance ConnectEC2 Instance Connect Endpoint
Inbound portsNone. The SSM Agent calls out22 from the client (public IP or VPN)22 or 3389 only from the endpoint
AuthIAM (ssm:StartSession), with tag-based conditionsIAM pushes a one-time public key (SendSSHPublicKey), valid 60 secondsIAM (ec2-instance-connect:OpenTunnel), then SSH key or pushed key
Needs on instanceSSM Agent + instance profile or Default Host Management ConfigurationEC2 Instance Connect package (for pushed keys)Nothing extra if you use your own key
Session loggingFull transcript to S3 or CloudWatch Logs, encrypted with KMSOnly the API call in CloudTrailTunnel open in CloudTrail
Private subnetYes, with ssm, ssmmessages, ec2messages VPC endpointsNo, needs a network pathYes, 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:StartSession on 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_keys until 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_keys on 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-ResetAccess automation.
  • 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/0 with AWS Config.
  • For forensic steps on a compromised host, see Compromised workloads.

Scenarios

Scenario
A media company wants to remove all bastion hosts. Administrators must reach Linux instances in private subnets with no internet access, no inbound security group rules may be added, and every command typed must be kept for 1 year, encrypted with a customer managed key. What should the security engineer implement?

Further reading

On this page