D. tonumber() cannot convert a set directly; it needs a single string or number
Solution
Step 1: Analyze the variable type
Variable "ids" is a set of strings, created by toset(["1", "2", "3"]).
Step 2: Check usage of tonumber()
The function tonumber() expects a single string or number, not a set. Passing a set causes an error.
Final Answer:
tonumber() cannot convert a set directly; it needs a single string or number -> Option D
Quick Check:
tonumber(set) = error [OK]
Hint: tonumber() works on single values, not collections [OK]
Common Mistakes:
Trying to convert whole sets with tonumber()
Confusing toset() behavior
Assuming output block syntax is wrong
5. You have a list of numbers as strings: ["10", "20", "30"]. You want to convert it to a set of numbers in Terraform. Which expression correctly does this?
hard
A. tolist(toset(["10", "20", "30"]))
B. toset(tolist(tonumber(["10", "20", "30"])))
C. toset([for v in ["10", "20", "30"] : tonumber(v)])
D. tonumber(toset(["10", "20", "30"]))
Solution
Step 1: Understand the input and desired output
The input is a list of strings representing numbers. The goal is to get a set of numbers.
Step 2: Use a for-expression to convert each string to number
Using [for v in list : tonumber(v)] converts each string to a number, producing a list of numbers.
Step 3: Convert the resulting list to a set
Wrapping the for-expression with toset() converts the list of numbers to a set.
Final Answer:
toset([for v in ["10", "20", "30"] : tonumber(v)]) -> Option C
Quick Check:
Convert strings to numbers then to set [OK]
Hint: Use for-expression with tonumber() inside toset() [OK]
Common Mistakes:
Trying to apply tonumber() directly to a list
Converting to list after tonumber() without iteration
Using tonumber() on a set directly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.