Lesson 2 of 6 Compute Security: EC2, Containers and Lambda

Delivering Workload Credentials Safely with IAM Roles and IMDSv2

This lesson explains how each compute type delivers short-lived credentials from IAM roles, so workloads never need long-term keys stored on servers or in code.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how IMDSv2 protects instance credentials from SSRF.
  • Identify the role each compute service uses to deliver credentials.
  • Explain how instance credentials used outside AWS are detected.
  • Enforce IMDSv2 across an organisation.

EC2 and IMDSv2

EC2 instances obtain credentials through the instance metadata service, which must be protected. The following list describes instance profiles and IMDSv2.

  • An instance profile attaches a role; apps get temporary credentials from the instance metadata service (IMDS).
  • IMDSv2 requires a session token obtained with HTTP PUT, blocking most SSRF and open-proxy credential theft possible with IMDSv1.
  • Set HttpTokens=required and a hop limit of 1 (2 if containers on the instance need IMDS).
  • Enforce with an SCP on ec2:MetadataHttpTokens, a declarative policy or account defaults.

The following diagram matches each point of control to the mechanism that enforces IMDSv2 across an organisation.

Enforcing IMDSv2 after an SSRF test retrieved instance credentials. One SCP statement refuses launches that do not require session tokens and another stops anyone but PlatformAdmin weakening the setting; existing instances are switched to required tokens, after the MetadataNoToken metric shows who still uses IMDSv1.
Account-level defaults or a declarative policy are alternatives for new instances.
Spot the bug

An IMDSv2 SCP that enforces nothing

Exam skills 3.2.2 · 6.1.3

Find the lines that stop this SCP from requiring IMDSv2 at launch.

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

Roles by compute type

Each compute service delivers temporary credentials to workloads through its own kind of role. Amazon ECS uses two. The task role gives the application containers their AWS permissions, such as reading a DynamoDB table. The task execution role is used by the ECS agent, not the application, to pull images from ECR, send logs to CloudWatch and fetch secrets for injection into containers. Amazon EKS uses EKS Pod Identity or IAM roles for service accounts (IRSA) to give each Kubernetes service account its own role, so pods do not inherit the broader permissions of the node's role.

A Lambda function's execution role defines what the function can do, while a separate resource-based policy controls who can invoke it. Services such as CodeBuild and Glue assume a service role to act on your behalf. Their trust policy should include aws:SourceArn and aws:SourceAccount conditions, so that another customer cannot trick the service into using your role, a problem known as the confused deputy. The following table summarises the roles.

ComputeRoleGrants
ECSTask roleThe application containers' AWS permissions
ECSTask execution roleThe ECS agent: pull from ECR, send logs, fetch secrets for injection
EKSPod Identity or IRSAPer-service-account roles, so pods do not use the node role
LambdaExecution roleWhat the function can do (a resource-based policy controls who can invoke it)
Services (CodeBuild, Glue…)Service roleActing on your behalf; use aws:SourceArn and aws:SourceAccount in the trust policy to prevent confused deputy

Confusing the ECS task role and task execution role is a common exam trap.

Sort into groups

Task role or task execution role?

Exam skills 3.2.2

Decide which ECS role needs each permission.

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

Detecting misuse

GuardDuty raises InstanceCredentialExfiltration findings when instance role credentials are used outside AWS or from another account.

True or false

Workload identity facts

Exam skills 3.2.2

Check how compute workloads receive and protect credentials.

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

Enforcing IMDSv2 everywhere

A penetration test retrieved instance credentials through an SSRF flaw. The organisation decides that no instance may run with IMDSv1. An SCP refuses launches that do not require session tokens, and stops anyone weakening the setting afterwards:

{
  "Version": "2012-10-17",
  "Statement": [
    { "Sid": "RequireIMDSv2AtLaunch", "Effect": "Deny", "Action": "ec2:RunInstances",
      "Resource": "arn:aws:ec2:*:*:instance/*",
      "Condition": { "StringNotEquals": { "ec2:MetadataHttpTokens": "required" } } },
    { "Sid": "NoWeakeningMetadataOptions", "Effect": "Deny", "Action": "ec2:ModifyInstanceMetadataOptions",
      "Resource": "*",
      "Condition": { "StringNotLike": { "aws:PrincipalArn": "arn:aws:iam::*:role/PlatformAdmin" } } }
  ]
}
# Fix existing instances (hop limit 2 if containers on the host need IMDS)
aws ec2 modify-instance-metadata-options --instance-id i-0123456789abcdef0 \
  --http-tokens required --http-put-response-hop-limit 1

Setting the account-level defaults for new instances, or a declarative policy, achieves the same result with fewer launch failures for teams whose templates omit the option. The CloudWatch metric MetadataNoToken shows which instances still make IMDSv1 calls before enforcement.

Check yourself

Which instance metadata setting protects instance credentials from SSRF attacks?

Show answer

Requiring IMDSv2, whose session tokens an SSRF request cannot obtain.

Exam Tips

Remember for the exam

  • An SSRF vulnerability that exposes instance credentials calls for IMDSv2 as a requirement.
  • When an application in an ECS task needs S3 access, grant it through the task role, not the execution role.
  • When the ECS agent needs to pull images and read secrets, grant this through the task execution role.
  • For per-pod AWS permissions in EKS, use Pod Identity or IRSA.
  • To enforce IMDSv2 across the organisation, use an SCP on ec2:MetadataHttpTokens or a declarative policy.
  • Never store access keys on instances.