Browse the Repo

file-type-icon.circleci
file-type-icon_docs
file-type-iconexamples
file-type-iconmodules
file-type-iconattach-eni
file-type-icondisable-instance-metadata
file-type-iconbin
file-type-iconREADME.md
file-type-iconinstall.sh
file-type-iconec2-backup
file-type-iconpersistent-ebs-volume
file-type-iconrequire-instance-metadata-service-version
file-type-iconroute53-helpers
file-type-iconsingle-server
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.adoc
file-type-iconcore-concepts.md
file-type-iconterraform-cloud-enterprise-private-module-...

Browse the Repo

file-type-icon.circleci
file-type-icon_docs
file-type-iconexamples
file-type-iconmodules
file-type-iconattach-eni
file-type-icondisable-instance-metadata
file-type-iconbin
file-type-iconREADME.md
file-type-iconinstall.sh
file-type-iconec2-backup
file-type-iconpersistent-ebs-volume
file-type-iconrequire-instance-metadata-service-version
file-type-iconroute53-helpers
file-type-iconsingle-server
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.adoc
file-type-iconcore-concepts.md
file-type-iconterraform-cloud-enterprise-private-module-...
Single EC2 Instance

Single EC2 Instance

Run a single EC2 instance for stateless or stateful apps. Supports IAM roles, EBS volumes, ENIs, and EIPs.

Code Preview

Preview the Code

mobile file icon

README.md

down

Disable Instance Metadata Access script

This folder contains a script you can use to disable access to the Instance Metadata service once it is no longer required.

This script is particularly helpful in use-cases where you only want your instances to consult the Instance Metadata endpoint initially, perhaps during boot, in order to retrieve some necessary information.

Once that is done, you can call this script to further secure your instance by disabling any future access to the Instance Metadata service.

disable-instance-metadata: This script can be run on an EC2 instance to disable further access to the Instance Metadata service from that instance. It uses the AWS API to disable access to the endpoint.

Check out the route53-helpers example for how to use these scripts with Packer and Terraform.

Installing the scripts

You can install these scripts using the Gruntwork Installer:

gruntwork-install --module-name "disable-instance-metadata" --repo "https://github.com/gruntwork-io/terraform-aws-server" --tag "0.13.3"

Using the script

The disable-instance-metadata script has the following prerequisites:

  1. It must be run on an EC2 instance
  2. The EC2 instance must have an IAM role with permissions to modify the Instance Metadata service's options. See the route53-helpers example) for a reference implementation.
  3. The EC2 instance must have the AWS CLI (version 2.2.37 or higher), unzip and jq installed.

Run the disable-instance-metadata script in the User Data of your EC2 instances, after any required calls to the Instance Metadata service have been made. This way, your instances will still be able to access the Instance Metadata service when needed, but will also disable further access to the service upon boot.

Here is an example usage:

disable-instance-metadata

Example output:

Disabling instance metadata access...
{
    "InstanceId": "i-002132f6f69e13b22",
    "InstanceMetadataOptions": {
        "State": "pending",
        "HttpTokens": "optional",
        "HttpPutResponseHopLimit": 1,
        "HttpEndpoint": "disabled",
        "HttpProtocolIpv6": "disabled"
    }
}

This will result in subsequent calls to the Instance Metadata service to fail.

Questions? Ask away.

We're here to talk about our services, answer any questions, give advice, or just to chat.

Ready to hand off the Gruntwork?