A building with a guard at every door
Picture a big office building. Every room has a door, and at every door stands a guard.
When Mia wants to open the files room, the guard asks two questions, one after the other. First: who are you? Mia shows her staff badge, and the guard checks it's real. Second: are you allowed in here? The guard looks at the rules written down for Mia, and only then opens the door.
The building has an owner too, with a master key that opens everything. Because that key is so powerful, the owner locks it in a safe and uses it only for the rare jobs nobody else may do, like selling the building.
People don't only enter as themselves. There's also a rack of job vests by the entrance, such as "Cleaner" or "Delivery". Put one on and you get the vest's rights for a while. Vests come later.
Principals, authentication and authorization
IAM (Identity and Access Management) controls access to every AWS API call in an account. Each request is made by a principal:
| Principal | What it is |
|---|---|
| Root user | The identity that created the account, signed in with its email address. It has full access to everything in the account, and you can't attach IAM policies to it to limit it |
| IAM user | A long-lived identity for a person or program, with a console password and/or access keys |
| IAM role | An identity with no credentials of its own. Trusted principals assume it and get temporary credentials |
Every request goes through two steps. Authentication: AWS checks the request's signature (Signature Version 4, computed from the secret access key) or the console sign-in, and works out which principal sent it. Authorization: AWS evaluates the policies that apply and decides Allow or Deny.
Lock the root user away: turn on MFA, create no access keys for it, and use it only for the few tasks that need it, such as closing the account or changing some billing settings. Day-to-day work goes through IAM users, roles, or IAM Identity Center for people.
Permission cards
The rules the guard reads are written on permission cards, called policies. Each line on a card says four things: yes or no, what you may do (open, read, write), which room, and sometimes only if, such as "only on weekdays" or "only from inside the building".
Guards follow three rules, in this order of strength:
- If no card says yes, the answer is no. Nobody gets in by accident.
- A line saying yes lets you in.
- A line saying no beats every yes. If one card says "never the safe room", no other card can change that.
Policies and how they're evaluated
A policy is a JSON document of statements. Each statement has an Effect (Allow or Deny), one or more
Actions (s3:GetObject, wildcards like s3:Get* allowed), the Resource ARNs it covers, and an optional
Condition on request context keys:
1{2"Version": "2012-10-17",3"Statement": [4{5"Effect": "Allow",6"Action": ["s3:GetObject", "s3:PutObject"],7"Resource": "arn:aws:s3:::invoices/*",8"Condition": { "Bool": { "aws:SecureTransport": "true" } }9},10{11"Effect": "Deny",12"Action": "s3:DeleteObject",13"Resource": "*"14}15]16}
"Version": "2012-10-17" is the policy language version, not a date to update. The evaluation rules:
- Default (implicit) deny: a request with no matching
Allowis denied. - An explicit allow in an applicable policy overrides the implicit deny.
- An explicit deny in any applicable policy overrides every allow.
So "deny wins, otherwise you need an allow". Policies are attached to principals as AWS managed, customer managed (reusable, versioned) or inline policies.
Other policy types can only narrow access, never grant it: a permissions boundary caps what a user's or
role's identity policies can allow; service control policies (SCPs) in AWS Organizations cap every principal
in a member account, including its root user; session policies passed to AssumeRole cap one session. An
explicit deny in any of them still wins, and an action must be allowed by each one that applies.
Debugging: an AccessDenied error often names the policy type that denied the request. The IAM policy
simulator and CloudTrail show which principal made the call and what was evaluated.
Cards on people, cards on doors
A permission card can hang in two places. Most are clipped to a person's badge: "Mia may read the files room". Some are taped to a room's door instead: "Mia may come in", or even "Leo from the building next door may read the files room".
Inside one building, one yes on either card is enough. But when Leo walks over from next door, two yeses are needed: his own building must let him go, and the door here must name him. That way neither building can open the other's rooms alone.
Identity-based and resource-based policies
- Identity-based policies attach to a user, group or role and say what that principal may do. They have no
Principalelement, because the principal is whoever they're attached to. - Resource-based policies attach to a resource, such as an S3 bucket policy, an SQS queue policy, a KMS key
policy or a role's trust policy. They have a
Principalelement naming who may act on that resource, which can be a principal in another account.
Within one account, an allow in either is usually enough (the exceptions include role trust policies and KMS key policies, which must allow the principal themselves). Cross-account, the request needs an allow in both: the caller's account must allow it in an identity-based policy, and the resource's policy must allow the caller. An explicit deny in either still wins.
Job vests and day passes
Handing out permanent badges for every job gets messy. So the building keeps job vests, called roles, on a rack at the vest desk. Each vest has its own permission card, plus a tag saying who may borrow it: "anyone who works in our building" or "the cleaning robot".
When Mia borrows the "Auditor" vest, the vest desk checks the tag, then gives her a day pass that works for one hour. While she wears the vest, the guards read the vest's card, not hers. When the hour is up, the pass stops working all by itself, and there's nothing to take back.
Machines get vests too. The cleaning robot gets a fresh day pass from the desk before the old one runs out, without anyone carrying keys to it. A temp worker called in for one job turns up already wearing their vest.
Roles and temporary credentials from STS
A role has a permissions policy (what it may do) and a trust policy, a resource-based policy saying which
principals may assume it ("Action": "sts:AssumeRole" with a Principal such as another account, a user or a
service like ec2.amazonaws.com).
To use a role, a caller asks AWS STS (Security Token Service) to AssumeRole. STS checks that the trust policy
allows the caller (and, for cross-account calls or a trust policy that trusts a whole account, that the caller's
own policies allow sts:AssumeRole), then returns temporary credentials: an access key
ID (starting ASIA), a secret key, a session token and an expiry. DurationSeconds defaults to one hour,
can be as short as 15 minutes, and as long as the role's maximum session duration (1 to 12 hours). Role
chaining (using a role's session to assume another role) is capped at one hour.
Services assume roles for you:
- EC2: an instance profile attaches a role to an instance. The SDK reads credentials from the instance metadata service (use IMDSv2), and they are rotated before they expire.
- Lambda: the function's execution role is assumed by Lambda, which passes the credentials in the
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEYandAWS_SESSION_TOKENenvironment variables (with SnapStart, through the container credentials endpoint instead, which the default chain also finds). - ECS tasks get a task role, and EKS pods get one through EKS Pod Identity or IAM roles for service accounts (IRSA).
Only the rooms you need, and no keys that last forever
Two habits keep the building safe. First, every card lists only the rooms that person or vest really needs. The cleaner's vest opens the cleaning cupboard, not the safe room. If something goes wrong, less goes wrong. That habit is called least privilege.
Second, avoid spare keys that never expire. Some staff are given a copied key so a program can open doors without a person there. If that key falls out of a pocket, or is left in a shared folder, whoever finds it can walk in as that person, any day, until someone notices and changes the lock. A day pass found in the street stops working within a few hours at most.
Least privilege and long-lived access keys
Least privilege: grant only the actions and resources a principal needs, scoped by ARN and conditions. Start from AWS managed policies if you must, then tighten them using IAM Access Analyzer's unused-access findings and policy generation from CloudTrail activity.
An IAM user's access keys (AKIA... key ID plus a secret, at most two per user) are long-term: they never
expire on their own. That's the risk: keys committed to Git, baked into images or left in a laptop's
~/.aws/credentials work for anyone, from anywhere, until someone deactivates them. Prefer:
- roles for anything running on AWS (instance profiles, execution roles, task roles);
- IAM Identity Center or federation for people, giving them short-lived sessions;
- IAM Roles Anywhere or OIDC federation (for example GitHub Actions assuming a role) for workloads outside AWS.
If keys are unavoidable, rotate them, require MFA where you can, and watch for unused keys. A leaked key is contained by deactivating it, and a leaked temporary credential by its short expiry (or by revoking the role's active sessions).
Your Java app finds its pass
Your Java app needs a pass before it can open any door, and it shouldn't have one written into its code. So it checks a list of places, in order, and uses the first pass it finds: a note handed to it at start-up, its coat pockets, a pass file at home, and finally the vest desk window of the machine it's running on.
On a laptop it usually finds your own pass in the file at home. In the cloud, the same app, with no changes, walks to the vest desk and gets a day pass for its vest.
The default credentials provider chain in Java
In the AWS SDK for Java 2.x, a client built without explicit credentials uses DefaultCredentialsProvider,
which tries these providers in order and uses the first that returns credentials:
- Java system properties
aws.accessKeyId,aws.secretAccessKey(andaws.sessionToken); - environment variables
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN(this is how Lambda's execution role arrives); - a web identity token file (EKS IAM roles for service accounts; needs the
stsmodule); - the shared
~/.aws/credentialsand~/.aws/configprofile (a profile can also point at SSO or a role to assume, with thessoandssooidc, orsts, modules on the classpath); - container credentials (ECS task roles, EKS Pod Identity);
- instance profile credentials from the EC2 instance metadata service.
1// No credentials in code: the default chain finds them wherever the app runs.2S3Client s3 = S3Client.builder()3.region(Region.EU_WEST_1)4.build();56s3.getObject(GetObjectRequest.builder()7.bucket("invoices")8.key("2025/inv-42.pdf")9.build(), Path.of("inv-42.pdf"));
The provider caches temporary credentials and refreshes them before they expire, so long-running apps keep
working. To assume a role explicitly, wrap an StsClient in StsAssumeRoleCredentialsProvider. In Spring Boot,
expose the client as a bean and let the same chain resolve credentials per environment: a profile locally, the
role in the cloud. Never put access keys in application.properties.