Skip to content

Commit

Permalink
Merge pull request #30107 from wzzzrd86/main
Browse files Browse the repository at this point in the history
Enable all metadata-options to be enabled regardless of http_endpoint setting for EC2 Launch Template
  • Loading branch information
johnsonaj committed Mar 29, 2023
2 parents 64354b4 + c1a3e88 commit d02edbd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .changelog/30107.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_launch_template: Allow `metadata_options` to be applied when `http_endpoint` is not configured
```
21 changes: 9 additions & 12 deletions internal/service/ec2/ec2_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1935,21 +1935,18 @@ func expandLaunchTemplateInstanceMetadataOptionsRequest(tfMap map[string]interfa

if v, ok := tfMap["http_endpoint"].(string); ok && v != "" {
apiObject.HttpEndpoint = aws.String(v)
}

if v == ec2.LaunchTemplateInstanceMetadataEndpointStateEnabled {
// These parameters are not allowed unless HttpEndpoint is enabled.
if v, ok := tfMap["http_tokens"].(string); ok && v != "" {
apiObject.HttpTokens = aws.String(v)
}
if v, ok := tfMap["http_tokens"].(string); ok && v != "" {
apiObject.HttpTokens = aws.String(v)
}

if v, ok := tfMap["http_put_response_hop_limit"].(int); ok && v != 0 {
apiObject.HttpPutResponseHopLimit = aws.Int64(int64(v))
}
if v, ok := tfMap["http_put_response_hop_limit"].(int); ok && v != 0 {
apiObject.HttpPutResponseHopLimit = aws.Int64(int64(v))
}

if v, ok := tfMap["instance_metadata_tags"].(string); ok && v != "" {
apiObject.InstanceMetadataTags = aws.String(v)
}
}
if v, ok := tfMap["instance_metadata_tags"].(string); ok && v != "" {
apiObject.InstanceMetadataTags = aws.String(v)
}

if v, ok := tfMap["http_protocol_ipv6"].(string); ok && v != "" {
Expand Down
29 changes: 29 additions & 0 deletions internal/service/ec2/ec2_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2945,6 +2945,23 @@ func TestAccEC2LaunchTemplate_metadataOptions(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.instance_metadata_tags", "enabled"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccLaunchTemplateConfig_metadataOptionsNoHTTPEndpoint(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckLaunchTemplateExists(ctx, resourceName, &template),
resource.TestCheckResourceAttr(resourceName, "metadata_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.http_endpoint", "enabled"), //Setting any of the values in metadata options will set the http_endpoint to enabled, you will not see it via the Console, but will in the API for any instance made from the template
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.http_protocol_ipv6", "disabled"),
resource.TestCheckResourceAttr(resourceName, "metadata_options.0.instance_metadata_tags", "enabled"),
),
},
},
})
}
Expand Down Expand Up @@ -4087,7 +4104,19 @@ resource "aws_launch_template" "test" {
}
`, rName)
}
func testAccLaunchTemplateConfig_metadataOptionsNoHTTPEndpoint(rName string) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
name = %[1]q
metadata_options {
http_tokens = "required"
http_put_response_hop_limit = 2
instance_metadata_tags = "enabled"
}
}
`, rName)
}
func testAccLaunchTemplateConfig_enclaveOptions(rName string, enabled bool) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
Expand Down

0 comments on commit d02edbd

Please sign in to comment.