Lesson 2 of 6 AWS IAM

Writing Least-Privilege Identity-Based Policies

This lesson explains how identity-based policies, attached to users, groups and roles, state which actions they may perform on which resources, and how to write them with least privilege.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish AWS managed, customer managed and inline policies.
  • Describe the elements of a policy statement, including conditions.
  • Apply least privilege and attribute-based access control.

Policy forms

Identity-based policies are attached to users, groups and roles, and come in three forms. AWS managed policies are written and updated by AWS, such as ReadOnlyAccess or policies for common job functions. They are convenient for getting started, but they are broad and change when AWS updates them, so they rarely give least privilege.

Customer managed policies are written by you, can be attached to many identities, and keep up to five versions, so a change can be rolled back. They are the usual way to grant least-privilege permissions that you control. Inline policies are embedded in a single user, group or role and are deleted with it. They suit a strict one-to-one relationship, where a permission must never be attached to any other identity by mistake. The following table summarises the three forms.

FormCharacteristicsUse for
AWS managedBroad, maintained by AWS, such as ReadOnlyAccessGetting started and common job functions
Customer managedReusable across identities and versionedLeast-privilege permissions you control
InlineBelongs to a single identity and is deleted with itStrict one-to-one relationships

Policy structure and conditions

A policy statement has an Effect of Allow or Deny, the Action it covers, the Resource ARNs it applies to, and an optional Condition. A condition compares a key from the request context with a value, and the statement applies only when the comparison is true.

Conditions are how policies express rules beyond "who can do what". aws:SourceIp limits requests to an IP range, such as the corporate network. aws:MultiFactorAuthPresent requires that the caller signed in with MFA, which suits destructive actions. aws:RequestedRegion limits which Regions can be used, and aws:SecureTransport requires TLS, typically in a bucket policy that denies plain HTTP. aws:PrincipalTag and aws:ResourceTag implement attribute-based access control (ABAC): one policy allows access only when the caller's tag, such as project, matches the resource's tag, so new projects need no new policies. The following table summarises these keys.

Condition keyExample use
aws:SourceIpOnly from the corporate IP range
aws:MultiFactorAuthPresentOnly when MFA was used
aws:RequestedRegionOnly in approved Regions
aws:PrincipalTag, aws:ResourceTagAttribute-based access control
aws:SecureTransportOnly over TLS
Match the pairs

Condition keys

Exam skills 1.1.2 · 1.3.6

Match each requirement with the IAM condition key that enforces it.

Log in to try this activity Interactive activities are free with an account.

Least privilege and ABAC

Least privilege means granting only the actions and resources an identity needs. In practice, policies start with the minimum and add permissions as needs appear, then are refined with IAM Access Analyzer, which can generate policies from actual activity, and last accessed information, which shows unused permissions.

Two models organise permissions. Role-based access control creates a policy for each job function, such as developer or auditor; it is simple when there are few teams. Attribute-based access control (ABAC) writes one policy that allows access when a tag on the principal matches a tag on the resource, such as project; new projects then need no new policies, only correctly tagged principals and resources, so ABAC scales better for many teams. The following ABAC statement lets developers manage only instances tagged with their own project.

{
  "Effect": "Allow",
  "Action": ["ec2:StartInstances", "ec2:StopInstances", "ec2:RebootInstances"],
  "Resource": "arn:aws:ec2:*:*:instance/*",
  "Condition": {
    "StringEquals": {"aws:ResourceTag/project": "${aws:PrincipalTag/project}"}
  }
}
Scenario questions

RBAC or ABAC?

Exam skills 1.1.2

Choose the access model for each organisation.

Log in to try this activity Interactive activities are free with an account.
Worked example

One policy for fifty project teams

Fifty project teams share an account. Each team must manage only its own EC2 instances and S3 prefixes, and new projects start every month.

ApproachPolicies neededEffort for a new project
One customer managed policy per team (RBAC)Fifty, each listing the team's resourcesWrite and attach a new policy
ABAC with tagsOne, comparing aws:PrincipalTag/project with aws:ResourceTag/projectTag the new team's role and resources

With ABAC, a developer whose role carries project=atlas can stop only instances tagged project=atlas. A tag policy and an SCP that denies creating untagged resources stop anyone escaping the scheme by leaving resources untagged.

Check yourself

What is the difference between an AWS managed policy and an inline policy?

Show answer

An AWS managed policy is created and updated by AWS and can be attached to many identities. An inline policy is embedded in a single user, group or role and is deleted with it.

Exam Tips

Remember for the exam

  • To restrict actions to certain Regions, use the aws:RequestedRegion condition.
  • To scale permissions across many projects, use ABAC with matching principal and resource tags.
  • For reusable least-privilege permissions, write customer managed policies.
  • To require MFA for sensitive actions, use the aws:MultiFactorAuthPresent condition.
  • To remove permissions a role never uses, use last accessed information or Access Analyzer unused access findings.