Sub topic 12
Roles for AWS services and workloads
This chapter assumes you know what a role and a trust policy are, and that you have used aws sts assume-role at least once. Here you will see how AWS services and the applications you run on them get credentials without any stored access keys.
Why services need roles
An EC2 instance, a Lambda function, or an ECS task often has to call other AWS APIs. Putting an access key in the code or in a config file makes that key a long-lived secret that someone can copy. A role removes the secret. The service assumes the role for you and hands your application temporary credentials that expire and rotate automatically.
There are three related kinds of role. They differ in who creates them and who may change them.
| Kind | Who defines it | Typical use |
|---|---|---|
| Service role | You | A service acts for you, such as Lambda reading from S3 |
| Instance profile | You | A container that attaches one role to an EC2 instance |
| Service-linked role | The AWS service | Predefined permissions the service needs to manage resources in your account |
Service roles
A service role is an ordinary IAM role whose trust policy names an AWS service as the principal. The trust policy below lets EC2 assume the role.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}The role also needs a permissions policy, such as the S3 read policy from the policy chapters. The trust policy decides who can become the role. The permissions policy decides what the role can do once assumed.
Instance profiles
EC2 does not attach a role to an instance directly. It attaches an instance profile, which is a container that holds one role. When you create a role for EC2 in the console, IAM creates an instance profile with the same name. With the CLI or CloudFormation, you create it yourself.
aws iam create-instance-profile --instance-profile-name AppProfile
aws iam add-role-to-instance-profile \
--instance-profile-name AppProfile \
--role-name AppRoleThe application on the instance then asks the instance metadata service for credentials. AWS SDKs and the CLI do this automatically, so your code needs no credential settings.
Service-linked roles
A service-linked role is defined by an AWS service and linked to it. The service owns the trust policy and the permissions policy, and you can view the role but not edit its permissions. Often the service creates it the first time you use a feature. Deleting it may be blocked while the service still uses it.
Common errors
- Missing
iam:PassRole. To give a service a role, such as when launching an instance with a profile or creating a Lambda function, your own identity needsiam:PassRoleon that role's ARN. Without it, the call fails with an authorization error even though the role is correct. - Wrong trust principal. A role trusted by
lambda.amazonaws.comcannot be used by EC2. The service reports that it cannot assume the role. - Using access keys on the instance. Keys in environment variables or
~/.aws/credentialstake precedence over the instance role in the SDK credential chain. Remove them so the role is used. - Expecting instant changes to the permissions policy to be invisible. Policy edits apply to the role's existing sessions after a short propagation delay, so you do not need to restart the instance.
Exercise
A Lambda function fails to start because the role cannot be assumed. The role has a correct S3 permissions policy and its trust policy names ec2.amazonaws.com. What is wrong, and what do you change?
Show answer
The trust policy names the wrong service. Lambda must be the trusted principal, so change the Principal to { "Service": "lambda.amazonaws.com" }. The permissions policy is not the cause, because it only applies after the role is assumed.