Enforce IMDSv2 for AWS EC2 Instances
Enforcing IMDSv2 for AWS EC2 instances enhances security by requiring session-based authentication to access instance metadata, mitigating the risk of unauthorized metadata exposure through vulnerabilities like SSRF (Server-Side Request Forgery). This helps ensure that only authorized applications and users can retrieve sensitive instance data.
This policy pack can help you configure the following settings for EC2 instances:
- Enforce IMDSv2, which requires session-based authentication
- Set the
PUTresponse hop limit to restrict IMDS access
Getting Started
Requirements
- Terraform
- Guardrails mods:
Credentials
To create a policy pack through Terraform:
- Ensure you have
Turbot/Adminpermissions (or higher) in Guardrails - Create access keys in Guardrails
And then set your credentials:
export TURBOT_WORKSPACE=myworkspace.acme.comexport TURBOT_ACCESS_KEY=acce6ac5-access-key-hereexport TURBOT_SECRET_KEY=a8af61ec-secret-key-herePlease see Turbot Guardrails Provider authentication for additional authentication methods.
Usage
Install Policy Pack
NoteBy default, installed policy packs are not attached to any resources.
Policy packs must be attached to resources in order for their policy settings to take effect.
Clone:
git clone https://github.com/turbot/guardrails-samples.gitcd guardrails-samples/policy_packs/aws/ec2/enforce_imdsv2_for_instancesRun the Terraform to create the policy pack in your workspace:
terraform initterraform planThen apply the changes:
terraform applyApply Policy Pack
Log into your Guardrails workspace and attach the policy pack to a resource.
If this policy pack is attached to a Guardrails folder, its policies will be applied to all accounts and resources in that folder. The policy pack can also be attached to multiple resources.
For more information, please see Policy Packs.
Enable Enforcement
TipYou can also update the policy settings in this policy pack directly in the Guardrails console.
Please note your Terraform state file will then become out of sync and the policy settings should then only be managed in the console.
By default, the policies are set to Check in the pack's policy settings. To enable automated enforcements, you can switch these policies settings by adding a comment to the Check setting and removing the comment from one of the listed enforcement options:
resource "turbot_policy_setting" "aws_ec2_instance_metadata_service" { resource = turbot_policy_pack.main.id type = "tmod:@turbot/aws-ec2#/policy/types/instanceMetadataService" # value = "Check: Enabled for V2 only" value = "Enforce: Enabled for V2 only"}Then re-apply the changes:
terraform planterraform apply