Transition to using Instance Metadata Service Version 2 - Amazon Elastic Compute Cloud
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Transition to using Instance Metadata Service Version 2

When migrating to IMDSv2, we recommend that you use the following tools and transition path.

Tools for helping with the transition to IMDSv2

If your software uses IMDSv1, use the following tools to help reconfigure your software to use IMDSv2.

Amazon software

The latest versions of the Amazon CLI and Amazon SDKs support IMDSv2. To use IMDSv2, make sure that your EC2 instances have the latest versions of the CLI and SDKs. For information about updating the CLI, see Installing, updating, and uninstalling the Amazon CLI in the Amazon Command Line Interface User Guide.

All Amazon Linux 2 and Amazon Linux 2023 software packages support IMDSv2. In Amazon Linux 2023, IMDSv1 is disabled by default.

For the minimum Amazon SDK versions that support IMDSv2, see Use a supported Amazon SDK.

IMDS Packet Analyzer

The IMDS Packet Analyzer is an open-sourced tool that identifies and logs IMDSv1 calls from your instance’s boot phase. This can assist in identifying the software making IMDSv1 calls on EC2 instances, allowing you to pinpoint exactly what you need to update to get your instances ready to use IMDSv2 only. You can run IMDS Packet Analyzer from a command line or install it as a service. For more information, see IMDS Packet Analyzer on GitHub.

CloudWatch

IMDSv2 uses token-backed sessions, while IMDSv1 does not. The MetadataNoToken CloudWatch metric tracks the number of calls to the Instance Metadata Service (IMDS) that are using IMDSv1. By tracking this metric to zero, you can determine if and when all of your software has been upgraded to use IMDSv2.

After you've disabled IMDSv1, you can use the MetadataNoTokenRejected CloudWatch metric to track the number of times an IMDSv1 call was attempted and rejected. By tracking this metric, you can ascertain whether your software needs to be updated to use IMDSv2.

For more information, see Instance metrics.

Updates to EC2 APIs and CLIs

For new instances, you can use the RunInstances API to launch new instances that require the use of IMDSv2. For more information, see Configure instance metadata options for new instances.

For existing instances, you can use the ModifyInstanceMetadataOptions API to require the use of IMDSv2. For more information, see Modify instance metadata options for existing instances.

To require the use of IMDSv2 on all new instances launched by Auto Scaling groups, your Auto Scaling groups can use either a launch template or a launch configuration. When you create a launch template or create a launch configuration, you must configure the MetadataOptions parameters to require the use of IMDSv2. The Auto Scaling group launches new instances using the new launch template or launch configuration, but existing instances are not affected. For existing instances in an Auto Scaling group, you can use the ModifyInstanceMetadataOptions API to require the use of IMDSv2 on the existing instances, or terminate the instances and the Auto Scaling group will launch new replacement instances with the instance metadata options settings that are defined in the new launch template or launch configuration.

Use an AMI that configures IMDSv2 by default

When you launch an instance, you can automatically configure it to use IMDSv2 by default (the HttpTokens parameter is set to required) by launching it with an AMI that is configured with the ImdsSupport parameter set to v2.0. You can set the ImdsSupport parameter to v2.0 when you register the AMI using the register-image CLI command, or you can modify an existing AMI by using the modify-image-attribute CLI command. For more information, see Configure the AMI.

IAM policies and SCPs

You can use an IAM policy or Amazon Organizations service control policy (SCP) to control users as follows:

The IAM policy or SCP must contain the following IAM condition keys:

  • ec2:MetadataHttpEndpoint

  • ec2:MetadataHttpPutResponseHopLimit

  • ec2:MetadataHttpTokens

If a parameter in the API or CLI call does not match the state specified in the policy that contains the condition key, the API or CLI call fails with an UnauthorizedOperation response.

Furthermore, you can choose an additional layer of protection to enforce the change from IMDSv1 to IMDSv2. At the access management layer with respect to the APIs called via EC2 Role credentials, you can use a new condition key in either IAM policies or Amazon Organizations service control policies (SCPs). Specifically, by using the condition key ec2:RoleDelivery with a value of 2.0 in your IAM policies, API calls made with EC2 Role credentials obtained from IMDSv1 will receive an UnauthorizedOperation response. The same thing can be achieved more broadly with that condition required by an SCP. This ensures that credentials delivered via IMDSv1 cannot actually be used to call APIs because any API calls not matching the specified condition will receive an UnauthorizedOperation error.

For example IAM policies, see Work with instance metadata. For more information on SCPs, see Service Control Policies in the Amazon Organizations User Guide.

Using the above tools, we recommend that you follow this path for transitioning to IMDSv2.

Step 1: At the start

Update the SDKs, CLIs, and your software that use Role credentials on their EC2 instances to versions compatible with IMDSv2. For information about updating the CLI, see Upgrading to the latest version of the Amazon CLI in the Amazon Command Line Interface User Guide.

Then, change your software that directly accesses instance metadata (in other words, that does not use an SDK) using the IMDSv2 requests. You can use the IMDS Packet Analyzer to identify the software that you need to change to use IMDSv2 requests.

Step 2: Track your transition progress

Track your transition progress by using the CloudWatch metric MetadataNoToken. This metric shows the number of IMDSv1 calls to the IMDS on your instances. For more information, see Instance metrics.

Step 3: When there is zero IMDSv1 usage

When the CloudWatch metric MetadataNoToken records zero IMDSv1 usage, your instances are ready to be fully transitioned to using IMDSv2. At this stage, you can do the following:

  • Account default

    You can set IMDSv2 to be required as an account default. When an instance is launched, the instance configuration is automatically set to the account default.

    To set the account default, do the following:

    • Amazon EC2 console: On the EC2 Dashboard, under Account attributes, Data protection and security, for IMDS defaults, set Instance metadata service to Enabled and Metadata version to V2 only (token required). For more information, see Set IMDSv2 as the default for the account.

    • Amazon CLI: Use the modify-instance-metadata-defaults CLI command and specify --http-tokens required and --http-put-response-hop-limit 2.

  • New instances

    When launching a new instance, you can do the following:

    • Amazon EC2 console: In the launch instance wizard, set Metadata accessible to Enabled and Metadata version to V2 only (token required). For more information, see Configure the instance at launch.

    • Amazon CLI: Use the run-instances CLI command and specify that IMDSv2 is required.

  • Existing instances

    For existing instances, you can do the following:

    • Amazon EC2 console: On the Instances page, select your instance, choose Actions, Instance settings, Modify instance metadata options, and for IMDSv2, choose Required. For more information, see Require the use of IMDSv2.

    • Amazon CLI: Use the modify-instance-metadata-options CLI command to specify that only IMDSv2 is to be used.

    You can modify the instance metadata options on running instances, and you don't need to restart the instances after modifying the instance metadata options.

Step 4: Check if your instances are transitioned to IMDSv2

You can check if any instances are not yet configured to require the use of IMDSv2, in other words, IMDSv2 is still configured as optional. If any instances are still configured as optional, you can modify the instance metadata options to make IMDSv2 required by repeating the preceding Step 3.

To filter your instances:

  • Amazon EC2 console: On the Instances page, filter your instances by using the IMDSv2 = optional filter. For more information about filtering, see Filter resources using the console. You can also view whether IMDSv2 is required or optional for each instance: In the Preferences window, toggle on IMDSv2 to add the IMDSv2 column to the Instances table.

  • Amazon CLI: Use the describe-instances CLI command and filter by metadata-options.http-tokens = optional, as follows:

    aws ec2 describe-instances --filters "Name=metadata-options.http-tokens,Values=optional" --query "Reservations[*].Instances[*].[InstanceId]" --output text

Step 5: When all of your instances are transitioned to IMDSv2

The ec2:MetadataHttpTokens, ec2:MetadataHttpPutResponseHopLimit, and ec2:MetadataHttpEndpoint IAM condition keys can be used to control the use of the RunInstances and the ModifyInstanceMetadataOptions APIs and corresponding CLIs. If a policy is created, and a parameter in the API call does not match the state specified in the policy using the condition key, the API or CLI call fails with an UnauthorizedOperation response. For example IAM policies, see Work with instance metadata.

Furthermore, after you've disabled IMDSv1, you can use the MetadataNoTokenRejected CloudWatch metric to track the number of times an IMDSv1 call was attempted and rejected. If, after disabling IMDSv1, you have software that is not working properly and the MetadataNoTokenRejected metric records IMDSv1 calls, it's likely that this software needs to be updated to use IMDSv2.