How to use terraform output as input variable of same terraform template?

How to use terraform output as input variable in same terraform template?

data "linode_instances" "my-instances" {}
output "instance_id" {
value = data.linode_instances.my-instances.instances.0.id
}

I want to use this output as input variable in below api in place of linode_id.

curl -H "Authorization: Bearer $token" https://api.linode.com/v4/linode/instances/{linode_id} | json_pp

1 Reply

While I don't have an exact answer for how to do this, I have a few suggestions that may help you get closer to your end goal.

Linode CLI

First, one of our developers offered this suggestion. They said you could create a linode_token as part of your TF. You could then take the value of that and pass it in as a StackScript variable, and have your StackScript (which you mentioned in a previous post) install the linode-cli and from there it could query the instances under your account to get the information your need.

This page in the Linode Terraform Registry. They also suggested that to just see instances, you could set the scope to "linodes:read_only."

Linode API

I also played around with the API to see how you might use X-filters to limit your search. If you set up your Terraform file to choose a label that contains a specific word or words, you could then search for that word with the API and output the ID of any instance containing that word. For example, if you create servers for this purpose that all contain the "NewInstance" in the label, you could search for the IDs like this:

curl -H "Authorization: Bearer $TOKEN" \
    -H 'X-Filter: { "label": {"+contains": "NewInstance" } }' https://api.linode.com/v4/linode/instances | jq | grep "id"

Metadata Service

Finally, you may want to look into our Metadata service, either as a way of getting information about your instance. One of my teammates mentioned querying the Metadata Service and injecting that into further action with Ansible and thought it might work for Terraform as well. That makes sense, but you'd need to do some additional research to figure out the specifics. I will say that since our Metadata service works with cloud-init, which is widely used in the industry, you can likely find many more sources about how to use it. Here are a few guides that may help you get started:

Hopefully something in there gets you closer to your goal or someone else in our Community is able to weigh in in the future with more precise guidance.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct