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.

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
}
}
FieldTypeRequiredDescription
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]stringEnvironment variables to set when running Terraform
variablesanyVariables to pass to Terraform

Network configuration for the Terraform container.

FieldTypeRequiredDescription
idstringID of the network to attach to (in reference format)
ip_addressstringStatic IP address to assign
aliases[]stringNetwork aliases for the container

Volume mounts for the Terraform container.

FieldTypeRequiredDescription
sourcestringSource path on the host (relative paths are relative to the HCL file)
destinationstringDestination path inside the container (must be absolute)
typestringVolume type: “bind”, “volume”, or “tmpfs” (defaults to “bind”)
read_onlyboolWhether the volume should be read-only
bind_propagationstringBind propagation mode: “shared”, “private”, “slave”, “rslave”, “rprivate”
bind_propagation_non_recursiveboolWhether to use non-recursive bind mounting
selinux_relabelstringSELinux relabeling mode: “shared” or “private”

The following fields are computed at runtime and can be referenced by other resources:

FieldTypeDescription
meta.idstringFull resource ID (e.g., resource.terraform.example)
meta.typestringResource type (terraform)
meta.namestringResource name
outputanyTerraform outputs defined in the configuration
apply_outputstringConsole output from the terraform apply command
source_checksumstringChecksum of the source directory
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
}
}