Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable all metadata-options to be enabled regardless of http_endpoint setting for EC2 Launch Template #30107

Merged
merged 6 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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