How Tokens Work
In TypeScript, when you assignresourceA.id to a property of resourceB, the value of resourceA.id is not yet available. Instead of a literal string, tfts uses a Token.
Tokens are represented internally as special markers: ${TfToken[unique_id]}. These markers are placeholders that the framework resolves during the synthesis process.
Token Types
tfts provides several functions to create and work with tokens:ref(fqn, attribute): Creates a reference token to a resource attribute (e.g.,aws_instance.main.id).fn(name, ...args): Creates a token representing a Terraform function call (e.g.,element(var.list, 0)).raw(value): Wraps a raw string as a token, preventing it from being escaped or modified.lazy(() => value): Creates a token whose value is calculated only when it is resolved during synthesis.
Resolution during Synthesis
When you callapp.synth(), the framework performs the following steps:
- Traversal: It walks the construct tree.
- Resolution: It identifies all token markers within the resource properties.
- Mapping: It replaces each marker with the actual Terraform HCL expression (e.g.,
${aws_instance.main.id}).
resolveTokens() function.
Creating Custom Tokens
While most tokens are created automatically by the resource classes, you can create your own usingcreateToken().
Why Tokens Matter
Tokens allow tfts to maintain type safety while dealing with the dynamic nature of infrastructure. They ensure that:- Dependencies are tracked: By referencing a token from another resource, tfts automatically knows that the second resource depends on the first.
- Lazy Evaluation: Values are only computed when needed, allowing for complex logic that depends on the final state of the tree.
- HCL Compatibility: Tokens ensure that the generated HCL uses the correct Terraform interpolation syntax.