Dynamic Eval
Dynamic blocks with mantis.core.Eval
The mantis.core.Eval task in Mantis provides a powerful way to dynamically generate and manipulate configurations, similar to Terraform's dynamic blocks. This task allows you to use CUE expressions to create flexible and reusable infrastructure code.
Overview
The mantis.core.Eval task evaluates CUE expressions at runtime, enabling you to:
- Generate dynamic configurations based on input variables
- Perform complex calculations and transformations
- Create reusable logic for infrastructure deployment
Task Structure
task_name: {
@task(mantis.core.Eval)
dep: [...] // Dependencies on other tasks
cueexpr: """
// CUE expression goes here
"""
exports: [{
var: "output_variable_name"
jqpath: ".result_path"
}]
}
Key Components
1. Task Annotation: Use the @task(mantis.core.Eval) annotation to specify that this is an evaluation task.
2. CUE Expression: The cueexpr field contains the CUE expression to be evaluated. This is where you define your dynamic logic.
3. Exports: Use the exports field to specify which values from the evaluation result should be exported for use in other tasks.
exports: [{
var: "output_variable_name" // Store the value specified by jqpath in this variable
jqpath: ".result_path" // Use jqpath to extract the value from the result
}]
4. Using Variables: You can reference variables from other tasks using the @var tag within your CUE expression:
some_value: string @var(variable_from_another_task)
Example Usage
Here's an example of using mantis.core.Eval to dynamically generate subnet configurations:
generate_subnet_configs: {
@task(mantis.core.Eval)
dep: [create_vpc, get_azs]
cueexpr: """
import "mantis"
vpc_id: string @var(vpc_id)
vpc_cidr: string @var(vpc_cidr_block)
az_names: [...string] @var(az_names)
result: {
subnet_az1: {
vpc_id: vpc_id
cidr_block: mantis.CidrSubnet(vpc_cidr, 8, 1)
availability_zone: az_names[0]
tags: {
Name: "mantis-subnet-az1"
}
}
subnet_az2: {
vpc_id: vpc_id
cidr_block: mantis.CidrSubnet(vpc_cidr, 8, 2)
availability_zone: az_names[1]
tags: {
Name: "mantis-subnet-az2"
}
}
}
"""
exports: [{
var: "subnet_configs"
jqpath: ".result"
}]
}
In this example, the task:
- Depends on create_vpc and get_azs tasks
- Uses variables from other tasks (vpc_id, vpc_cidr_block, az_names)
- Generates subnet configurations dynamically
- Exports the result as subnet_configs
Best Practices
- Keep expressions modular and focused on a single purpose
- Use meaningful variable names for clarity
- Leverage CUE's type system for validation within expressions
- Comment complex logic for better maintainability
- Use built-in functions like mantis.CidrSubnet for common operations
Comparison with Terraform
While similar to Terraform's dynamic blocks, mantis.core.Eval offers several advantages:
1. More flexible: Can generate any valid CUE structure, not just nested blocks
2. Powerful: Access to CUE's full range of built-in functions and operations like field and list comprehensions.
3. Reusable: Can be easily parameterized and shared across projects
By leveraging the mantis.core.Eval task, you can create highly dynamic and reusable infrastructure code in Mantis, surpassing the capabilities of traditional Terraform configurations.