How it works
The explanation, with examples you can run.
Principals
- IAM users have long-lived credentials (a password, access keys). AWS recommends using them as little as possible: people should sign in through IAM Identity Center or another identity provider, and workloads should use roles.
- Roles have permissions but no credentials of their own. A trusted principal calls
sts:AssumeRoleand receives temporary credentials that expire, typically after an hour. EC2 instances, ECS tasks and Lambda functions all get role credentials automatically. - AWS services act as principals when, for example, SNS delivers to your SQS queue or EventBridge invokes your function.
- The root user of each account can do anything and is not limited by IAM policies in that account. Protect it with MFA and do not use it day to day.
A policy
{ "Version": "2012-10-17", "Statement": [ { "Sid": "ReadOrders", "Effect": "Allow", "Action": ["dynamodb:GetItem", "dynamodb:Query"], "Resource": "arn:aws:dynamodb:eu-west-1:111122223333:table/orders" } ]}Actions are service:Operation. Resources are ARNs, which include the partition, service, Region, account and resource path. Conditions add context checks: source VPC, tags, whether MFA was used, whether the request used TLS. "Version": "2012-10-17" is the current policy language version, not a date you should change.
Roles have two policies
A role carries a trust policy (who may assume it) and permissions policies (what it may do once assumed). An ECS task role trusts ecs-tasks.amazonaws.com; a CI role might trust your CI provider through OpenID Connect, so no AWS keys are stored in the CI system at all. Cross-account access is the same mechanism: account B's role trusts account A, and A's principals assume it.
Least privilege in practice
- Scope actions and resources.
s3:*on*is almost never what an application needs. - Use one role per workload, so a compromised service only has its own permissions.
- Use the tools: IAM Access Analyzer can generate a policy from CloudTrail activity and report unused permissions and resources shared outside your account.
- Put guard rails above accounts with AWS Organizations: service control policies (SCPs) cap what any principal in an account can do, even an administrator.
How several policies combine for one request, and why a single Deny beats any number of Allows, is the next topic.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.