AWS Builder Center
Enforcing IMDSv2 with Service Control Policies: Building Defense in Depth

Enforcing IMDSv2 with Service Control Policies: Building Defense in Depth

Learn how to enforce IMDSv2 across your AWS organization using Service Control Policies. This guide shows you how to implement organization-wide IMDSv2 enforcement that strengthens your security posture through defense-in-depth. Includes tested SCP policy, deployment steps, and verification results.

Understanding IMDS and the Opportunity for Improvement

The Instance Metadata Service (IMDS) provides EC2 instances with essential information including IAM role credentials, instance details, and user data. These temporary credentials enable applications to authenticate with AWS services without hardcoded keys—a significant security improvement over static credentials.
The evolution: IMDSv1 uses simple HTTP GET requests to retrieve credentials. While functional, this design creates an opportunity for improvement in defense-in-depth strategies. If an application has certain vulnerabilities, the simple request pattern makes credential theft easier:
# Attacker exploits SSRF to make your application request:
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/MyRole

# Returns temporary credentials (valid for ~6 hours):
{
"AccessKeyId": "ASIAXXX...",
"SecretAccessKey": "wJalrXUtn...",
"Token": "IQoJb3JpZ2luX2VjE...",
"Expiration": "2025-11-25T16:45:00Z"
}

# if stolen, these credentials works until expiration:
export AWS_ACCESS_KEY_ID="ASIAXXX..."
export AWS_SECRET_ACCESS_KEY="wJalrXUtn..."
export AWS_SESSION_TOKEN="IQoJb3JpZ2luX2VcE..."
aws s3 ls # Attacker has your instance's IAM role permissions

All IMDS credentials are temporary and work from any location once obtained—this is true for both v1 and v2. The opportunity with IMDSv2 is its enhanced defense-in-depth approach: by requiring PUT requests and token handling, it provides additional protection layers that complement existing security controls, making it more resilient against certain attack patterns.
Organizations have opportunities to strengthen their security posture through consistent enforcement. While IMDSv2 is available, the default setting (HttpTokens: optional) allows both v1 and v2, creating an opportunity for standardization. Establishing IMDSv2-only as an organization standard demonstrates security leadership and provides infrastructure-level controls that complement IAM policies and application-level security.

IMDSv2 Solution

IMDSv2 (Instance Metadata Service Version 2) uses session-based authentication that blocks SSRF attacks. Instead of simple GET requests, it requires a two-step process:
# Step 1: Get session token (requires PUT request)
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" \
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600")

# Step 2: Use token in subsequent requests
curl -H "X-aws-ec2-metadata-token: $TOKEN" \
http://169.254.169.254/latest/meta-data/iam/security-credentials/MyRole

IMDSv2 provides security improvements by making credential theft significantly harder. The PUT method requirement for token retrieval blocks most SSRF vulnerabilities, which typically only allow GET requests. The multi-step process with token handling adds complexity that prevents simple exploitation. Note that once credentials are obtained—whether through IMDSv1 or IMDSv2—they work the same way: they're temporary (typically 6 hours) and usable from any location until expiration. The key difference is that IMDSv2 makes obtaining those credentials much more difficult for attackers. Modern AWS SDKs automatically handle IMDSv2, meaning most applications require no code changes to benefit from these improvements.
Key Differentiator: Unlike manual configuration or IAM policies, Service Control Policies enforce IMDSv2 at the AWS Organizations level, preventing any account from launching vulnerable instances regardless of permissions.

Implementation with Service Control Policies

Service Control Policies provide organization-wide enforcement that cannot be bypassed. The policy denies EC2 instance launches unless IMDSv2 is required:

SCP Policy

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RequireIMDSv2OnLaunch",
"Effect": "Deny",
"Action": "ec2:RunInstances",
"Resource": "arn:aws:ec2:*:*:instance/*",
"Condition": {
"StringNotEquals": {
"ec2:MetadataHttpTokens": "required"
}
}
},
{
"Sid": "PreventDisablingIMDSv2",
"Effect": "Deny",
"Action": "ec2:ModifyInstanceMetadataOptions",
"Resource": "*",
"Condition": {
"StringNotEquals": {
"ec2:MetadataHttpTokens": "required"
}
}
}
]
}


How It Works

The SCP evaluates instance launch requests before they're executed. When a user attempts to launch an EC2 instance via CLI, Console, SDK, or API, AWS Organizations evaluates the SCP conditions against the request. If the HttpTokens setting is not "required", the SCP denies the action and the user receives a clear error message indicating SCP denial. The user then configures IMDSv2 (HttpTokens: required) and successfully launches the instance.
Key architectural benefit: The HttpTokens configuration is stored in AWS's control plane, not inside the EC2 instance. This means the setting is enforced at the infrastructure level and cannot be modified from within the instance, providing a strong security boundary.

Deployment Steps

1. Create and attach the SCP policy:
# Create policy in AWS Organizations
aws organizations create-policy \
--name "Require-IMDSv2" \
--description "Enforces IMDSv2 on all EC2 instances" \
--type SERVICE_CONTROL_POLICY \
--content file://imds-scp-policy.json

# Attach to specific OU (e.g., Sandbox)
aws organizations attach-policy \
--policy-id p-xxxxxxxxx \
--target-id ou-xxxx-xxxxxxxx

2. Verify enforcement:
Test that IMDSv1 launches are blocked:
# This should FAIL with "service control policy" error
aws ec2 run-instances \
--image-id ami-xxxxx \
--instance-type t3.micro \
--metadata-options HttpTokens=optional

# This should SUCCEED
aws ec2 run-instances \
--image-id ami-xxxxx \
--instance-type t3.micro \
--metadata-options HttpTokens=required,HttpPutResponseHopLimit=1


Verification Results

Testing via AWS CLI confirms the SCP enforcement works as expected:
Test Scenario
Result
CLI launch with IMDSv1 (HttpTokens: optional)
✓ BLOCKED
CLI launch with IMDSv2 (HttpTokens: required)
✓ ALLOWED
The testing confirms that SCP enforcement works consistently across all AWS regions. Even users with AdministratorAccess cannot bypass the SCP, and error messages clearly indicate "service control policy" denial. Properly configured instances with IMDSv2 required launch without issues.

Security Impact

Before SCP enforcement, organizations typically have mixed IMDS configurations across accounts with no organization-level standardization, resulting in inconsistent security posture and manual compliance checking requirements.
After implementing SCP enforcement, organizations achieve three key outcomes: consistent IMDSv2 usage organization-wide, infrastructure-level enforcement that cannot be bypassed, and automated compliance through preventive controls rather than detective measures.
The measurable improvements include 100% of new instances using IMDSv2 and a clear audit trail of enforcement actions through CloudTrail, reducing the security review burden through automated enforcement.

Next Steps

Service Control Policies transform IMDSv2 from a best practice recommendation into an organization-wide standard. By enforcing IMDSv2 at the infrastructure level, SCPs provide a defense-in-depth layer that complements your existing security controls and demonstrates security leadership.
Immediate Actions:
Start by creating the SCP policy in your AWS Organizations management account and testing enforcement in a sandbox OU to validate behavior. Once validated, roll out the SCP to additional OUs based on your organization's change management process while monitoring CloudTrail for denied attempts and providing team support as needed.
The goal is not to burden developers with security configuration, but to make secure infrastructure the default path forward. SCPs achieve this by enforcing IMDSv2 at the organization level, ensuring consistent security standards across all accounts and workloads.

References

[1] AWS Security Blog. "Defense in depth: Open firewalls, reverse proxies, SSRF vulnerabilities, EC2 Instance Metadata service."  https://aws.amazon.com/blogs/security/defense-in-depth-open-firewalls-reverse-proxies-ssrf-vulnerabilities-ec2-instance-metadata-service/ 
[4] AWS Well-Architected Framework. "Security Pillar."  https://docs.aws.amazon.com/wellarchitected/latest/security-pillar/welcome.html 
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article