snippetterraformMinor
How do I find out the attributes of a terraform object?
Viewed 0 times
thehowattributesfindobjectoutterraform
Problem
I'm creating a Terraform file, and something is obviously wrong,
How do I find out what the attributes of
Error: Incorrect condition type
on ssh.tf line 47, in locals:
47: is_generated = data.local_sensitive_file.public_key ? true : false
├────────────────
│ data.local_sensitive_file.public_key is object with 4 attributes
The condition expression must be of type bool.How do I find out what the attributes of
data.local_sensitive_file.public_key are though?Solution
Since
At the time of writing the documentation for
Provider documentation is typically the best place to find out about attributes of a resource type because it'll also tell you what these attributes represent, in addition to their names.
Not all objects you'll encounter in Terraform will originate in providers though, so a more general answer that will work for any object you can refer to in an expression is to run
Unfortunately for a resource (either a
local_sensitive_file here is a data source, you can assume that its attributes are the attributes defined by the provider that this data source belongs to, which is hashicorp/local.At the time of writing the documentation for
local_sensitive_file says that it has the following attributes:filename
content
content_base64
id
Provider documentation is typically the best place to find out about attributes of a resource type because it'll also tell you what these attributes represent, in addition to their names.
Not all objects you'll encounter in Terraform will originate in providers though, so a more general answer that will work for any object you can refer to in an expression is to run
terraform console and then ask it for the type of the value in question:> type(data.local_sensitive_file.public_key)
object({
content: string,
content_base64: string,
filename: string,
id: string,
})Unfortunately for a resource (either a
resource or data block) it won't yet have a known type until it's been created or read for the first time, and so if your configuration isn't valid then you would need to make it valid enough to run terraform apply successfully before you could use this technique. But for objects that are defined within the Terraform configuration itself, rather than by external operations that haven't run yet, the console is a useful way to inspect data and data types.Code Snippets
> type(data.local_sensitive_file.public_key)
object({
content: string,
content_base64: string,
filename: string,
id: string,
})Context
StackExchange DevOps Q#17429, answer score: 4
Revisions (0)
No revisions yet.