Security Groups and Key Pairs
Control inbound and outbound traffic with security groups and manage SSH authentication with EC2 key pairs.
Security Groups and Key Pairs is a free AWS Solutions Architect 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 Solutions Architect learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Are Security Groups?
A security group is a virtual stateful firewall that controls inbound and outbound traffic to and from an EC2 instance (or other AWS resources like RDS, Lambda, ELB). Security groups operate at the instance level—each rule specifies a protocol (TCP/UDP/ICMP), port range, and source/destination (IP range or another security group). Unlike traditional firewalls, security groups are stateful: if you allow inbound traffic, the response traffic is automatically allowed outbound without an explicit rule.
Inbound and Outbound Rules
Security groups have separate inbound and outbound rule sets. Inbound rules control traffic arriving at the instance—by default, all inbound traffic is denied. Outbound rules control traffic leaving the instance—by default, all outbound traffic is allowed. You add explicit Allow rules; there are no explicit Deny rules in security groups (use Network ACLs for explicit denies). All rules in a security group are evaluated together using a logical OR—any matching Allow rule permits the traffic.
# Add an inbound SSH rule to a security group
aws ec2 authorize-security-group-ingress \
--group-id sg-12345678 \
--protocol tcp \
--port 22 \
--cidr 203.0.113.0/24
# Add an inbound HTTP rule
aws ec2 authorize-security-group-ingress \
--group-id sg-12345678 \
--protocol tcp \
--port 80 \
--cidr 0.0.0.0/0Security Group Chaining
Security groups support source/destination security group references, which is more flexible and secure than specifying IP ranges. For example, an ALB security group allows 0.0.0.0/0 on port 443. The EC2 instances' security group allows inbound port 8080 only from the ALB security group ID—not from any IP. If an ALB is replaced or scaled, the rule still applies correctly without IP changes. This pattern creates a layered, dynamic defence for multi-tier architectures.
# Allow inbound from another security group (not an IP)
aws ec2 authorize-security-group-ingress \
--group-id sg-ec2-instances \
--protocol tcp \
--port 8080 \
--source-group sg-load-balancerSecurity Groups Are Stateful
Stateful means the security group tracks connection state. If your inbound rule allows TCP port 80 from the internet and a client makes a request, the response packets (outbound) are automatically allowed back to the client—even if there is no outbound rule for port 80. This is fundamentally different from Network ACLs, which are stateless and require explicit rules for both directions of traffic. Statefulness makes security groups easier to configure for typical application traffic patterns.
Multiple Security Groups on One Instance
You can attach multiple security groups to a single EC2 instance (up to 5 by default, adjustable). The rules from all attached security groups are combined using a union (OR logic)—if any security group allows a request, the request is permitted. This means security groups can only add permissions, never restrict them—if you want to tighten access, you must remove or modify rules, not add a more restrictive security group. Keep this additive behaviour in mind for exam questions about access restriction.
Default Security Group Behaviour
Every VPC includes a default security group. Its default rules: inbound allows all traffic from other instances also in the default security group; outbound allows all traffic to anywhere. This is deliberately permissive so new instances can communicate by default. For production environments, create a custom security group with a locked-down inbound rule set and remove instance associations with the default security group to avoid accidental exposure.
Key Pairs: How They Work
EC2 key pairs use asymmetric cryptography for SSH authentication. AWS generates the key pair (or you import your own public key): AWS stores the public key and injects it into the instance at launch; you download and store the private key (.pem file)—AWS never stores it. SSH uses the private key to prove your identity to the server without transmitting a password. The private key must have restrictive permissions (chmod 400) or SSH will refuse to use it.
# Create a key pair and save the private key
aws ec2 create-key-pair \
--key-name ProdKey \
--key-type rsa \
--key-format pem \
--query 'KeyMaterial' \
--output text > ProdKey.pem
chmod 400 ProdKey.pemRecovering Access Without a Key Pair
If you lose the private key for a Linux EC2 instance, you cannot SSH in conventionally. Recovery options include: Systems Manager Session Manager (if the SSM agent is running and an IAM role is attached—no key needed), EC2 Instance Connect (injects a temporary key via browser or CLI—requires an open SSH port and EIC permissions), or stop the instance, detach the root EBS volume, attach it to another instance, modify the authorized_keys file, re-attach and restart. For Windows, use Systems Manager to retrieve the password.
# Connect using EC2 Instance Connect (temporary key injection)
aws ec2-instance-connect send-ssh-public-key \
--instance-id i-0abcdef1234567890 \
--instance-os-user ec2-user \
--ssh-public-key file://~/.ssh/id_rsa.pub
ssh -i ~/.ssh/id_rsa ec2-user@54.123.45.67Best Practice: Limit SSH Access
Leaving SSH (port 22) open to 0.0.0.0/0 exposes your instance to brute-force and credential-stuffing attacks from anywhere on the internet. Best practices: restrict SSH to your specific corporate IP range or use a bastion host (jump box) in a public subnet with a tightly controlled security group, then SSH from the bastion to private instances. Better still, use Systems Manager Session Manager to eliminate SSH port exposure entirely—no inbound rules needed at all.
# Example: restrict SSH to a corporate IP range
aws ec2 authorize-security-group-ingress \
--group-id sg-12345678 \
--protocol tcp \
--port 22 \
--cidr 198.51.100.0/24 # Your corporate CIDRSecurity Group Limits and Quotas
Key default limits for security groups (adjustable via Service Quotas): up to 2,500 security groups per VPC, 60 inbound and 60 outbound rules per security group, and 5 security groups per network interface. When referencing another security group as a source, each referenced security group–rule combination counts as a rule. If you hit rule limits, consolidate by referencing security group IDs instead of individual IP ranges, or use prefix lists to group multiple CIDRs into one manageable entity.
Managed Prefix Lists
A managed prefix list is a set of CIDR blocks that you can reference in security group rules or route tables. AWS maintains AWS-managed prefix lists for services like S3 and CloudFront, so you can allow traffic to/from these services without maintaining IP ranges yourself (which change over time). You can also create customer-managed prefix lists to group your corporate IP ranges—update the prefix list in one place and all security groups referencing it inherit the change automatically.
# Allow outbound HTTPS to Amazon S3 using the AWS-managed prefix list
aws ec2 authorize-security-group-egress \
--group-id sg-12345678 \
--ip-permissions '[{"IpProtocol":"tcp","FromPort":443,"ToPort":443,"PrefixListIds":[{"PrefixListId":"pl-63a5400a"}]}]'Quick Check
Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.
Lesson Recap
In this lesson you learned: security groups are stateful virtual firewalls that allow traffic by default deny, chaining security groups by ID is more secure and flexible than using IP ranges, and key pairs provide asymmetric SSH authentication while Systems Manager Session Manager removes the need for open SSH ports. Next up we cover EC2 storage options: Instance Store vs EBS.
Frequently asked questions
Is the “Security Groups and Key Pairs” lesson free?
Yes — the full text of “Security Groups and Key Pairs” is free to read here on the web, and the AWS Solutions Architect 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 Solutions Architect course, upgrade to CoddyKit PRO.
What will I learn in “Security Groups and Key Pairs”?
Control inbound and outbound traffic with security groups and manage SSH authentication with EC2 key pairs. You practise AWS Solutions Architect 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 Solutions Architect?
No prior experience is required. AWS Solutions Architect 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 “Security Groups and Key Pairs” 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 Solutions Architect lesson?
Yes. Every AWS Solutions Architect 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
- Launching Your First EC2 Instance
- Instance Types and Pricing Models
- Security Groups and Key Pairs
- EC2 Storage: Instance Store vs EBS