Skip to content

Commit c114bb8

Browse files
committed
chore(examples): Document conditional
1 parent 765048a commit c114bb8

File tree

4 files changed

+115
-20
lines changed

4 files changed

+115
-20
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:functions-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:functions-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:functions-conditional
9+
import { Token } from "cdktf";
10+
import { Instance } from "@cdktf/provider-aws/lib/instance";
11+
// DOCS_BLOCK_END:functions-conditional
12+
// DOCS_BLOCK_START:functions-conditional,operators,functions,functions-raw
13+
import { Fn } from "cdktf";
14+
// DOCS_BLOCK_END:functions-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:functions-conditional,operators,functions-raw
1219
import { Op } from "cdktf";
13-
// DOCS_BLOCK_END:operators,functions-raw
20+
// DOCS_BLOCK_END:functions-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:functions-conditional
48+
new Instance(this, "web", {
49+
ami: "ami-2757f631",
50+
count: Token.asNumber(
51+
Fn.conditional(Op.eq(Token.asAny("terraform.workspace"), "prod"), 2, 1),
52+
),
53+
instanceType: "t2.micro",
54+
});
55+
// DOCS_BLOCK_END:functions-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: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ The `element` function gets the first element from the list of Availability Zone
3636
import { TerraformStack, TerraformVariable } from "cdktf";
3737
import { Construct } from "constructs";
3838
import { AwsProvider } from "@cdktf/provider-aws/lib/aws-provider";
39-
import { Fn, TerraformOutput } from "cdktf";
39+
import { Fn } from "cdktf";
40+
import { TerraformOutput } from "cdktf";
4041
import { DataAwsAvailabilityZones } from "@cdktf/provider-aws/lib/data-aws-availability-zones";
4142
export class FunctionsStack extends TerraformStack {
4243
constructor(scope: Construct, id: string) {
@@ -79,15 +80,13 @@ import imports.aws.data_aws_availability_zones.DataAwsAvailabilityZonesConfig;
7980
from cdktf import Fn, TerraformOutput
8081
from imports.aws.provider import AwsProvider
8182
from imports.aws.data_aws_availability_zones import DataAwsAvailabilityZones
82-
8383
zones = DataAwsAvailabilityZones(self, 'zones',
84-
state="available",
85-
)
84+
state="available",
85+
)
8686

8787
TerraformOutput(self, 'first-zone',
8888
value=Fn.element(zones.names, 0)
8989
)
90-
9190
```
9291

9392
```csharp
@@ -159,6 +158,15 @@ func NewFunctionsStack(scope constructs.Construct, name string) cdktf.TerraformS
159158

160159
## Special functions
161160

161+
### Conditional Expressions
162+
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></CodeTabs>
169+
162170
### Property Access Helpers
163171

164172
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 +177,8 @@ To access nested properties from untyped objects or other datasources that retur
169177
<!-- #NEXT_CODE_BLOCK_SOURCE:csharp examples/csharp/documentation#functions-lookup -->
170178
<!-- #NEXT_CODE_BLOCK_SOURCE:go examples/go/documentation#functions-lookup -->
171179

180+
<CodeTabs>
181+
172182
```ts
173183
const v = new TerraformVariable(this, "complex_object", {
174184
type: "object({users: list(object({name: string}))})",
@@ -181,7 +191,7 @@ new TerraformOutput(this, "first_user_name", {
181191

182192
```python
183193
v = TerraformVariable(self, "complex-object",
184-
type = 'object({users: list(object({name: string}))})',
194+
type='object({users: list(object({name: string}))})',
185195
)
186196
TerraformOutput(self, 'users',
187197
value=Fn.lookup(v.string_value, "users")
@@ -230,6 +240,8 @@ cdktf.NewTerraformOutput(stack, jsii.String("first-user-name"), &cdktf.Terraform
230240
})
231241
```
232242

243+
</CodeTabs>
244+
233245
### Raw string helper
234246

235247
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 +252,8 @@ Another helper function offered by CDKTF is `Fn.rawString` which can be used to
240252
<!-- #NEXT_CODE_BLOCK_SOURCE:csharp examples/csharp/documentation#functions-raw-string -->
241253
<!-- #NEXT_CODE_BLOCK_SOURCE:go examples/go/documentation#functions-raw-string -->
242254

255+
<CodeTabs>
256+
243257
```ts
244258
new TerraformOutput(this, "quotes", {
245259
value: Fn.rawString(`"b"`),
@@ -287,6 +301,8 @@ cdktf.NewTerraformOutput(stack, jsii.String("template"), &cdktf.TerraformOutputC
287301
})
288302
```
289303

304+
</CodeTabs>
305+
290306
## Operators
291307

292308
Use the `Op` object to include operators like `!`, `+`, and `-`.
@@ -298,7 +314,8 @@ Use the `Op` object to include operators like `!`, `+`, and `-`.
298314
<CodeTabs>
299315

300316
```ts
301-
import { Fn, TerraformOutput } from "cdktf";
317+
import { Fn } from "cdktf";
318+
import { TerraformOutput } from "cdktf";
302319
import { Op } from "cdktf";
303320

304321
const zones = new DataAwsAvailabilityZones(this, "zones", {
@@ -360,7 +377,8 @@ It is also possible to use all built-in Terraform functions without using CDKTF'
360377
<CodeTabs>
361378

362379
```ts
363-
import { Fn, TerraformOutput } from "cdktf";
380+
import { Fn } from "cdktf";
381+
import { TerraformOutput } from "cdktf";
364382
import { Op } from "cdktf";
365383
import { DataAwsAvailabilityZones } from "@cdktf/provider-aws/lib/data-aws-availability-zones";
366384

website/docs/cdktf/concepts/tokens.mdx

Lines changed: 49 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/state/workspaces#current-workspace-interpolation).
2525

2626
### Example
2727

@@ -216,3 +216,51 @@ 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+
<!-- #NEXT_CODE_BLOCK_SOURCE:python examples/python/documentation#functions-conditional -->
230+
<!-- #NEXT_CODE_BLOCK_SOURCE:ts examples/typescript/documentation#functions-conditional -->
231+
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',
241+
)
242+
```
243+
244+
```ts
245+
import { Token } from "cdktf";
246+
import { Instance } from "@cdktf/provider-aws/lib/instance";
247+
import { Fn } from "cdktf";
248+
import { Op } from "cdktf";
249+
new Instance(this, "web", {
250+
ami: "ami-2757f631",
251+
count: Token.asNumber(
252+
Fn.conditional(Op.eq(Token.asAny("terraform.workspace"), "prod"), 2, 1),
253+
),
254+
instanceType: "t2.micro",
255+
});
256+
```
257+
258+
```terraform
259+
resource "aws_instance" "web" {
260+
ami = "ami-2757f631"
261+
count = (terraform.workspace == "prod") ? 2 : 1
262+
instance_type = "t2.micro"
263+
}
264+
```
265+
266+
</CodeTabs>

0 commit comments

Comments
 (0)