Skip to content

Commit 5ce2561

Browse files
committed
Functions page
1 parent 2d98a5e commit 5ce2561

File tree

4 files changed

+78
-28
lines changed

4 files changed

+78
-28
lines changed

examples/python/documentation/functions.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,50 @@
22
# SPDX-License-Identifier: MPL-2.0
33

44
from constructs import Construct
5-
from cdktf import TerraformStack, App, TerraformVariable, Token
5+
from cdktf import Op, TerraformStack, TerraformVariable, Token
6+
67
# DOCS_BLOCK_START:functions-usage-example
78
from cdktf import Fn, TerraformOutput
89
from imports.aws.provider import AwsProvider
910
from imports.aws.data_aws_availability_zones import DataAwsAvailabilityZones
1011
# DOCS_BLOCK_END:functions-usage-example
12+
from imports.aws.instance import Instance
13+
1114

1215
class FunctionsStack(TerraformStack):
1316
def __init__(self, scope: Construct, id: str):
1417
super().__init__(scope, id)
15-
AwsProvider(self, "aws",
18+
AwsProvider(self, "aws",
1619
region="us-east-1"
1720
)
1821

1922
# DOCS_BLOCK_START:functions-usage-example
20-
2123
zones = DataAwsAvailabilityZones(self, 'zones',
22-
state="available",
23-
)
24+
state="available",
25+
)
2426

2527
TerraformOutput(self, 'first-zone',
2628
value=Fn.element(zones.names, 0)
2729
)
28-
2930
# DOCS_BLOCK_END:functions-usage-example
3031

32+
# DOCS_BLOCK_START:conditional
33+
Instance(
34+
self,
35+
'web',
36+
ami='ami-2757f631',
37+
count=Token.as_number(
38+
Fn.conditional(Op.eq(Token.as_any('terraform.workspace'), 'prod'), 2, 1)
39+
),
40+
instance_type='t2.micro',
41+
)
42+
# DOCS_BLOCK_END:conditional
43+
3144
# INTERNAL NOTE: Due to an JSII bug, we have to pass the variable as a string_value in Python
3245
# We can remove it, once https://github.com/aws/jsii/pull/4209 is released
3346
# DOCS_BLOCK_START:functions-lookup
3447
v = TerraformVariable(self, "complex-object",
35-
type = 'object({users: list(object({name: string}))})',
48+
type='object({users: list(object({name: string}))})',
3649
)
3750
TerraformOutput(self, 'users',
3851
value=Fn.lookup(v.string_value, "users")
@@ -50,4 +63,3 @@ def __init__(self, scope: Construct, id: str):
5063
value=Fn.raw_string('${TEMPLATE}')
5164
)
5265
# DOCS_BLOCK_END:functions-raw-string
53-

examples/typescript/documentation/functions.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ import { TerraformStack, TerraformVariable } from "cdktf";
55
import { Construct } from "constructs";
66
import { AwsProvider } from "@cdktf/provider-aws/lib/aws-provider";
77
// DOCS_BLOCK_END:functions
8+
// DOCS_BLOCK_START:conditional
9+
import { Token } from "cdktf";
10+
import { Instance } from "@cdktf/provider-aws/lib/instance"
11+
// DOCS_BLOCK_END:conditional
12+
// DOCS_BLOCK_START:conditional,operators,functions,functions-raw
13+
import { Fn } from "cdktf";
14+
// DOCS_BLOCK_END:conditional,operators,functions,functions-raw
815
// DOCS_BLOCK_START:operators,functions,functions-raw
9-
import { Fn, TerraformOutput } from "cdktf";
16+
import { TerraformOutput } from "cdktf";
1017
// DOCS_BLOCK_END:operators,functions,functions-raw
11-
// DOCS_BLOCK_START:operators,functions-raw
18+
// DOCS_BLOCK_START:conditional,operators,functions-raw
1219
import { Op } from "cdktf";
13-
// DOCS_BLOCK_END:operators,functions-raw
20+
// DOCS_BLOCK_END:conditional,operators,functions-raw
1421
// DOCS_BLOCK_START:functions-raw,functions
1522
import { DataAwsAvailabilityZones } from "@cdktf/provider-aws/lib/data-aws-availability-zones";
1623
// DOCS_BLOCK_END:functions-raw,functions
@@ -37,6 +44,16 @@ export class FunctionsStack extends TerraformStack {
3744
});
3845
// DOCS_BLOCK_END:functions
3946

47+
// DOCS_BLOCK_START:conditional
48+
new AwsInstance(this, "web", {
49+
ami: "ami-2757f631",
50+
count: Token.as_number(
51+
Fn.conditional(Op.eq(Token.as_any("terraform.workspace"), "prod"), 2, 1)
52+
),
53+
instance_type: "t2.micro",
54+
});
55+
// DOCS_BLOCK_END:conditional
56+
4057
// DOCS_BLOCK_START:functions-lookup
4158
const v = new TerraformVariable(this, "complex_object", {
4259
type: "object({users: list(object({name: string}))})",

website/docs/cdktf/concepts/functions.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ func NewFunctionsStack(scope constructs.Construct, name string) cdktf.TerraformS
159159

160160
## Special functions
161161

162+
### Conditional Expressions
163+
The ternary [conditional expression](/terraform/language/expressions/conditionals) is supported in CDKTF as a function. Its first argument for the condition or predicate is usually an [operator](/terraform/cdktf/concepts/functions#operators) such as `Op.eq()` to ensure a runtime comparison. Programming language operators like `==` will be evaluated at synthesis time and the result hardcoded into the generated JSON or HCL. Depending on usage, output and inputs may need their [Token](/terraform/cdktf/concepts/tokens) types specified.
164+
165+
<!-- #NEXT_CODE_BLOCK_SOURCE:python examples/typescript/documentation#functions-conditional -->
166+
<!-- #NEXT_CODE_BLOCK_SOURCE:python examples/python/documentation#functions-conditional -->
167+
168+
<CodeTabs>
169+
</CodeTabs>
170+
171+
162172
### Property Access Helpers
163173

164174
To access nested properties from untyped objects or other datasources that return a dynamic datatype, use the Terraform function `lookup` or, for nested access, the function "Fn.lookupNested()" which is a function offered by CDKTF that allows to avoid nesting `Fn.lookup` calls.
@@ -169,6 +179,8 @@ To access nested properties from untyped objects or other datasources that retur
169179
<!-- #NEXT_CODE_BLOCK_SOURCE:csharp examples/csharp/documentation#functions-lookup -->
170180
<!-- #NEXT_CODE_BLOCK_SOURCE:go examples/go/documentation#functions-lookup -->
171181

182+
<CodeTabs>
183+
172184
```ts
173185
const v = new TerraformVariable(this, "complex_object", {
174186
type: "object({users: list(object({name: string}))})",
@@ -230,6 +242,8 @@ cdktf.NewTerraformOutput(stack, jsii.String("first-user-name"), &cdktf.Terraform
230242
})
231243
```
232244

245+
</CodeTabs>
246+
233247
### Raw string helper
234248

235249
Another helper function offered by CDKTF is `Fn.rawString` which can be used to escape raw strings that contain characters that CDKTF or Terraform would try to interpret otherwise.
@@ -240,6 +254,8 @@ Another helper function offered by CDKTF is `Fn.rawString` which can be used to
240254
<!-- #NEXT_CODE_BLOCK_SOURCE:csharp examples/csharp/documentation#functions-raw-string -->
241255
<!-- #NEXT_CODE_BLOCK_SOURCE:go examples/go/documentation#functions-raw-string -->
242256

257+
<CodeTabs>
258+
243259
```ts
244260
new TerraformOutput(this, "quotes", {
245261
value: Fn.rawString(`"b"`),
@@ -287,6 +303,8 @@ cdktf.NewTerraformOutput(stack, jsii.String("template"), &cdktf.TerraformOutputC
287303
})
288304
```
289305

306+
</CodeTabs>
307+
290308
## Operators
291309

292310
Use the `Op` object to include operators like `!`, `+`, and `-`.

website/docs/cdktf/concepts/tokens.mdx

Lines changed: 20 additions & 17 deletions
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 Terraform's `null` type and [terraform.workspace](/terraform/language/expressions/references#filesystem-and-workspace-info).
24+
- Using Terraform's `null` type and [terraform.workspace](/terraform/language/state/workspaces#current-workspace-interpolation).
2525

2626
### Example
2727

@@ -226,27 +226,30 @@ In the code below, the outer `Token.as_number()` avoids:
226226
The inner `Token.as_any()` avoids generating extra quotes `"terraform.workspace"` and extra dollar signs `"$${terraform.workspace}"`.
227227

228228
<CodeTabs>
229-
```python
230-
from cdktf import App, Fn, Op, TerraformStack, Token
231-
from cdktf_cdktf_provider_null.resource import Resource
232-
229+
<!-- #NEXT_CODE_BLOCK_SOURCE:python examples/python/documentation#functions-conditional -->
230+
<!-- #NEXT_CODE_BLOCK_SOURCE:ts examples/typescript/documentation#functions-conditional -->
233231

234-
stack = TerraformStack(App(), 'stack')
235-
Resource(
236-
stack,
237-
'this',
238-
count=Token.as_number(
239-
Fn.conditional(
240-
Op.eq(Token.as_any('terraform.workspace'), 'prod'), 1, 0
232+
```python
233+
Instance(
234+
self,
235+
'web',
236+
ami='ami-2757f631',
237+
count=Token.as_number(
238+
Fn.conditional(Op.eq(Token.as_any('terraform.workspace'), 'prod'), 2, 1)
239+
),
240+
instance_type='t2.micro',
241241
)
242-
),
243-
)
244-
print(stack.to_hcl_terraform()['hcl'])
242+
```
243+
244+
```ts
245+
foo
245246
```
246247

247248
```terraform
248-
resource "null_resource" "this" {
249-
count = (terraform.workspace == "prod") ? 1 : 0
249+
resource "aws_instance" "web" {
250+
ami = "ami-2757f631"
251+
count = (terraform.workspace == "prod") ? 2 : 1
252+
instance_type = "t2.micro"
250253
}
251254
```
252255
</CodeTabs>

0 commit comments

Comments
 (0)