Skip to content

Terraform

The terraform resource allows you to provision infrastructure using Terraform configurations within your lab environment. It executes Terraform commands in a containerized environment and can pass variables and capture outputs for use by other resources.

As a lab author, you can use terraform resources to:

  • Lab Environment Setup: Provision cloud infrastructure required for your lab scenarios using Terraform configurations
  • Real Infrastructure Integration: Connect your lab to actual cloud resources when simulated environments aren’t sufficient
  • Dynamic Configuration: Generate configuration files for other lab resources based on Terraform-provisioned infrastructure

Terraform resources allow lab authors to integrate real cloud infrastructure provisioning into their lab setup workflows.

resource "terraform" "example" {
source = "./terraform"
}
resource "terraform" "example" {
source = "./terraform"
version = "1.9.8"
working_directory = "/terraform"
# Environment variables
environment = {
AWS_REGION = "us-east-1"
TF_LOG = "INFO"
}
# Terraform variables
variables = {
instance_count = 2
vpc_cidr = "10.0.0.0/16"
}
# Network configuration
network {
id = resource.network.main.meta.id
ip_address = "10.0.0.5"
aliases = ["terraform"]
}
# Volume mounts
volume {
source = "./terraform-modules"
destination = "/modules"
type = "bind"
read_only = true
}
}
FieldRequiredTypeDescription
sourcestringSource directory containing Terraform configuration files
versionstringVersion of Terraform to use. Defaults to “1.9.8”.
working_directorystringWorking directory to run terraform commands. Defaults to ”./“.
environmentmap(string)Environment variables to set when running Terraform. Defaults to empty map.
variablesanyVariables to pass to Terraform
networkblockNetwork attachments (repeatable)
volumeblockVolume mounts (repeatable)

terraform → network

Network configuration for the Terraform container (repeatable).

FieldRequiredTypeDescription
idreference to networkReference to network resource
ip_addressstringStatic IP address. Auto-assigned if not specified.
aliaseslist(string)Network aliases. Defaults to empty list.

terraform → volume

Volume mount configuration for the Terraform container (repeatable).

FieldRequiredTypeDescription
sourcestringSource path or volume name
destinationstringMount path inside container
typestringVolume type: “bind”, “volume”, or “tmpfs”. Defaults to “bind”.
read_onlyboolMount as read-only. Defaults to false.
bind_propagationstringBind propagation mode: “shared”, “private”, “slave”, “rslave”, “rprivate”
bind_propagation_non_recursiveboolUse non-recursive bind mounting. Defaults to false.
selinux_relabelstringSELinux relabeling mode: “shared” or “private”

These attributes are set by the system after Terraform execution:

Field Type Description
output any Terraform outputs defined in the configuration
apply_output string Console output from the terraform apply command
source_checksum string Checksum of the source directory
  • Source directory must exist and contain valid Terraform configuration files
  • Working directory paths are normalized and made relative to container root
  • Volume source paths are made absolute relative to config file location
  • Terraform version must be a valid version string
  • Network and volume configurations follow container resource validation rules
resource "terraform" "aws_vpc" {
source = "./infrastructure"
variables = {
region = "us-west-2"
environment = "lab"
}
}

Advanced Configuration with Custom Version

Section titled “Advanced Configuration with Custom Version”
resource "terraform" "kubernetes" {
source = "./k8s-terraform"
version = "1.8.0"
environment = {
KUBE_CONFIG_PATH = "/root/.kube/config"
}
variables = {
cluster_name = "lab-cluster"
node_count = 3
}
volume {
source = "./kubeconfig"
destination = "/root/.kube"
type = "bind"
}
}

Using Terraform Outputs in Other Resources

Section titled “Using Terraform Outputs in Other Resources”
resource "terraform" "infrastructure" {
source = "./terraform"
variables = {
vpc_cidr = "10.0.0.0/16"
}
}
## Reference Terraform outputs
resource "container" "app" {
image {
name = "myapp:latest"
}
environment = {
VPC_ID = resource.terraform.infrastructure.output.vpc_id
}
}
  1. State Management: Use remote state backends for production-like scenarios
  2. Version Pinning: Specify exact Terraform versions for consistency across environments
  3. Variable Organization: Use structured variable files and clear naming conventions
  4. Output Usage: Define meaningful outputs for integration with other lab resources
  5. Resource Tagging: Include proper tags for cost tracking and resource management
  6. Network Isolation: Use dedicated networks for multi-tier application demonstrations
  7. Volume Mounts: Mount configuration files and modules as read-only when possible
  8. Environment Variables: Use environment variables for sensitive configuration like API keys