Security and compliance · 12 min
IAM policies, least privilege and credentials
In this lesson you will learn how to read an IAM policy, how AWS decides whether to allow a request, what least privilege is, how long-term credentials differ from temporary ones and which practices AWS recommends for IAM.
An IAM policy is the document that says what an identity can do in AWS, that is, a user, a group or a role. Policies are needed because in AWS everything starts out denied. A newly created IAM user, group or role can't do anything, and the only exception is the root user, which has complete access. For any other identity to read a bucket, which is where files are kept in Amazon S3, or start an instance, someone has to attach a policy that allows it.
For example, in an online store, the employee who uploads the product photos gets a policy that lets him save files in the photos bucket. Without that policy, the upload button gives him an access denied error.
How is a policy built?
A policy is a JSON document with one or more statements, and each statement says what is allowed or denied and on which resources. A resource is anything you create in AWS, like a bucket, an instance or a database. For the exam you don't need to write JSON, because the questions ask you to recognize a policy and know what each part does.

Each statement has four parts:
- Effect, which says whether the statement allows (Allow) or denies (Deny).
- Action, which says which actions it covers, for example reading S3 objects.
- Resource, which says which resources it applies to.
- Condition, which is optional and sets more limits, such as from which IP address, only with MFA or at what times.
For example, a statement at the store could say: Effect Allow, Action read objects, Resource the photos bucket, Condition only from the office network. Read in words, the statement allows reading the catalog photos, but only for whoever connects from the office.
Match each part with what it says
0 of 4 pairsTap a part of the policy and then what it says.
0 of 4 pairs.
What types of policies are there?
Policies are divided into three types based on who maintains them.

Each type is created and changed in a different way:
- AWS managed policies, which AWS creates and maintains, such as
IAMReadOnlyAccess, and which you can't edit. - Customer managed policies, which you create and change whenever you want.
- Inline policies, which are attached to a single identity and are deleted along with it.
For example, the store can give its accountant an AWS managed policy that only allows viewing, create its own customer managed policy for the photos team, and attach an inline policy to a temporary employee, which is deleted on its own when that user is deleted.
True or false?
0 of 3You can edit the IAMReadOnlyAccess policy to remove an action from it.
If you delete a user, its inline policy is deleted along with it.
You can change a customer managed policy whenever you want.
How does AWS decide whether to allow a request?
A request is anything an identity asks AWS to do, like opening a file or deleting a bucket. An identity can have several policies at once, its own and those of its groups. When that identity requests something, AWS reviews all of its policies together and in a fixed order, and no policy takes priority for being attached to the user or to the group.

AWS looks for a Deny first, and only if it finds none does it look for an Allow, so an explicit Deny wins even if another policy allows the same thing. If no policy says anything about that action, the request is denied by default. For example, if the store's Photos group allows deleting photos, but an intern's user has a policy that denies deleting, the intern can't delete photos. And if nobody wrote anything about seeing the payments, he can't see them either, because whatever isn't allowed stays denied.
Decide whether AWS allows the request
A developer tries to delete an S3 bucket. Their user has the AmazonS3FullAccess policy, which says Allow; their group may have another one that says Deny for s3:DeleteBucket. Turn each one on and off and follow the path of the request.
Allowed.
No policy denies the action and AmazonS3FullAccess allows it, so the developer deletes the bucket.
What is least privilege?
The IAM guide defines least privilege as granting only the permissions needed for a task: which actions, on which resources and under which conditions. That way, if someone steals an identity's credentials or that identity makes a mistake, the damage stays limited to the little it could do. AWS accepts that you start with AWS managed policies, which are broad, while you figure out what you need, and then asks you to trim those permissions down.
To trim them, IAM has two tools. IAM Access Analyzer can generate a policy based on what an identity actually used, and last accessed information tells you when the identity last used each service. For example, if a role has permission for 20 services and used only 3, you remove the other 17.
Long-term and temporary credentials
Credentials are the details an identity uses to prove who it is. The credentials of an IAM user, which are its password and its access keys, last until someone deletes them, so if they leak they work until that day. Temporary credentials, on the other hand, are generated by AWS Security Token Service (AWS STS) when someone assumes a role, and they last from minutes to hours before expiring on their own. For example, if a store employee accidentally publishes a long-term password, whoever finds it can use it until it is changed, while leaked temporary credentials stop working within a few hours.
An access key has two parts, an ID and a secret key, and it is used to work with AWS from the CLI, an SDK or the API. The secret part can only be seen when the key is created, and each user can have two keys at most. The IAM guide asks you not to create keys for the root user and not to put them in your code. For example, if a store programmer pastes his secret key into the code and uploads that code to a public site, anyone who reads it can use AWS in the store's name.

To give an application access, AWS recommends a role: the instance requests the role's credentials from AWS STS, receives them and uses them to read the bucket. That way there are no keys stored in the instance or in the code, and an application built with an AWS SDK finds those credentials on its own. For example, the store's website, which runs on an EC2 instance, reads the photos from the bucket with the instance's role, and nobody has to write a key in any file.
How do you give it access?
An application running on an EC2 instance needs to read objects from an S3 bucket. How do you give it access?
IAM best practices
AWS publishes a list of security best practices for IAM, and those practices include:
- Federation with temporary credentials, which is how people should sign in, using an account they already have in another system, like their company email account, and which, for multiple accounts, AWS recommends doing with AWS IAM Identity Center.
- Roles, which workloads such as EC2 and Lambda use instead of keys.
- MFA, which is turned on for the root user and for IAM users.
- Long-term keys, which, if you need them, are rotated when needed.
- Protecting the root user, which is used only for the tasks that require it.
- Least privilege, which is trimmed down step by step with IAM Access Analyzer.
- Reviewing what isn't used, which means deleting the users, roles, permissions and credentials that nobody uses.
For example, when an employee leaves the store, reviewing what isn't used means deleting his user and his keys, so nobody can get in with them later.
Besides these practices, every account has a password policy, which sets rules for IAM users' passwords, such as the minimum length, the character types, expiration and reuse. This policy doesn't apply to the root user or to access keys. For example, the store can require every employee password to include numbers and letters, and that rule changes nothing for the root user's password.
Accounts with the new experience
In an account with the new experience you don't attach policies to your team, because each person you invite is an administrator of the whole project and that access can't be limited by service. IAM Access Analyzer isn't available in that account either, except for policy validation, which checks whether a policy is written correctly.
Roles are also created in a different way, because the IAM role manager creates them for you, for example when you create a Lambda function in the console, and it can't be turned off until you turn on the advanced features. The policies you do write, for roles and programmatic users, work the same as in a full account, with Effect, Action, Resource and Condition, and with an explicit Deny winning over any Allow.
On the exam
Questions on this topic describe access that has to be granted or a request that has to be evaluated. For example, if the question says a user has one policy that allows deleting and another that denies it, the answer is that they can't delete. These words in the question point to the answer:
| Words in the question | Answer |
|---|---|
| Only the necessary permissions, the least access possible | Least privilege |
| An application on EC2 needs access to another service | An IAM role, never keys in the code or root user credentials |
| Two policies, one allows and the other denies | Denied |
| Credentials that expire on their own | AWS STS, when assuming a role |
| A single sign-in for multiple accounts | AWS IAM Identity Center |
Review question
A developer has the AmazonS3FullAccess managed policy attached. Their group has another policy with "Effect": "Deny" for the s3:DeleteBucket action. What happens when they try to delete a bucket?
Next
- Security best practices in IAM, the full list in the AWS documentation.
- Take one of the Cloud Practitioner practice exams.