Skip to main content

Enforcing IMDSv2 Across the AWS Account Stops GitLab Runner's Docker Machine Executor Launching CI Machines

High severityAmazon Web Services About 20 minutes to fix

Overview

GitLab's Docker Machine driver asks EC2 for IMDSv1 on every launch, so account-level IMDSv2 enforcement refuses its CI machines. Two MachineOptions fix it.

At a glance

Symptom
After IMDSv2 enforcement is turned on, the Docker Machine executor can no longer create new EC2 machines, so jobs that need a new machine…
Likely cause
The driver asks for IMDSv1 explicitly at launch, which overrides the account default, and enforcement refuses any instance that would allow IMDSv1.
Impact
Every pipeline that uses this runner slows and then stops as idle machines age out.
Verification signal
New machines launch with IMDSv2 required, and jobs can still use the instance profile.
Safe correction
Set the two metadata options on the runner, not the account, so its new machines require IMDSv2 and keep a hop limit of 2 for containers.
Rollback or recovery
Restoring the backup puts the runner back exactly where it was, which under enforcement means launches fail again.

Turning on account-level IMDSv2 enforcement in an AWS Region makes EC2 refuse every launch that asks for IMDSv1, and GitLab Runner’s Docker Machine executor asks for it on every launch unless you tell it otherwise. Its amazonec2 driver sends HttpTokens=optional and a hop limit of 1 in each RunInstances call, and a value set at launch overrides the account default. The fix is to add amazonec2-metadata-token=required and amazonec2-metadata-token-response-hop-limit=2 to the runner’s MachineOptions, so new machines launch with IMDSv2 required and CI/CD jobs and their services containers can still reach the instance credentials. Enforcement stays on.

Symptom

After IMDSv2 enforcement is turned on, the Docker Machine executor can no longer create new EC2 machines, so jobs that need a new machine cannot get one. The runner manager’s log shows the driver’s launch error wrapping EC2’s refusal:

Error launching instance: UnsupportedOperation: You can't launch instances with IMDSv1 because httpTokensEnforced is enabled for this account. Either launch the instance with httpTokens=required or contact your account owner to disable httpTokensEnforced using the ModifyInstanceMetadataDefaults API or the account settings in the EC2 console.

It usually does not start at the moment of the change. Enforcement does not alter instances that already exist, so idle machines keep taking jobs. The autoscaler removes each machine once it has been idle for longer than IdleTime and tries to create new ones to keep IdleCount machines waiting, and it is those new launches that fail. Capacity drains away over the following hours, which hides the link to the security change made earlier.

Why enforcement looks safe to switch on

The usual order is to set IMDSv2 as the account default first, watch for a while, then enforce. With the default set to required, new instances launch with HttpTokens required and a hop limit of 2, and launches that rely on the default carry on working. That quiet period reads as proof that every launch in the account already asks for IMDSv2. It only proves that nothing launched in that time overrode the default, and the Docker Machine driver overrides it on every launch.

Root Cause

The driver asks for IMDSv1 explicitly at launch, which overrides the account default, and enforcement refuses any instance that would allow IMDSv1. EC2 decides each instance’s metadata options in a fixed order. A value given at launch, in the request or a launch template, comes first and overrides the account setting and the AMI. Enforcement is evaluated after that order has settled the values, and refuses any instance that would come out with HttpTokens optional.

GitLab’s fork of Docker Machine added IMDSv2 support to the amazonec2 driver as two options, and kept IMDSv1 as their default. In the driver source, amazonec2-metadata-token defaults to optional and amazonec2-metadata-token-response-hop-limit defaults to 1, and the driver always puts both into the MetadataOptions of its RunInstances request. A runner configured before enforcement, with neither option in MachineOptions, therefore asks for IMDSv1 explicitly. The account default never gets a say, and enforcement refuses the launch.

Impact

Every pipeline that uses this runner slows and then stops as idle machines age out. Spot and on-demand machines go through the same RunInstances call, so switching purchase options does not help. The Docker Machine executor was deprecated in GitLab 17.5 and is due to be removed in GitLab 20.0 (May 2027), but GitLab still supports it until then.

The quick fixes, and what they cost

The quick way out is the one the error message offers: set HttpTokensEnforced back to disabled. That lifts enforcement for every new instance in the Region, not just the runner’s, and lets any launch come up with IMDSv1 again. IMDSv2’s session token is what blocks most SSRF attempts and open reverse proxies from reading instance metadata, and IMDSv1 has none of it, as EC2 instances without IMDSv2 enforcement let SSRF steal IAM role credentials explains. The other tempting move, once jobs inside containers lose credentials, is a large hop limit. AWS keeps the token’s TTL at 1 by default so it cannot leave an instance misconfigured as a router, NAT device or tunnel; 2 is what AWS recommends for containers, and a higher value lets the token travel further than the containers need.

How to confirm it without changing anything

  1. Read the account setting for the runner’s Region:
    aws ec2 get-instance-metadata-defaults --region eu-west-2

    The trap needs "HttpTokensEnforced": "enabled" under AccountLevel. ManagedBy tells you whether it was set in the account or by an AWS Organizations declarative policy, which decides who can change it.

  2. On the runner manager, look for the two options in the configuration file (/etc/gitlab-runner/config.toml when the runner runs as root):
    grep -n "amazonec2-metadata" /etc/gitlab-runner/config.toml

    No output means the driver is using its defaults, optional and 1.

  3. List the instances that still allow IMDSv1:
    aws ec2 describe-instances --filters "Name=metadata-options.http-tokens,Values=optional" --query "Reservations[*].Instances[*].[InstanceId]" --output text

    Runner machines launched before enforcement appear here. They are the ones still taking jobs.

Correction

Set the two metadata options on the runner, not the account, so its new machines require IMDSv2 and keep a hop limit of 2 for containers. Before you start, make sure the job images use an AWS CLI or SDK recent enough to call IMDSv2, because the new machines will reject IMDSv1 calls. The MetadataNoToken CloudWatch metric on today’s runner machines counts IMDSv1 calls; if it is above zero during pipelines, find and update that software first.

  1. Back up the configuration.
    sudo cp /etc/gitlab-runner/config.toml /etc/gitlab-runner/config.toml.bak
  2. Test the options once by hand. GitLab recommends running docker-machine create manually with the options you intend to put in MachineOptions before the runner uses them. Add --amazonec2-metadata-token required --amazonec2-metadata-token-response-hop-limit 2 to your usual flags. Remove it afterwards with docker-machine rm and its name.
  3. Add the two options to the runner. In the [runners.machine] section, add these two lines inside MachineOptions, next to the existing amazonec2-* entries:
    "amazonec2-metadata-token=required",
    "amazonec2-metadata-token-response-hop-limit=2",

    GitLab Runner checks config.toml for changes every 3 seconds and reloads it, so no restart is needed.

  4. Let the old machines age out. Machines launched with IMDSv1 keep working until they are removed after IdleTime. Under enforcement, an instance switched to required cannot be switched back to optional, so leave them alone rather than converting them by hand.

The hop limit matters as much as the token setting. The Docker Machine executor runs each job in a Docker container on the machine, and every image under the job’s services keyword runs in another container beside it. To IMDS, a call from inside a container is one hop further away than a call from the host, so with a hop limit of 1 the token response may never reach a job or service container. SDKs that require IMDSv2 then get no answer at all, and others retry and fall back to IMDSv1, which the machine now refuses.

Validation

New machines launch with IMDSv2 required, and jobs can still use the instance profile.

  1. Watch the runner manager’s log for the next machine creation. Pass when there is no UnsupportedOperation error and the machine joins the pool.
  2. Run the describe-instances command above again once the old machines have aged out. Pass when no runner machine is in the list.
  3. Run a pipeline whose job calls aws sts get-caller-identity from the job container, alongside at least one service. Pass when it returns an assumed-role ARN for the runner machine’s instance-profile role.
  4. Check MetadataNoTokenRejected for the new machines. Pass when it stays at zero, which means nothing in the jobs is still making IMDSv1 calls.

Rollback

Restoring the backup puts the runner back exactly where it was, which under enforcement means launches fail again.

  • The new options cause a problem: sudo cp /etc/gitlab-runner/config.toml.bak /etc/gitlab-runner/config.toml. The runner reloads it within seconds.
  • Only as a last resort, with the account owner’s agreement: aws ec2 modify-instance-metadata-defaults --region eu-west-2 --http-tokens-enforced disabled lifts enforcement for the whole Region. Turn it back on with --http-tokens-enforced enabled as soon as the runner is fixed. If ManagedBy shows declarative-policy, this command cannot change it; the policy owner has to.

Avoid it on the next IMDSv2 rollout

Before enforcement goes on in any Region, look for everything that sets HttpTokens explicitly at launch: Docker Machine and other autoscalers, launch templates and infrastructure-as-code. Those launches ignore the account default. AWS’s recommended path puts enforcement last, after IMDSv2 is required on every instance. The ImdsV1Enabled metric shows which instances still allow IMDSv1, even when nothing calls it, so check it for the Region before you enforce.

Plan the move to the GitLab Runner Autoscaler before Docker Machine’s removal in GitLab 20.0, and set IMDSv2 required with a hop limit of 2 for its instances from the start. If CI jobs go on from the instance role to a deployment role, remember that a role assumed through another role is capped at one hour, whatever its maximum session duration says.

3

Apply the safer control

Before you change production

Confirm the affected scope, export the current configuration, and test the replacement control in a non-production environment first.

Fix commands and configuration

amazonec2
HttpTokens=optional
amazonec2-metadata-token=required
4

When to escalate

Validation and rollback are covered in the sections above.

Escalate

Escalate when the blast radius is uncertain, the control cannot be tested safely, or remediation requires an outage or security exception.

The KBY Brief

Fixed? Get the next one before it hits you: The KBY Brief

Practical fixes for the systems you run, by email about twice a week. Unsubscribe at any time.

About this page

Credited to

Dates

Published

Corrections

No corrections are listed for this page. Some review sweeps corrected pages without naming each one; those are in the corrections log.

Was this useful?

Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.

After remediation

Further reading stays below the corrective workflow and is selected by platform, category and shared technical keywords.

Discover more

Connected KBY resources