Quarantining a Compromised EC2 Instance
Cut an infected workload off with restrictive security groups.
Quarantining a Compromised EC2 Instance is a free AWS Security Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Security Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
When to Quarantine
If GuardDuty or your monitoring shows an EC2 instance making malicious outbound connections, mining crypto, or scanning your network, it needs to be quarantined.
Quarantine isolates the instance from everything else so it can do no further harm, while keeping it alive for investigation rather than wiping the evidence.
Security Group Swap
The cleanest quarantine is to replace the instance's security groups with a single forensic group that has no inbound rules and, ideally, no outbound rules.
Because security groups are stateful and instance-attached, this immediately severs essentially all new connections without touching anything else in the subnet.
aws ec2 modify-instance-attribute \
--instance-id i-0abc123 \
--groups sg-quarantineWhy Not Just Stop It
Stopping or terminating the instance flushes RAM, where much malware lives, and may destroy attacker artifacts.
Quarantine keeps the instance running but isolated, so you can snapshot disk, dump memory, and observe behavior. You eradicate only after the evidence is safely captured.
Cut the IAM Role
A compromised instance frequently carries an IAM role via its instance profile, and the attacker may be exfiltrating those temporary credentials to use elsewhere.
Replace or remove the role and revoke active sessions so the stolen credentials stop working across all AWS services, not just on the instance itself.
Preserve Forensic State
Immediately snapshot every attached EBS volume to freeze the disk, and capture memory if your tooling allows.
Tag these artifacts as quarantine evidence. Mounting snapshot copies read-only on a separate forensic instance lets you investigate without altering the originals or risking re-infection.
Network ACL Backstop
For an extra layer, you can add a deny rule in the subnet's network ACL for the instance's private IP.
NACLs are stateless and apply at the subnet boundary, so this catches any path a security group change might miss. Use it as a backstop, not the primary control.
Detach from Load Balancers
If the instance sits behind an Elastic Load Balancer or in an Auto Scaling group, deregister it so traffic stops flowing to it.
Put the Auto Scaling instance on standby or detach it so the group does not terminate your evidence by replacing what it thinks is an unhealthy host.
Automate the Quarantine
At scale, manual steps are too slow. A common pattern: GuardDuty finding to EventBridge rule to Lambda, which swaps the security group, snapshots volumes, removes the role, and tags the instance.
This turns a multi-step response into a single automated action that fires within seconds of detection.
Investigate the Root Cause
Quarantine buys time to ask how the attacker got in. Review CloudTrail, VPC Flow Logs, and on-host logs to find the entry point — an exposed port, an unpatched vulnerability, or a stolen credential.
Without the root cause, a rebuilt instance can be compromised the very same way.
Rebuild, Don't Repair
After evidence is captured and the root cause is understood, replace the instance from a clean, patched AMI rather than cleaning the infected one.
You can rarely guarantee a backdoor is fully removed. Redeploying from a trusted image is faster and far more reliable than scrubbing a compromised host.
Tag and Track the Quarantine
Mark the quarantined instance and its snapshots with clear tags like Status=Quarantine and the incident ID, so no automation or operator accidentally returns it to service or deletes the evidence.
Track it through to closure in your incident record, confirming both that the threat is contained and that the forensic artifacts are preserved.
Quick Check
Pick the right credential step.
Recap
Quarantine a compromised EC2 instance by swapping in a deny-all security group while keeping it running, then snapshot EBS volumes and capture memory before changing anything. Remove the instance-profile role and revoke active sessions so stolen credentials die, detach it from load balancers and Auto Scaling, and add a NACL backstop. Automate the whole flow from GuardDuty, investigate the root cause, and rebuild from a trusted AMI.
Frequently asked questions
Is the “Quarantining a Compromised EC2 Instance” lesson free?
Yes — the full text of “Quarantining a Compromised EC2 Instance” is free to read here on the web, and the AWS Security Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Security Academy course, upgrade to CoddyKit PRO.
What will I learn in “Quarantining a Compromised EC2 Instance”?
Cut an infected workload off with restrictive security groups. You practise AWS Security Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AWS Security Academy?
No prior experience is required. AWS Security Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Quarantining a Compromised EC2 Instance” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AWS Security Academy lesson?
Yes. Every AWS Security Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Signs of Leaked Access Keys
- Revoking and Rotating Exposed Secrets
- Quarantining a Compromised EC2 Instance
- Snapshotting Volumes for Forensics