Skip to main content

Variables

Variables in CUE provide powerful ways to manage configurations across different environments and inject dynamic information. This document covers advanced usage of variables, including conditional inclusion of files and system information injection.

Conditional Inclusion of CUE Files

CUE allows for conditional inclusion of files using build attributes. This feature provides flexibility in managing configurations across different environments or conditions. Here's how it works:

cue
@if(production)

cpu: 2
memory: 4096
disk: 1024M
// ... more configurations ...

In this example, the variables.cue file will only be included in the evaluation when the production tag is present. You can control which files are included in your evaluation by using the -t flag when running mantis commands: Bash

mantis run --init install_app.tf.cue -t production

This approach allows you to maintain different configurations for various environments (e.g., development, staging, production) in separate files and include them conditionally 1.

Injecting System Information

CUE provides a powerful feature to inject system information into your evaluations using tag variables. This can be particularly useful for including dynamic information like the current directory, operating system, or timestamp. Let's modify our variables.cue file to include some system information: variables.cue

package defs

project_name: "flask-dynamodb-app"
deployment_time: string @tag(deploy_time,var=now)
deployment_user: string @tag(deploy_user,var=username)

In this example, we've added two new fields: deployment_time: This will be set to the current time when the evaluation runs. deployment_user: This will be set to the username of the person running the evaluation. To include this system information in your evaluation, use the -T flag when running CUE commands:

mantis run -T

This will inject the current time and username into your configuration 2.

Combining Conditional Inclusion and System Information

You can combine these features to create powerful and flexible configurations. For example: variables.cue

@if(production)
package defs

project_name: "flask-dynamodb-app"
deployment_time: string @tag(deploy_time,var=now)
deployment_user: string @tag(deploy_user,var=username)
environment: "production"

@if(!production)
package defs

project_name: "flask-dynamodb-app-dev"
deployment_time: string @tag(deploy_time,var=now)
deployment_user: string @tag(deploy_user,var=username)
environment: "development"

Now you can switch between production and development configurations while still injecting system information:

mantis run -T -t production # For production
mantis run -T # For development (no production tag)

This approach allows you to maintain different configurations for various environments, include system information, and easily switch between them using tags and command-line flags.

Mantis Runtime Variables

Mantis provides a powerful mechanism for exporting variables from one task and using them in subsequent tasks within a flow. This feature allows for dynamic configuration and runtime injection of values.

Exporting Variables

Variables can be exported from a task using the exports field. Here's an example from the install_app.tf.cue file:

create_dynamodb_table: {
@task(mantis.core.TF)
// ... other configurations ...
exports: [{
jqpath: ".aws_dynamodb_table.hello_world_table.name"
var: "table_name"
}, {
jqpath: ".aws_dynamodb_table.hello_world_table.arn"
var: "table_arn"
}]
}

In this example, two variables are exported:

  • table_name: The name of the created DynamoDB table
  • table_arn: The ARN of the created DynamoDB table

These variables become available for use in subsequent tasks within the same flow.

Using Exported Variables for Runtime Injection

Exported variables can be injected into other configurations at runtime using the @var annotation. Here's an example from the deployment.cue file:

flaskDynamoDBDeployment: {
// ... other configurations ...
spec: {
template: {
spec: {
containers: [{
// ... other container configurations ...
env: [
// ... other environment variables ...
{
name: "DYNAMODB_TABLE_NAME"
value: string | *null @var(table_name)
}
]
}]
}
}
}
}

In this example, the DYNAMODB_TABLE_NAME environment variable is set using the table_name variable that was exported from the create_dynamodb_table task.

Benefits of Runtime Variable Injection

  1. Dynamic Configuration: Values created or determined during the execution of one task can be used to configure subsequent tasks.
  2. Reduced Hardcoding: Eliminates the need to hardcode values that may change between environments or deployments.
  3. Consistency: Ensures that the same values are used across different parts of your infrastructure and application deployment.
  4. Flexibility: Allows for more adaptable and reusable configurations that can work across different environments.

By leveraging Mantis runtime variables, you can create more dynamic, flexible, and maintainable infrastructure-as-code configurations.

Footnotes

  1. https://cuelang.org/docs/howto/conditionally-include-cue-files-using-build-attributes/

  2. https://cuelang.org/docs/howto/inject-system-information-into-evaluation-using-tag-variable/