Operators
You learned how to reference component exports to connect your pipelines in the previous topic. Now you’ll learn how to use operators to manipulate, transform, and combine values in your expressions.
Operators are essential for creating dynamic configurations that can:
- Calculate values: Perform arithmetic operations on component exports and literals.
- Compare data: Make decisions based on component states or configuration values.
- Combine strings: Build dynamic identifiers, paths, or configuration strings.
- Access nested data: Extract values from complex objects and arrays.
The component controller evaluates all operators following the standard PEMDAS order of mathematical operations.
Arithmetic operators
String operators
String concatenation is particularly useful for building dynamic values from component exports:
prometheus.scrape "app" {
targets = [{ "__address__" = local.file.host.content + ":" + local.file.port.content }]
job_name = "app-" + sys.env("ENVIRONMENT")
}Comparison operators
You can use the equality operators == and != with any operands.
The operands in ordering operators <, <=, >, and >= must be orderable and of the same type.
The results of these comparisons are:
- Boolean values are equal if both are either
trueorfalse. - Numerical (integer and floating-point) values follow the usual ordering.
- String values follow lexical ordering, byte-wise.
- Objects are equal if all their fields are equal.
- Arrays are equal if their corresponding elements are equal.
Comparison operators are commonly used for conditional configuration:
// Check if environment matches
remote_write_enabled = sys.env("ENVIRONMENT") == "production"Logical operators
Logical operators work with boolean values and return a boolean result.
Assignment operator
The Alloy configuration syntax uses = as the assignment operator.
An assignment statement can only assign a single value. Each value must be assignable to the attribute or object key:
- You can assign
nullto any attribute. - You can assign numerical, string, boolean, array, function, capsule, and object types to attributes of the corresponding type.
- You can assign numbers to string attributes with an implicit conversion.
- You can assign strings to numerical attributes if they represent a number.
- You can’t assign blocks.
The component controller performs type checking during evaluation to ensure assignments are valid before configuring components.
Brackets
For example, the following code uses curly braces and square brackets to define an object and an array.
obj = { app = "alloy", namespace = "dev" }
arr = [1, true, 7 * (1+1), 3]Access operators
You can use the Alloy access operators to retrieve nested values. Use square brackets to access zero-indexed array elements or object fields by enclosing the field name in double quotes. Use the dot operator to access object fields without double quotes or component exports.
// Access array elements and object fields
obj["app"]
arr[1]
// Access object fields and component exports
obj.app
local.file.token.content
discovery.kubernetes.endpoints.targets[0]["__address__"]
// Build complex expressions with nested access
prometheus.scrape "dynamic" {
targets = discovery.kubernetes.endpoints.targets
job_name = discovery.kubernetes.endpoints.targets[0]["job"] + "-scraper"
}If you use the [ ] operator to access a non-existent object member, the result is null.
If you use the . operator to access a non-existent named member of an object, an error occurs.
Next steps
Now that you understand operators, continue learning about expressions:
- Types and values - Learn how the type system works with operators to ensure expressions evaluate correctly
- Function calls - Use operators inside function calls and with function return values
For building complete configurations:
- Component exports - Combine operators with component references to create dynamic pipelines



