Lesson 1 of 4 AWS IAM

Understanding IAM Identities, Credentials and Policy Types

This lesson introduces the building blocks of IAM: authentication and authorisation, the root user, users, groups and roles, the kinds of credential, the types of policy, Amazon Resource Names and the tools that help developers apply least privilege. The later lessons in this module build on these ideas.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish authentication from authorisation in an AWS API request.
  • Explain the purpose of the root user, IAM users, groups and roles, and why roles are preferred for applications.
  • Distinguish long-term access keys from temporary security credentials.
  • Distinguish AWS managed, customer managed, inline and resource-based policies from guardrails that only limit permissions.
  • Interpret the parts of an Amazon Resource Name.
  • Recognise errors caused by IAM's eventual consistency.

What IAM does

Every request to an AWS API, whether it comes from the console, the AWS CLI, an SDK or another AWS service, is checked by IAM in two steps. Authentication establishes who is making the request: the SDK signs each request with Signature Version 4 using the caller's credentials, and AWS verifies the signature. Authorisation then decides whether that identity may perform the requested action on the requested resource, by evaluating the policies that apply.

IAM is a global service: users, roles and policies are defined once per account and apply in every Region. It has no charge. Because every action depends on it, many errors that developers meet, such as AccessDenied or UnrecognizedClientException, are IAM errors, and telling an authentication failure from an authorisation failure is the first step in fixing them.

Principals and identities

A principal is an entity that can make requests to AWS. IAM provides the following identities, each suited to a different purpose.

The root user

The root user is created with the account and signs in with the account's email address. It has complete access to every resource, and IAM policies cannot restrict it, although SCPs can restrict the root user of a member account in AWS Organizations. It should be protected with multi-factor authentication (MFA), have no access keys, and be used only for the few tasks that require it, such as changing the account's support plan or closing the account.

IAM users

An IAM user represents one person or application and has long-term credentials: a console password, access keys, or both. These credentials remain valid until someone rotates or deletes them, so a leaked key gives an attacker lasting access. AWS therefore recommends federated sign-in through IAM Identity Center for people, and roles for applications. IAM users are now mainly kept for exceptions, such as a third-party tool that supports only access keys.

IAM groups

A group is a collection of IAM users, such as Developers, that makes it easier to give many users the same permissions: policies attached to the group apply to every member. A group is not a principal. It cannot sign in, cannot be named as the Principal in a resource-based policy and cannot contain other groups.

IAM roles

A role is an identity with permissions but no long-term credentials. A trusted entity, such as a Lambda function, an EC2 instance, a user in another account or a federated user, assumes the role and receives temporary credentials from AWS Security Token Service (AWS STS) that expire automatically. Each role has two policies: a trust policy that says who may assume it, and permissions policies that say what it may do. Roles are the standard way for applications to access AWS, as Giving Applications Access to AWS with IAM Roles explains. The following table summarises the identities.

IdentityCredentialsUse for
Root userEmail, password and MFAOnly the few tasks that require it
IAM userLong-term password and access keysExceptions that cannot use roles or federation
IAM groupNone; not a principalAttaching the same policies to many users
IAM roleTemporary credentials from STSApplications, AWS services, cross-account access and federated users

Credentials

Different tools authenticate with different credentials, and choosing the right kind is one of the most important security decisions a developer makes.

Access keys are used by the AWS CLI and SDKs. They consist of an access key ID, such as AKIAIOSFODNN7EXAMPLE, and a secret access key. An IAM user can have two at a time, so a new key can be created and deployed before the old one is deactivated. Access keys must never be stored in code, committed to a repository or baked into an image.

Temporary security credentials add a session token to the key ID and secret, and they expire after a set time, from 15 minutes to several hours. Their key IDs begin with ASIA. Roles, IAM Identity Center and federation all issue temporary credentials, which is why they are preferred: a leaked credential stops working on its own.

MFA adds a second factor, such as an authenticator app or a security key, to a password or to an STS request. Policies can require it with the aws:MultiFactorAuthPresent condition key.

On a developer's computer, the recommended set-up is a CLI profile that signs in through IAM Identity Center with aws sso login, so no long-term keys are stored. Code running on AWS receives a role's credentials automatically, and the SDK's default credential provider chain finds whichever credentials are available.

Types of policy

A policy is a JSON document that allows or denies actions, as Writing IAM Policies and Tracing How AWS Evaluates Them explains. Policies differ in where they are attached and in what they can do.

Identity-based policies

An identity-based policy is attached to a user, group or role and says what that identity may do. It comes in three forms. An AWS managed policy, such as AmazonS3ReadOnlyAccess or AWSLambdaBasicExecutionRole, is written and updated by AWS; it is convenient but often broader than a workload needs. A customer managed policy is written by the account owner, can be attached to many identities and keeps a version history, so it is the usual choice for least-privilege permissions. An inline policy is embedded in a single identity and is deleted with it, which suits permissions that must never be reused elsewhere.

Resource-based policies

A resource-based policy is attached to a resource and names the principals that may use it in a Principal element. Examples include S3 bucket policies, SQS queue policies, SNS topic policies, KMS key policies, Lambda function policies and the trust policy of a role. Resource-based policies are how one account grants access to another account, or how a service such as Amazon S3 is allowed to invoke a Lambda function.

Guardrails

Some policies never grant permissions; they only set a maximum. A permissions boundary limits what an identity's own policies can allow, which lets a team create roles without being able to exceed its own permissions. Service control policies (SCPs) and resource control policies (RCPs) in AWS Organizations set limits for whole accounts, and a session policy narrows the permissions of one assumed-role session. The following table summarises the policy types.

Policy typeAttached toGrants permissions?
AWS managed, customer managed or inlineA user, group or roleYes
Resource-basedA resource, such as a bucket, queue, key or functionYes, to the principals it names
Permissions boundaryA user or roleNo; it sets a maximum
SCP or RCPAn organisational unit or accountNo; it sets a maximum
Session policyOne assumed-role or federated sessionNo; it sets a maximum

Amazon Resource Names

Policies identify resources by their Amazon Resource Name (ARN), which has the form arn:partition:service:region:account-id:resource. The partition is aws for commercial Regions. Some fields are empty for global resources: IAM ARNs have no Region, and S3 bucket ARNs have neither a Region nor an account ID. The resource part can contain wildcards, such as *, to match many resources. The following table shows typical examples.

ResourceARN
Lambda functionarn:aws:lambda:eu-west-1:111122223333:function:orders-api
DynamoDB tablearn:aws:dynamodb:eu-west-1:111122223333:table/Orders
IAM rolearn:aws:iam::111122223333:role/orders-api-role
S3 objects under a prefixarn:aws:s3:::customer-docs/invoices/*

Tools for getting permissions right

Least privilege means granting only the permissions a workload needs, and IAM provides several tools that help developers find them, described in the following list.

  • IAM Access Analyzer validates policies as they are written, generates a policy from the actions a role actually used in AWS CloudTrail, and reports unused access and resources shared outside the account.
  • Last accessed information shows when each service, and for some services each action, was last used by an identity, so unused permissions can be removed.
  • The IAM policy simulator tests whether a set of policies allows a given action on a given resource without making the real call.
  • The credential report lists every IAM user with the age and last use of their passwords and access keys.

IAM is also eventually consistent. A new role, policy or change is replicated across AWS and may take a few seconds to take effect everywhere, so code and pipelines that create IAM resources and use them immediately should retry.

Worked example

A role that Lambda cannot assume

A deployment script creates an execution role and then a Lambda function that uses it, one command after the other. The second command often fails with InvalidParameterValueException: The role defined for the function cannot be assumed by Lambda, but running the script again a minute later succeeds.

The trust policy is correct, because the retry works. The cause is IAM's eventual consistency: the role existed, but the change had not yet reached the part of AWS that Lambda uses to assume it. The script should wait for the role, for example with aws iam wait role-exists followed by a short retry loop on the create-function call. Infrastructure as code tools such as AWS CloudFormation handle this wait automatically, which is one reason to prefer them for creating IAM resources.

Had the retry also failed, the next step would be to check the trust policy, which must allow lambda.amazonaws.com to call sts:AssumeRole, and to confirm that the caller has iam:PassRole permission for the role.

Check yourself

Why can an S3 bucket policy not name the Developers IAM group as its principal, and how can the group's members be given access?

Show answer

A group is not a principal: it never makes requests, so it cannot appear in a Principal element. Access is granted with an identity-based policy attached to the group, or by letting the users assume a role that the bucket policy names.

Exam Tips

Remember for the exam

  • Authentication establishes who signed a request; authorisation evaluates the policies for the action and resource. IAM is global and free.
  • Groups are not principals: they cannot be named in a resource-based policy or assume a role, and they cannot be nested.
  • Prefer roles and IAM Identity Center, which issue temporary credentials, over IAM users with long-term access keys.
  • Temporary credentials include a session token and have key IDs beginning with ASIA; long-term access keys begin with AKIA.
  • Customer managed policies are reusable and versioned; inline policies belong to one identity; AWS managed policies are convenient but often too broad.
  • Resource-based policies name a Principal and enable cross-account and service-to-service access. Permissions boundaries, SCPs and session policies only limit.
  • IAM changes are eventually consistent, so scripts that create a role and use it immediately should wait and retry.