AWSSupport-ConfigureEC2Metadata
Description
This runbook helps you configure instance metadata service (IMDS) options for Amazon Elastic Compute Cloud (Amazon EC2) instances. Using this runbook, you can configure the following:
-
Enforce the use of IMDSv2 for instance metadata.
-
Configure the
HttpPutResponseHopLimitvalue. -
Allow or deny instance metadata access.
For more information about instance metadata, see Configuring the Instance Metadata Service in the Amazon EC2 User Guide.
Document type
Automation
Owner
Amazon
Platforms
Linux, macOS, Windows
Parameters
-
AutomationAssumeRole
Type: String
Description: (Optional) The Amazon Resource Name (ARN) of the Amazon Identity and Access Management (IAM) role that allows Systems Manager Automation to perform the actions on your behalf. If no role is specified, Systems Manager Automation uses the permissions of the user that starts this runbook.
-
EnforceIMDSv2
Type: String
Valid values: required | optional | unchanged
Default: optional
Description: (Optional) Enforce IMDSv2. If you choose
required, the Amazon EC2 instance will only use IMDSv2. If you chooseoptional, you can choose between IMDSv1 and IMDSv2 for metadata access. Specifyunchangedto keep the current value instead of applying the default.Important
If you enforce IMDSv2, applications that use IMDSv1 might not function correctly. Before enforcing IMDSv2, make sure your applications that use IMDS are upgraded to a version that support IMDSv2. For information about Instance Metadata Service Version 2 (IMDSv2), see Configuring the Instance Metadata Service in the Amazon EC2 User Guide.
-
HttpPutResponseHopLimit
Type: Integer
Valid values: 0-64
Default: 0
Description: (Optional) The desired HTTP PUT response hop limit value (1-64) for instance metadata requests. This value controls the number of hops that the PUT response can traverse. To prevent the response from traveling outside of the instance, specify
1for the parameter value. Specify0to keep the current value. -
InstanceId
Type: String
Description: (Required) The ID of the Amazon EC2 instance whose metadata settings you want to configure.
-
MetadataAccess
Type: String
Valid values: enabled | disabled | unchanged
Default: enabled
Description: (Optional) Allow or deny instance metadata access in the Amazon EC2 instance. If you specify
disabled, all other parameters will be ignored and the metadata access will be denied for the instance. Specifyunchangedto keep the current value instead of applying the default. If the current value isdisabled,unchangedalso ignores all other parameters.
Required IAM permissions
The AutomationAssumeRole parameter requires the following actions to
use the runbook successfully.
-
ec2:DescribeInstances -
ec2:ModifyInstanceMetadataOptions -
ssm:GetAutomationExecution -
ssm:StartAutomationExecution
Document Steps
-
getCurrentMetadataOptions - Gets the current metadata options of the instance and resolves
unchangedto the current value. -
branchOnMetadataAccess - Branches automation based on the value of
MetadataAccessparameter. -
disableMetadataAccess - Calls the ModifyInstanceMetadataOptions API action to disable metadata endpoint access.
-
branchOnHttpPutResponseHopLimit - Branches automation based on the value of
HttpPutResponseHopLimitparameter. -
maintainHopLimitAndConfigureImdsVersion - If
HttpPutResponseHopLimitis 0, maintains current hop limit and changes other metadata options. -
waitBeforeAssertingIMDSv2State - Waits 30 seconds before asserting IMDSv2 status.
-
setHopLimitAndConfigureImdsVersion - If
HttpPutResponseHopLimitis greater than 0, configures the metadata options using the given input parameters. -
castHttpPutResponseHopLimit - Casts the value of the
HttpPutResponseHopLimitparameter to a string. -
waitBeforeAssertingHopLimit - Waits 30 seconds before asserting metadata options.
-
assertHopLimit - Asserts the
HttpPutResponseHopLimitproperty is set to the value you specified. -
branchOnIMDSv2Option - Branches verification based on the value of
EnforceIMDSv2parameter. -
assertIMDSv2IsOptional - Asserts
HttpTokensvalue set tooptional. -
assertIMDSv2IsEnforced - Asserts
HttpTokensvalue set torequired. -
waitBeforeAssertingMetadataState - Waits 30 seconds before asserting the metadata state is disabled.
-
assertMetadataIsDisabled - Asserts metadata is
disabled. -
describeMetadataOptions - Gets the metadata options after the changes you've specified have been applied.
Outputs
getCurrentMetadataOptions.InitialState
getCurrentMetadataOptions.InitialMetadataAccess
getCurrentMetadataOptions.InitialIMDSv2
getCurrentMetadataOptions.InitialHttpPutResponseHopLimit
describeMetadataOptions.State
describeMetadataOptions.MetadataAccess
describeMetadataOptions.IMDSv2
describeMetadataOptions.HttpPutResponseHopLimit