hashicorp / hashicorp/packer-plugin-amazon

Respect the AWS account default when metadata_options.http_tokens is omitted

Open
#703 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
91
Forks
141
Avg merge
2d 18h
Merged PRs (30d)
3

Description

#### Community Note

Please vote on this issue by adding a 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to the original issue to help the community and maintainers prioritize this request.
Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request.
If you are interested in working on this issue or have submitted a pull request, please leave a comment.

#### Description

The Amazon builder currently treats an omitted `metadata_options.http_tokens` setting as `optional`. This causes Packer to explicitly send `HttpTokens=optional` in the `RunInstances` request.

When `http_tokens` is not configured in the HCL template, Packer should leave that request field unset so that AWS can apply the account-level default for the target Region.

**Current behavior**

Given an AWS account whose account-level IMDS default is configured as `required`:

```hcl
source "amazon-ebs" "example" {
region = "us-east-1"
source_ami = "ami-xxxxxxxx"
instance_type = "t3.small"
ssh_username = "ec2-user"

# metadata_options is intentionally omitted
}
```

Packer launches the temporary build instance with:

```text
HttpTokens=optional
```

This occurs because the Amazon plugin defaults `HttpTokens` to `optional` even when the setting is absent from the HCL configuration.

This explicit launch value takes precedence over the AWS account default and also takes precedence over an AMI configured with `ImdsSupport=v2.0`. If account-level IMDSv2 enforcement is enabled, the build may fail because Packer is explicitly requesting `optional`.

** Ideal (Expected) Behavior **

When `metadata_options.http_tokens` is omitted from the HCL template:

- Packer should not include `HttpTokens` in the EC2 launch request.
- AWS should then apply its normal precedence rules, including the account-level default.
- Explicit values should continue to work as they do today:

```hcl
metadata_options {
http_tokens = "optional"
}
```

or:

```hcl
metadata_options {
http_tokens = "required"
}
```

The `imds_support = "v2.0"` setting should remain independent; it configures the resulting AMI and should not implicitly configure the temporary Packer build instance.

## Proposed change

Preserve the distinction between an unset value and an explicitly configured `optional` value. The plugin should only populate `MetadataOptions.HttpTokens` when `metadata_options.http_tokens` was explicitly supplied by the user.

Changing the existing default should not be considered a compatibility concern, as anyone wanting the current behavior can simply code http_tokens explicitly. But I acknowledge there may be other ways to introduce the needed behavior and I'll defer to the experts on this. The main point is here should be a supported way for users to defer to the AWS account-level default, and not force the setting of `MetadataOptions.HttpTokens` to one way or the other as occurs now.

#### Potential configuration

No configuration changes are needed to implement this feature.

#### Potential References

- [[Amazon builder documentation](https://developer.hashicorp.com/packer/integrations/hashicorp/amazon/latest/components/builder/ebs)](https://developer.hashicorp.com/packer/integrations/hashicorp/amazon/latest/components/builder/ebs)
- [`[RunConfig.Prepare](https://github.com/hashicorp/packer-plugin-amazon/blob/main/common/run_config.go)`](https://github.com/hashicorp/packer-plugin-amazon/blob/main/common/run_config.go)
- [`[RunInstances](https://github.com/hashicorp/packer-plugin-amazon/blob/main/common/step_run_source_instance.go)`[ metadata configuration](https://github.com/hashicorp/packer-plugin-amazon/blob/main/common/step_run_source_instance.go)](https://github.com/hashicorp/packer-plugin-amazon/blob/main/common/step_run_source_instance.go)
- [[AWS metadata option precedence](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-options.html)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-options.html)

Contributor guide

Open the contributing guide

Research direction

Start in common/run_config.go at RunConfig.Prepare, then trace metadata handling into common/step_run_source_instance.go and the RunInstances request. Check how omitted and explicitly set http_tokens values are represented, and run the existing Amazon plugin tests around these paths. Done means omitted values leave MetadataOptions.HttpTokens unset while explicit optional and required values remain supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, go
Domain
cloud
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.