Sub topic 14
Giving instances permissions with IAM roles
This chapter assumes you can launch an instance with the AWS CLI and that you know what an IAM policy is. It covers how an instance gets AWS permissions without stored access keys, and how code on the instance reads its metadata safely.
Why use a role instead of access keys
An application on an instance often needs to call AWS, for example to read from S3. Copying an IAM user's access keys onto the instance is risky, because the keys are long-lived and can leak through images, logs, or backups.
An IAM role avoids this. A role is an identity with permissions but no permanent credentials. The instance assumes the role and receives temporary credentials that AWS rotates automatically.
EC2 attaches a role to an instance through an instance profile. An instance profile is a container that holds one role. The console creates the profile for you. The CLI does not, so you create it yourself.
Create and attach a role
The role needs a trust policy that lets the EC2 service assume it. Then you attach a permissions policy, wrap the role in an instance profile, and associate the profile with the instance.
cat > trust.json <<'EOF'
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}]
}
EOF
aws iam create-role --role-name web-role \
--assume-role-policy-document file://trust.json
aws iam attach-role-policy --role-name web-role \
--policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
aws iam create-instance-profile --instance-profile-name web-profile
aws iam add-role-to-instance-profile \
--instance-profile-name web-profile --role-name web-role
aws ec2 associate-iam-instance-profile \
--instance-id i-0123456789abcdef0 \
--iam-instance-profile Name=web-profileYou can also pass --iam-instance-profile Name=web-profile to run-instances. The AWS CLI and SDKs on the instance find the credentials automatically, so no configuration is needed.
How credentials reach your code
The credentials are served by the instance metadata service (IMDS). This is an HTTP endpoint at 169.254.169.254, reachable only from the instance itself. The SDK reads it for you, but you can call it directly.
Reading metadata with IMDSv2
IMDSv2 is the session-based version of the service. You first request a token with a PUT request, then send it in a header on every read. Simple GET-only attacks, such as a server-side request forgery (SSRF) bug that tricks an application into fetching a URL, usually cannot get a token. That protects the role credentials.
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
http://169.254.169.254/latest/meta-data/instance-idThe launch command in an earlier chapter set --metadata-options HttpTokens=required. That setting rejects IMDSv1 requests, which need no token.
Common errors
- A 401 response from the metadata service means the token is missing, expired, or malformed.
- An
AccessDeniederror from AWS means the role's policy lacks the action. It does not mean the credentials are missing. - If the instance profile was created but never added to the instance, the credentials path returns 404.
Exercise
Why does requiring IMDSv2 reduce the damage from an SSRF bug in a web application on the instance?
Show answer
IMDSv2 needs a PUT request to get a session token before any read. A typical SSRF bug can only make simple GET requests, so it cannot obtain the token or read the role credentials.