Skip to content

Commit 1985c7e

Browse files
committed
Example with conditional and terraform.workspace
1 parent 765048a commit 1985c7e

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

website/docs/cdktf/concepts/tokens.mdx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ You may need to use Tokens for:
2121
- [Module outputs](/terraform/cdktf/concepts/modules) for boolean, string, lists, maps, and other complex types.
2222
- Resource attributes (such as `id`).
2323
- Terraform outputs based on resource attributes.
24-
- Using Terraforms `null` type.
24+
- Using Terraform's `null` type and [terraform.workspace](/terraform/language/expressions/references#filesystem-and-workspace-info).
2525

2626
### Example
2727

@@ -216,3 +216,32 @@ cdktf.Token_NullValue()
216216
```
217217

218218
</CodeTabs>
219+
220+
### Using `terraform.workspace`
221+
222+
In the code below, the outer `Token.as_number()` avoids:
223+
224+
> TypeError: type of argument count must be one of (int, float, cdktf.TerraformCount, NoneType); got jsii._reference_map.InterfaceDynamicProxy instead
225+
226+
The inner `Token.as_any()` avoids generating extra quotes `"terraform.workspace"` and extra dollar signs `"$${terraform.workspace}"`.
227+
228+
<CodeTabs>
229+
230+
```python
231+
from cdktf import App, Fn, Op, TerraformStack, Token
232+
from cdktf_cdktf_provider_null.resource import Resource
233+
234+
235+
stack = TerraformStack(App(), 'stack')
236+
Resource(
237+
stack,
238+
'this',
239+
count=Token.as_number(
240+
Fn.conditional(
241+
Op.eq(Token.as_any('terraform.workspace'), 'prod'), 1, 0
242+
)
243+
),
244+
)
245+
print(stack.to_hcl_terraform()['hcl'])
246+
```
247+
</CodeTabs>

0 commit comments

Comments
 (0)