30.2 IAM Done Right
In short: IAM (Identity and Access Management) is the front door of your account.
IAM (Identity and Access Management) is the front door of your account. Get this right and most attacks fail.
1. Lock the root user. The root user (your sign-up email) can do everything, including closing the account.
- Enable MFA on root (IAM console, Security credentials, Assign MFA device).
- Delete any root access keys. Root should never have keys.
- Use root only for billing and account-level tasks; do daily work as an IAM user or through IAM Identity Center.
2. MFA for every human. A stolen password alone should never be enough.
3. Least privilege. Give only the permissions a job needs. Compare:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::reels-media-pune/*"
}]
}
This policy lets the app read and write objects in one bucket. Attaching AdministratorAccess to the app instead would let one bug in your PHP code hand an attacker your whole account.
4. Roles, not access keys. An EC2 instance should get permissions from an IAM role (you did this in Chapter 16), which gives short-lived credentials that rotate automatically. Long-lived access keys in code or ~/.aws/credentials on a server are the most common way cloud accounts get stolen.
5. Review regularly. IAM, Credential report (download CSV) shows who has MFA, old passwords and unused keys. IAM Access Analyzer flags resources shared outside your account.
aws sts get-caller-identity # who am I right now?
aws iam generate-credential-report
aws iam get-credential-report --query Content --output text | base64 -d > cred-report.csv
aws iam list-access-keys --user-name dev-sanket # any old keys?
Ravindra Bagale's Tip
The biggest mistake: writing an access key into your code and pushing it to GitHub. Bots scan public repos within minutes and use the key to start crypto-mining servers – and the bill comes to you. A key should never be in code. Use a role on EC2, aws configure on your laptop, and .gitignore. If it does get pushed, deactivate the key immediately (30.6).
Ravindra Bagale's Tip – मराठी
सर्वात मोठी चूक: access key code मध्ये लिहून GitHub वर push करणे. Bots काही मिनिटांत public repo scan करतात आणि key वापरून crypto-mining servers चालू करतात – bill तुम्हाला येते. Key कधीही code मध्ये नको. EC2 वर role, laptop वर aws configure आणि .gitignore. Push झाली तर लगेच key deactivate करा (30.6).
Ravindra Bagale's Tip – हिंदी
सबसे बड़ी गलती: access key code में लिखकर GitHub पर push करना. Bots कुछ ही मिनटों में public repo scan करते हैं और key इस्तेमाल करके crypto-mining servers चालू कर देते हैं – bill आपको आता है. Key कभी भी code में नहीं. EC2 पर role, laptop पर aws configure और .gitignore. Push हो गई तो तुरंत key deactivate करो (30.6).
Lab
In your own account: (1) enable MFA on root and on your IAM user, (2) download the credential report and find any user without MFA, (3) create a policy that allows only s3:GetObject on one test bucket, attach it to a test user, and prove with the CLI that s3:PutObject is denied (AccessDenied). Delete the test user afterwards.