skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    @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.


  2. There is no terraform value in EC2 metadata. For the valid values please check Instance metadata categories – Amazon Elastic Compute Cloud.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search