Sub topic 12
Roles for AWS services and workloads
This chapter assumes you know what a role is, how a trust policy differs from a permissions policy, and that STS issues temporary credentials. Here you will see how those pieces apply when the caller is an AWS service or your own code running on AWS, not a person.
Why workloads use roles
A workload is a program that needs to call AWS APIs, such as a web app reading from S3 or a function writing to DynamoDB. You could give it an IAM user and access keys, but the keys would have to be stored somewhere, rotated by hand, and could leak. A role avoids this. The platform running your code assumes the role for it and delivers short-lived credentials that it renews automatically.
There are three related patterns, and they are easy to confuse:
| Pattern | Who assumes the role | Who controls the permissions |
|---|---|---|
| Service role | An AWS service acting on your behalf | You |
| Instance profile (a special case of a service role for EC2) | Code running on an EC2 instance | You |
| Service-linked role | One specific AWS service | The service |
Service roles
A service role is a role that an AWS service assumes to perform actions in your account. Its trust policy names the service as the principal. Examples include a Lambda execution role, a role CloudFormation uses to create your stack's resources, and a role AWS Glue uses to read your data.
You write two policies for it:
- A trust policy that says which service may assume the role.
- One or more permissions policies that say what the service may do once it has assumed it.
The service's identity appears in the trust policy as a service principal, which is a DNS-style name such as ec2.amazonaws.com or lambda.amazonaws.com.
Instance profiles for EC2
EC2 cannot attach a role to an instance directly. It uses an instance profile, which is a container that holds exactly one role. The instance is launched with the profile, and the role inside it supplies the credentials.
This is the trust policy from earlier chapters that lets EC2 assume a role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}When you create an EC2 role in the console, AWS creates an instance profile with the same name for you. With the CLI or infrastructure-as-code tools you create each piece yourself. Save the trust policy as ec2-trust.json, then run:
aws iam create-role \
--role-name AppServerRole \
--assume-role-policy-document file://ec2-trust.json
aws iam attach-role-policy \
--role-name AppServerRole \
--policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
aws iam create-instance-profile --instance-profile-name AppServerProfile
aws iam add-role-to-instance-profile \
--instance-profile-name AppServerProfile \
--role-name AppServerRole
aws ec2 run-instances \
--image-id <your-AMI-id> \
--instance-type t3.micro \
--iam-instance-profile Name=AppServerProfileHow the credentials reach your code
The instance metadata service (IMDS) is an endpoint at 169.254.169.254 that is reachable only from the instance itself. The AWS SDKs and CLI call it, obtain temporary credentials for the role, and refresh them before they expire. Your code never sees a key file.
Other workload platforms
The same idea applies on other compute services, though the name of the role differs.
- Lambda: the execution role is the role Lambda assumes to run your function. It normally needs permission to write logs, plus whatever your code calls.
- ECS: the task role gives permissions to your containers' code. The separate task execution role is used by the ECS agent to pull images and send logs. Putting application permissions on the execution role is a common mistake.
- EKS: pods can receive roles through IAM roles for service accounts or EKS Pod Identity, so each pod gets only its own permissions instead of inheriting the node's.
Passing a role to a service
When you give a service a role, you are letting it act with that role's permissions. AWS therefore requires the action iam:PassRole, and it is checked against the role being passed. Without this check, anyone who could launch an instance or create a function could attach a powerful role and gain its permissions.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "arn:aws:iam::111122223333:role/AppServerRole",
"Condition": {
"StringEquals": { "iam:PassedToService": "ec2.amazonaws.com" }
}
}
]
}Scope Resource to specific role ARNs, not *, and use iam:PassedToService to limit which service may receive them. A user who can pass an administrator role and create a Lambda function has effectively become an administrator.
Service-linked roles
A service-linked role is predefined by a service and linked to it. You can see them in IAM under the path /aws-service-role/, with names that start with AWSServiceRoleFor. Examples are the roles used by Elastic Load Balancing, Auto Scaling, and Organizations integrations.
They differ from ordinary service roles in several ways:
- The service defines the trust policy and permissions. You can view them but cannot edit them.
- The service often creates the role for you the first time you use a feature. For that to work, your identity needs permission to create it (
iam:CreateServiceLinkedRole). - You can delete the role only after the service no longer uses it.
Use a service-linked role when the service offers one. Use an ordinary service role when you need to choose the permissions yourself.
Common errors
- "User is not authorized to perform iam:PassRole". The identity launching the resource lacks
iam:PassRoleon that role, or theiam:PassedToServicecondition does not match. - Wrong principal in the trust policy. If the role trusts
lambda.amazonaws.combut you attach it to an EC2 instance, the assume step fails. The service principal must match the service using the role. - Stale keys hiding the role. The SDK looks for credentials in a fixed order, and environment variables and the shared credentials file come before instance metadata. A leftover
AWS_ACCESS_KEY_IDon the instance silently overrides the role. Runaws sts get-caller-identityto see which identity the code actually uses. - Putting the role where the profile belongs. In the CLI,
--iam-instance-profiletakes the profile's name or ARN, not the role's, and the two can differ. - Edits that seem to lag. Changes to a role's permissions usually take effect within seconds but are eventually consistent. Newly created roles or profiles may not be usable for a moment.
Exercise
A developer can create Lambda functions but receives "not authorized to perform iam:PassRole" when attaching the role ReportsExecRole. The role's trust policy already names lambda.amazonaws.com. What is missing, and how should you grant it?
Show answer
The developer's identity lacks iam:PassRole for that role. The trust policy controls whether Lambda may assume the role, not whether the developer may hand it over. Add an Allow for iam:PassRole with Resource set to the ARN of ReportsExecRole, and add a condition StringEquals on iam:PassedToService with the value lambda.amazonaws.com.
Exercise
An application on an EC2 instance with an instance profile still gets Access Denied on S3, and aws sts get-caller-identity shows an IAM user instead of the role. What is the likely cause?
Show answer
Static credentials, such as AWS_ACCESS_KEY_ID in the environment or a profile in the shared credentials file, take precedence over instance metadata in the credential lookup order. Remove them so the SDK falls through to the instance profile role.