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.
| Compute | How it gets a role |
|---|---|
| EC2 | An instance profile |
| Lambda | The execution role |
| ECS | The 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.
- Environment variables, such as
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEYandAWS_SESSION_TOKEN. - The shared config and credentials files, including IAM Identity Center (SSO) profiles and profiles that assume a role.
- Container credentials, such as an ECS task role served from the container credentials endpoint.
- 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
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:AssumeRoleon 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 defaultThe following diagram shows the two checks in the cross-account example above.

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.
Roles for applications
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 runs | Credentials found by the SDK |
|---|---|
| Laptop | The developer's profile in ~/.aws/credentials (an administrator) |
| Lambda | The 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.
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:PassRolefor that role.