Lesson 05 β IAM Roles for EC2
Learning Path
βοΈ Phase 2 β AWS Cloud Security
π Module 05 β Amazon EC2 Security
π― Lesson Objective
Section titled βπ― Lesson ObjectiveβBy the end of this lesson, you will be able to:
- Understand why IAM Roles are required for EC2.
- Explain how IAM Roles work.
- Understand Instance Profiles.
- Attach IAM Roles to EC2 instances.
- Verify temporary AWS credentials.
- Apply enterprise least privilege principles.
- Eliminate the use of AWS Access Keys on servers.
π Lesson Information
Estimated Time: 2 Hours
Difficulty: Intermediate
Prerequisites: Lesson 04 β Launching Secure EC2 Instances
Hands-on Lab: Yes
πΌ Business Scenario
Section titled βπΌ Business ScenarioβCloudNova Technologies has deployed its application servers into private subnets.
The developers now need the application to:
- Read files from Amazon S3
- Send logs to CloudWatch
- Retrieve secrets from AWS Secrets Manager
- Register with AWS Systems Manager
A developer suggests storing the following credentials inside the application:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEYThe Cloud Security team immediately rejects this approach.
Instead, they require every EC2 instance to authenticate using an IAM Role.
π¨ Why Access Keys Are Dangerous
Section titled βπ¨ Why Access Keys Are DangerousβImagine storing AWS Access Keys inside a server.
Application
β
AWS Access Keys
β
Amazon S3If an attacker compromises the server, they may steal those credentials and access AWS resources.
Risks include:
- Data theft
- Resource deletion
- Cryptocurrency mining
- Privilege escalation
- Financial loss
Long-term credentials should never be stored on EC2 instances.
β The Secure Solution
Section titled ββ The Secure SolutionβAWS provides IAM Roles.
Instead of storing credentials on the server, AWS automatically provides temporary credentials to the EC2 instance.
EC2 Instance
β
IAM Role
β
Temporary Credentials
β
Amazon S3No passwords or access keys are stored on the server.
π€ What is an IAM Role?
Section titled βπ€ What is an IAM Role?βAn IAM Role is an AWS identity that grants permissions without requiring permanent credentials.
Unlike an IAM User:
- A Role has no password.
- A Role has no long-term Access Keys.
- Temporary credentials are generated automatically.
Roles are assumed by trusted AWS services such as:
- Amazon EC2
- AWS Lambda
- Amazon ECS
- Amazon EKS
- AWS CloudFormation
IAM User vs IAM Role
Section titled βIAM User vs IAM Roleβ| IAM User | IAM Role |
|---|---|
| Long-term credentials | Temporary credentials |
| Passwords | No password |
| Access Keys | No permanent keys |
| Used by people | Used by AWS services and applications |
| Manual credential management | Automatic credential rotation |
For EC2, IAM Roles are the recommended approach.
π How IAM Roles Work
Section titled βπ How IAM Roles Workβ EC2 Instance β IAM Instance Profile β IAM Role β AWS Security Token Service (STS) β Temporary Security Credentials β Amazon S3 / CloudWatch / Secrets ManagerAWS Security Token Service (STS) issues temporary credentials to the EC2 instance.
These credentials are automatically rotated by AWS.
What is an Instance Profile?
Section titled βWhat is an Instance Profile?βAn Instance Profile is a container that allows an IAM Role to be attached to an EC2 instance.
Think of it as the bridge between EC2 and IAM.
EC2 Instance
β
Instance Profile
β
IAM Role
β
AWS ServicesWhen you attach an IAM Role in the EC2 console, AWS automatically uses the corresponding Instance Profile.
CloudNova Architecture
Section titled βCloudNova Architectureβ Private EC2 Instance β IAM Instance Profile β CloudNova-EC2-Role β ββββββββββββββββ¬βββββββββββββββ¬βββββββββββββββ β β β Amazon S3 CloudWatch Systems ManagerNo AWS Access Keys are stored on the server.
Principle of Least Privilege
Section titled βPrinciple of Least PrivilegeβCloudNova follows the Principle of Least Privilege.
Grant only the permissions required.
β Bad Example
AdministratorAccessβ Better Example
AmazonS3ReadOnlyAccessβ Even Better
Read only
Specific S3 Bucket
Specific PrefixAlways grant the minimum permissions needed.
π Lab 01 β Create an IAM Role
Section titled βπ Lab 01 β Create an IAM RoleβOpen:
AWS Console
β
IAM
β
Roles
β
Create RoleSelect:
Trusted Entity
β
AWS Service
β
EC2Click:
NextStep 2 β Attach Permissions
Section titled βStep 2 β Attach PermissionsβAttach the following AWS managed policies:
AmazonSSMManagedInstanceCore
CloudWatchAgentServerPolicy
AmazonS3ReadOnlyAccessThese permissions allow the instance to:
- Register with Systems Manager.
- Publish CloudWatch metrics.
- Read data from Amazon S3.
Click:
NextStep 3 β Name the Role
Section titled βStep 3 β Name the RoleβExample:
CloudNova-EC2-RoleReview the configuration and click:
Create Roleπ Lab 02 β Attach the IAM Role
Section titled βπ Lab 02 β Attach the IAM RoleβNavigate to:
EC2
β
Instances
β
CloudNova-App-Server-01Choose:
Actions
β
Security
β
Modify IAM RoleSelect:
CloudNova-EC2-RoleClick:
Update IAM Roleπ Lab 03 β Verify the IAM Role
Section titled βπ Lab 03 β Verify the IAM RoleβConnect to the EC2 instance using Session Manager or SSH.
Run:
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/Expected output:
CloudNova-EC2-RoleNow retrieve the temporary credentials:
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/CloudNova-EC2-RoleYou should see JSON similar to:
{ "AccessKeyId": "...", "SecretAccessKey": "...", "Token": "...", "Expiration": "..."}These credentials are temporary and managed automatically by AWS.
π Lab 04 β Test S3 Access
Section titled βπ Lab 04 β Test S3 AccessβRun:
aws s3 lsIf the IAM Role has the required permissions, the command should successfully list accessible S3 buckets.
No access keys are required.
π Lab 05 β Verify Your Identity
Section titled βπ Lab 05 β Verify Your IdentityβRun:
aws sts get-caller-identityExample output:
{ "UserId": "...", "Account": "123456789012", "Arn": "arn:aws:sts::123456789012:assumed-role/CloudNova-EC2-Role/..."}Notice that the identity is an assumed IAM Role, not an IAM User.
π» AWS CLI Lab
Section titled βπ» AWS CLI LabβCreate an IAM Role
Section titled βCreate an IAM Roleβaws iam create-role \ --role-name CloudNova-EC2-Role \ --assume-role-policy-document file://trust-policy.jsonAttach a Managed Policy
Section titled βAttach a Managed Policyβaws iam attach-role-policy \ --role-name CloudNova-EC2-Role \ --policy-arn arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCoreList IAM Roles
Section titled βList IAM Rolesβaws iam list-rolesDescribe an IAM Role
Section titled βDescribe an IAM Roleβaws iam get-role \ --role-name CloudNova-EC2-RoleVerify the Caller Identity
Section titled βVerify the Caller Identityβaws sts get-caller-identityβ Verification
Section titled ββ VerificationβConfirm:
β IAM Role attached to the EC2 instance.
β No AWS Access Keys stored locally.
β Temporary credentials available through the Instance Metadata Service.
β aws s3 ls works successfully.
β aws sts get-caller-identity returns an assumed-role ARN.
π Troubleshooting
Section titled βπ TroubleshootingβProblem
Section titled βProblemβaws s3 ls returns AccessDenied.
Check:
- IAM policy permissions.
- S3 bucket policy.
- Correct IAM Role attached.
Problem
Section titled βProblemβNo IAM Role visible.
Verify:
- Instance Profile attached.
- EC2 instance refreshed after attaching the role.
Problem
Section titled βProblemβMetadata request fails.
Check:
- IMDS is enabled.
- Security configuration allows access to the Instance Metadata Service.
- Youβre using the correct metadata path.
Problem
Section titled βProblemβTemporary credentials not returned.
Verify:
- IAM Role exists.
- Trust relationship allows EC2 to assume the role.
- Instance Profile is correctly associated.
π’ Enterprise Best Practices
Section titled βπ’ Enterprise Best PracticesβCloudNova standards:
- Never store AWS Access Keys on EC2 instances.
- Use IAM Roles for all AWS service access.
- Apply the Principle of Least Privilege.
- Prefer custom IAM policies over broad managed policies where practical.
- Regularly review IAM permissions.
- Monitor IAM activity using CloudTrail.
- Remove unused roles and policies.
π« Common Mistakes
Section titled βπ« Common Mistakesββ Storing AWS Access Keys in application code.
β Granting AdministratorAccess to EC2 instances.
β Using one IAM Role for every server.
β Forgetting to review IAM permissions.
β Ignoring trust relationships.
β Not validating the permissions granted to the role.
π§ͺ DIY Challenge
Section titled βπ§ͺ DIY ChallengeβCreate a new IAM Role named:
CloudNova-App-RoleRequirements:
- Trusted entity: EC2
- Permissions:
- AmazonSSMManagedInstanceCore
- CloudWatchAgentServerPolicy
- Read-only access to a specific S3 bucket (create a custom policy if available)
Attach the role to a test EC2 instance.
Verify:
aws sts get-caller-identityaws s3 ls- IAM Role appears in the EC2 console.
- No AWS Access Keys are stored on the server.
Take screenshots of:
- IAM Role
- Attached Policies
- EC2 IAM Role configuration
- AWS CLI outputs
π Knowledge Check
Section titled βπ Knowledge Checkβ- What is an IAM Role?
- Why are IAM Roles preferred over AWS Access Keys on EC2?
- What is an Instance Profile?
- Which AWS service provides temporary credentials to IAM Roles?
- What does the Principle of Least Privilege mean?
- Which command verifies the IAM identity of an EC2 instance?
- Why should applications avoid storing AWS credentials?
- What is the purpose of the trust relationship in an IAM Role?
- How are IAM Roles attached to EC2 instances?
- How do temporary credentials improve security?
π‘ Key Takeaways
Section titled βπ‘ Key TakeawaysβAfter completing this lesson, you should understand:
- IAM Roles provide secure, temporary AWS credentials to EC2 instances without storing long-term access keys.
- Instance Profiles allow IAM Roles to be associated with EC2 instances.
- AWS Security Token Service (STS) automatically issues and rotates temporary credentials.
- Applying the Principle of Least Privilege reduces the impact of compromised workloads.
- IAM Roles are a fundamental security control for production AWS environments and should be used whenever EC2 instances need to access AWS services.
π Next Lesson
Section titled βπ Next Lessonββ‘οΈ Lesson 06 β Amazon EBS Encryption & AMIs