# IAM Best Practices: How to Avoid Common AWS Security Pitfalls

AWS Identity and Access Management (IAM) is your first line of defense in the cloud—but misconfigurations are the #1 cause of security breaches. A single overly permissive policy can expose your entire infrastructure.

This guide covers **IAM best practices** to lock down your AWS environment, whether you're an intermediate user or scaling enterprise workloads.

## **1\. Enforce MFA for All IAM Users (Especially Admins)**

### **Why This Matters**

* Most of hacking-related breaches use stolen credentials
    
* MFA blocks majority of automated attacks
    

### **Implementation**

1. **Require MFA for console access** (via IAM policies)
    
2. **Enforce MFA for sensitive API actions** (e.g., terminating EC2 instances)
    
3. **Use hardware keys (YubiKey) for highest security**
    

*Policy snippet requiring MFA:*

```json
{
  "Effect": "Deny",
  "Action": "*",
  "Resource": "*",
  "Condition": {
    "BoolIfExists": {"aws:MultiFactorAuthPresent": "false"}
  }
}
```

## **2\. Implement the Principle of Least Privilege (PoLP)**

### **Common Mistakes**

* Assigning `AdministratorAccess` by default
    
* Using wildcards (`"Action": "*"`) in policies
    

### **Better Approach**

1. **Start with** `ReadOnly` access and escalate as needed
    
2. **Use AWS Managed Policies** (e.g., `AmazonS3ReadOnlyAccess`)
    
3. **Regularly audit permissions with IAM Access Analyzer**
    

*Example restrictive policy (S3 read-only + MFA required):*

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:GetObject"],
      "Resource": ["arn:aws:s3:::secure-bucket/*"],
      "Condition": {
        "Bool": {"aws:MultiFactorAuthPresent": "true"}
      }
    }
  ]
}
```

## **3\. Replace Long-Term Credentials with Temporary Roles**

### **Why Static Keys Are Dangerous**

* 63% of cloud breaches involve compromised credentials (IBM)
    
* Long-term access keys don't expire unless rotated manually
    

### **Best Practice**

**Use IAM Roles for:**

* EC2 instances (Instance Profiles)
    
* Cross-account access
    
* AWS services (e.g., Lambda)
    
* **For humans, federate access via AWS SSO**
    

*Creating an EC2 instance profile:*

```bash
aws iam create-role --role-name EC2-S3-ReadOnly --assume-role-policy-document '{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": {"Service": "ec2.amazonaws.com"},
    "Action": "sts:AssumeRole"
  }]
}'
```

## **4\. Enable IAM Access Key Rotation (Automated)**

### **Manual Rotation Is Error-Prone**

* Keys often remain active indefinitely
    
* No visibility into which services use old keys
    

### **Automated Solution**

1. **Use AWS Secrets Manager rotation** (built-in Lambda templates)
    
2. **Enforce key expiration with IAM policies**
    
3. **Monitor with CloudTrail + EventBridge alerts**
    

*Policy forcing 90-day key rotation:*

```json
{
  "Effect": "Deny",
  "Action": ["*"],
  "Resource": "*",
  "Condition": {
    "NumericGreaterThan": {
      "aws:AccessKeyAge": "90"
    }
  }
}
```

## **5\. Monitor & Respond to Threats with IAM Access Analyzer**

### **Critical Features**

🔍 **Unused permissions analysis**  
🔍 **External access detection** (e.g., overly permissive S3 buckets)  
🔍 **Policy validation** (Checks for unsafe combinations)

### **Pro Tip**

1. **Set up weekly automated reports**
    
2. **Integrate findings with Security Hub**
    

*CLI command to list unused roles:*

```bash
aws iam generate-service-last-accessed-details --arn arn:aws:iam::123456789012:role/my-role
```
