Lesson 3 of 4 AWS IAM

Giving Applications Access to AWS with IAM Roles

This lesson explains how IAM roles provide temporary credentials through AWS STS. Roles are the preferred way to give applications access to AWS, in place of long-term access keys.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the role that each AWS compute service uses to provide credentials to code.
  • Describe the order in which the default credential provider chain searches for credentials.
  • Explain the two permissions that cross-account role assumption requires.
  • Explain why iam:PassRole is required to hand a role to a service.

Roles for AWS compute

Code running on AWS compute services should never contain access keys. Instead, each service assumes an IAM role on the code's behalf and makes the role's temporary credentials available, and the AWS SDK's default credential provider chain finds them without any configuration. The credentials are rotated automatically before they expire.

On EC2, a role is attached to the instance through an instance profile, a container for the role. The SDK reads the credentials from the instance metadata service. In Lambda, the execution role is part of the function's configuration, and its credentials are placed in the function's environment variables. In ECS, each task definition names a task role, whose credentials are served to the task's containers from a credentials endpoint, so different tasks on the same instance have different permissions. The separate task execution role is used by ECS itself to pull images and write logs, not by the application. Pods on EKS use EKS Pod Identity or IAM roles for service accounts in the same way. The following table summarises the mechanism for each service.

ComputeHow it gets a role
EC2An instance profile
LambdaThe execution role
ECSThe task role

Access keys should never be hard-coded in application code.

The default credential provider chain

The AWS SDKs and the AWS CLI do not require code to say where credentials come from. Instead, they search a fixed sequence of locations, called the default credential provider chain, and use the first credentials they find. This is what lets the same code use a developer's profile on a laptop and the attached role in Lambda or on EC2 without any change. The search order is as follows.

  1. Environment variables, such as AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN.
  2. The shared config and credentials files, including IAM Identity Center (SSO) profiles and profiles that assume a role.
  3. Container credentials, such as an ECS task role served from the container credentials endpoint.
  4. The EC2 instance metadata service, which provides the instance profile's role credentials.

Because environment variables are checked first, stale access keys left in them override everything else, including an attached role. This is a common reason for code running as an unexpected identity, with permissions that do not match the role the developer configured. aws sts get-caller-identity shows which identity is actually in use.

The following diagram shows the order in which the SDK searches for credentials.

The default credential provider chain. The SDK and CLI look in environment variables, then the shared config and credentials files, then container credentials, then the EC2 instance metadata service, and use the first credentials they find. Stale keys in environment variables therefore override an attached role; aws sts get-caller-identity shows the identity in use.
The search stops at the first credentials found, so a location earlier in the chain hides every later one.
Put in order

The default credential provider chain

Exam skills 2.1.3 · 4.1.1

Put the places the SDK looks for credentials in the order it checks them.

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

Cross-account access

A principal calls sts:AssumeRole on a role in the other account. Two permissions are needed:

  • The role's trust policy must allow the calling account or principal.
  • The caller's own policy must allow sts:AssumeRole on that role.

Example. Account A (111122223333) assumes ReportReader in account B (222233334444). The role's trust policy:

{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": {"AWS": "arn:aws:iam::111122223333:root"},
    "Action": "sts:AssumeRole",
    "Condition": {"StringEquals": {"sts:ExternalId": "reports-7f3a"}}
  }]
}
aws sts assume-role --role-arn arn:aws:iam::222233334444:role/ReportReader \
  --role-session-name alice-reports --external-id reports-7f3a
# Returns AccessKeyId, SecretAccessKey and SessionToken, valid for one hour by default

The following diagram shows the two checks in the cross-account example above.

Cross-account access with a role. A developer in account A calls sts:AssumeRole on the ReportReader role in account B, with an external ID. The developer's identity policy must allow sts:AssumeRole on the role, and the role's trust policy must allow account A. STS returns temporary credentials, valid for one hour by default, which the developer uses to reach resources in account B.
Each account controls its own side: account A decides who may ask, and account B decides whom it trusts.

Passing roles to services

Many services act with a role that a user chooses when configuring them: a Lambda function's execution role, an EC2 instance profile, a CloudFormation stack's service role or a CodeBuild project's service role. Handing a role to a service in this way is called passing the role, and it requires the iam:PassRole permission for that role, in addition to the permission for the service action itself, such as lambda:CreateFunction.

The permission exists to prevent privilege escalation. Without it, a developer who may create Lambda functions could create one with an administrator role and then use the function to do anything that role allows. Granting iam:PassRole only for specific roles, for example roles whose names begin with app-, and optionally only to a specific service with the iam:PassedToService condition, keeps developers within the permissions intended for them. A request that fails with an error mentioning iam:PassRole means this permission is missing.

Scenario questions

Roles for applications

Exam skills 2.1.6 · 2.1.7

Practise giving applications and services access with roles.

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

Credentials that work locally but not in Lambda

Code that reads from DynamoDB works on a developer's laptop but fails in Lambda with AccessDeniedException. The default credential provider chain explains why:

Where the code runsCredentials found by the SDK
LaptopThe developer's profile in ~/.aws/credentials (an administrator)
LambdaThe function's execution role, through environment variables set by Lambda

The code is identical; the identity is different. The fix is to grant the execution role the DynamoDB permissions the function needs, not to embed the developer's keys in the function. Testing locally with aws sts assume-role into the same role (or sam local with a profile for that role) would have exposed the problem before deployment.

Check yourself

What permission does a developer need to create a Lambda function that uses an existing execution role?

Show answer

iam:PassRole on that role, in addition to lambda:CreateFunction. PassRole controls which roles a user can hand to an AWS service.

Exam Tips

Remember for the exam

  • An application on EC2 should use an IAM role through an instance profile, never access keys.
  • Cross-account access uses a role in the target account whose trust policy allows the source account.
  • If an application uses the wrong credentials, check for environment variables overriding the role.
  • Creating a Lambda function with a role fails without iam:PassRole for that role.