Skip to content

Commit

Permalink
Merge pull request #32759 from kadrach/f-metadata-options-support-ipv…
Browse files Browse the repository at this point in the history
…6-22332

r/aws_instance: add support for http_protocol_ipv6 to metadata_options
  • Loading branch information
ewbankkit authored Aug 7, 2023
2 parents d09fc14 + 4d35702 commit d820b0f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changelog/32759.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_instance: Add `http_protocol_ipv6` attribute to `metadata_options` configuration block
```

```release-note:enhancement
data-source/aws_instance: Add `metadata_options.http_protocol_ipv6` attribute
```
11 changes: 11 additions & 0 deletions internal/service/ec2/ec2_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,12 @@ func ResourceInstance() *schema.Resource {
Default: ec2.InstanceMetadataEndpointStateEnabled,
ValidateFunc: validation.StringInSlice(ec2.InstanceMetadataEndpointState_Values(), false),
},
"http_protocol_ipv6": {
Type: schema.TypeString,
Optional: true,
Default: ec2.InstanceMetadataProtocolStateDisabled,
ValidateFunc: validation.StringInSlice(ec2.InstanceMetadataProtocolState_Values(), false),
},
"http_put_response_hop_limit": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -1813,6 +1819,7 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, meta in

if tfMap["http_endpoint"].(string) == ec2.InstanceMetadataEndpointStateEnabled {
// These parameters are not allowed unless HttpEndpoint is enabled.
input.HttpProtocolIpv6 = aws.String(tfMap["http_protocol_ipv6"].(string))
input.HttpPutResponseHopLimit = aws.Int64(int64(tfMap["http_put_response_hop_limit"].(int)))
input.HttpTokens = aws.String(tfMap["http_tokens"].(string))
input.InstanceMetadataTags = aws.String(tfMap["instance_metadata_tags"].(string))
Expand Down Expand Up @@ -3100,6 +3107,9 @@ func expandInstanceMetadataOptions(l []interface{}) *ec2.InstanceMetadataOptions

if m["http_endpoint"].(string) == ec2.InstanceMetadataEndpointStateEnabled {
// These parameters are not allowed unless HttpEndpoint is enabled
if v, ok := m["http_protocol_ipv6"].(string); ok && v != "" {
opts.HttpProtocolIpv6 = aws.String(v)
}

if v, ok := m["http_tokens"].(string); ok && v != "" {
opts.HttpTokens = aws.String(v)
Expand Down Expand Up @@ -3176,6 +3186,7 @@ func flattenInstanceMetadataOptions(opts *ec2.InstanceMetadataOptionsResponse) [

m := map[string]interface{}{
"http_endpoint": aws.StringValue(opts.HttpEndpoint),
"http_protocol_ipv6": aws.StringValue(opts.HttpProtocolIpv6),
"http_put_response_hop_limit": aws.Int64Value(opts.HttpPutResponseHopLimit),
"http_tokens": aws.StringValue(opts.HttpTokens),
"instance_metadata_tags": aws.StringValue(opts.InstanceMetadataTags),
Expand Down
4 changes: 4 additions & 0 deletions internal/service/ec2/ec2_instance_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ func DataSourceInstance() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"http_protocol_ipv6": {
Type: schema.TypeString,
Computed: true,
},
"http_put_response_hop_limit": {
Type: schema.TypeInt,
Computed: true,
Expand Down
1 change: 1 addition & 0 deletions internal/service/ec2/ec2_instance_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ func TestAccEC2InstanceDataSource_metadataOptions(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "metadata_options.#", resourceName, "metadata_options.#"),
resource.TestCheckResourceAttrPair(datasourceName, "metadata_options.0.http_endpoint", resourceName, "metadata_options.0.http_endpoint"),
resource.TestCheckResourceAttrPair(datasourceName, "metadata_options.0.http_protocol_ipv6", resourceName, "metadata_options.0.http_protocol_ipv6"),
resource.TestCheckResourceAttrPair(datasourceName, "metadata_options.0.http_tokens", resourceName, "metadata_options.0.http_tokens"),
resource.TestCheckResourceAttrPair(datasourceName, "metadata_options.0.http_put_response_hop_limit", resourceName, "metadata_options.0.http_put_response_hop_limit"),
resource.TestCheckResourceAttrPair(datasourceName, "metadata_options.0.instance_metadata_tags", resourceName, "metadata_options.0.instance_metadata_tags"),
Expand Down
6 changes: 6 additions & 0 deletions internal/service/ec2/ec2_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5007,6 +5007,7 @@ func TestAccEC2Instance_metadataOptions(t *testing.T) {
testAccCheckInstanceExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "metadata_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_endpoint", "enabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_protocol_ipv6", "disabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_tokens", "optional"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_put_response_hop_limit", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.instance_metadata_tags", "disabled"),
Expand All @@ -5018,6 +5019,7 @@ func TestAccEC2Instance_metadataOptions(t *testing.T) {
testAccCheckInstanceExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "metadata_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_endpoint", "disabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_protocol_ipv6", "disabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_tokens", "optional"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_put_response_hop_limit", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.instance_metadata_tags", "disabled"),
Expand All @@ -5029,6 +5031,7 @@ func TestAccEC2Instance_metadataOptions(t *testing.T) {
testAccCheckInstanceExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "metadata_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_endpoint", "enabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_protocol_ipv6", "enabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_tokens", "required"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_put_response_hop_limit", "2"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.instance_metadata_tags", "enabled"),
Expand All @@ -5040,6 +5043,7 @@ func TestAccEC2Instance_metadataOptions(t *testing.T) {
testAccCheckInstanceExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "metadata_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_endpoint", "enabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_protocol_ipv6", "disabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_tokens", "optional"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_put_response_hop_limit", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.instance_metadata_tags", "disabled"),
Expand Down Expand Up @@ -8542,6 +8546,7 @@ resource "aws_instance" "test" {
metadata_options {
http_endpoint = "enabled"
http_protocol_ipv6 = "enabled"
http_tokens = "required"
http_put_response_hop_limit = 2
instance_metadata_tags = "enabled"
Expand All @@ -8567,6 +8572,7 @@ resource "aws_instance" "test" {
metadata_options {
http_endpoint = "enabled"
http_protocol_ipv6 = "disabled"
http_tokens = "optional"
http_put_response_hop_limit = 1
instance_metadata_tags = "disabled"
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ interpolation.
* `auto_recovery` - Automatic recovery behavior of the instance.
* `metadata_options` - Metadata options of the Instance.
* `http_endpoint` - State of the metadata service: `enabled`, `disabled`.
* `http_protocol_ipv6` - Whether the IPv6 endpoint for the instance metadata service is `enabled` or `disabled`
* `http_tokens` - If session tokens are required: `optional`, `required`.
* `http_put_response_hop_limit` - Desired HTTP PUT response hop limit for instance metadata requests.
* `instance_metadata_tags` - If access to instance tags is allowed from the metadata service: `enabled`, `disabled`.
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ Metadata options can be applied/modified to the EC2 Instance at any time.
The `metadata_options` block supports the following:

* `http_endpoint` - (Optional) Whether the metadata service is available. Valid values include `enabled` or `disabled`. Defaults to `enabled`.
* `http_protocol_ipv6` - (Optional) Whether the IPv6 endpoint for the instance metadata service is enabled. Defaults to `disabled`.
* `http_put_response_hop_limit` - (Optional) Desired HTTP PUT response hop limit for instance metadata requests. The larger the number, the further instance metadata requests can travel. Valid values are integer from `1` to `64`. Defaults to `1`.
* `http_tokens` - (Optional) Whether or not the metadata service requires session tokens, also referred to as _Instance Metadata Service Version 2 (IMDSv2)_. Valid values include `optional` or `required`. Defaults to `optional`.
* `instance_metadata_tags` - (Optional) Enables or disables access to instance tags from the instance metadata service. Valid values include `enabled` or `disabled`. Defaults to `disabled`.
Expand Down

0 comments on commit d820b0f

Please sign in to comment.