ec2 class notes
ec2 class notes
Security Groups
Security Groups are virtual firewalls at the instance level, allowing you to
control inbound and outbound traffic for your EC2 instances. Unlike Network
ACLs, Security Groups are stateful, meaning that if you allow inbound traffic,
the corresponding outbound traffic is automatically allowed, and vice versa.
Key Features of Security Groups:
• Stateful: Security groups automatically track the state of network
connections. Once you allow inbound traffic, the response traffic is
automatically allowed, even if there's no explicit outbound rule.
• Rule-based: Security groups operate based on allow rules only. You
cannot explicitly deny traffic. All traffic that isn’t explicitly allowed is
denied by default.
• Instance-Level Control: Security groups apply at the EC2 instance level.
Multiple security groups can be associated with an instance, and an
instance can have multiple security groups attached.
• Dynamic Updates: When you modify a security group, changes are
applied immediately to all instances associated with that group, making
it easy to manage and update security rules.
When to Use Security Groups:
• For controlling access to EC2 instances.
• When you need fine-grained control over the types of traffic allowed to
your instances.
• To manage access based on IP addresses, ports, and protocols.
Comparison:
Feature Network ACLs Security Groups
Level of
Subnet Level Instance Level
Application
Rules Can allow or deny traffic Only allows traffic (no deny rules)
6. Security Practices
IAM Roles for EC2 Instances
Assign IAM roles to instances for secure access to AWS services:
aws iam create-role --role-name MyEC2Role --assume-role-policy-document
file://trust-policy.json
aws iam attach-role-policy --role-name MyEC2Role --policy-arn
arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
Example Use Case: Accessing S3
1. Assign the IAM role to the EC2 instance.
2. Run the following command from the instance to access S3:
aws s3 ls