Search code examples
amazon-web-servicesamazon-ec2terraformterraform-provider-awsopenai-api

Can anyone confirm if http://169.254.169.254/latest/meta-data/attributes/terraform is a real ec2 meta-data endpoint?


I was recently testing ChatGPT on terraform scripts and came across something that I can't tell if it's a GPT "cannon fodder" or if this is an undocumented feature.

On a trained ChatGPT session I input:

generate a bash script that can use random_pet resources from terraform

And this was the output:

#!/bin/bash

# Download metadata for the random_pet resource from the Terraform state file
metadata=$(curl -s http://169.254.169.254/latest/meta-data/attributes/terraform)

# Parse the metadata and extract the value of the "id" attribute
id=$(echo $metadata | jq -r '.random_pet_generator_id')

# Use the value of the "id" attribute in your Bash script
echo "Hello, $id!"

This looked bogus to me:

http://169.254.169.254/latest/meta-data/attributes/terraform

Until I read about Referring to Alternate Provider Configurations


Solution

  • @Marcin, I'm pretty sure that's not a real endpoint but hidden features are always a possibility. I ended up with:

    foo.tf

      # Enables access to instance tags from the instance metadata service.
      metadata_options {
        instance_metadata_tags = "enabled"
        http_endpoint          = "enabled"
      }
    
      # Apply tags to the resources during launch (does not include default_tags)
      dynamic "tag_specifications" {
        for_each = toset(["instance", "volume", "network-interface"])
        content {
          resource_type = tag_specifications.key
          tags = {
            baz = random_pet.generator.id
          }
        }
      }
    

    bar.sh

    BAZ_TAG=$(curl -s http://169.254.169.254/latest/meta-data/tags/instance/baz)
    

    I'm really curious how ChatGPT derived that endpoint. It definitely makes mistakes sometimes but even the mistakes are based on some element of truth.