Skip to main content

Terraform quickstart

Prerequisites
  • You should have completed the prerequisites and provider configuration in the Terraform overview.

To actually run a VM on ECI, you need two resources:

  1. eci_virtual_machine: defines the VM (specs, credentials)
  2. eci_virtual_machine_allocation: allocates it to a host and starts it

1. Full example

A single file is enough to bring up a VM end-to-end, including network, disk, NIC, and public IP.

# main.tf

# 1) Data sources: instance type, pricing, image
data "eci_instance_type" "m8" {
name = "M-8" # Memory-optimized, 8 vCore
}

data "eci_pricing" "vm_pricing" {
name = "M-8"
pricing_type = "ondemand"
}

data "eci_pricing" "storage_pricing" {
name = "Block Storage"
pricing_type = "ondemand"
}

data "eci_pricing" "ip_pricing" {
name = "Public IP"
pricing_type = "ondemand"
}

data "eci_block_storage_image" "ubuntu" {
name = "Ubuntu 22.04 LTS (20250116)"
}

# 2) Network: virtual network + subnet
resource "eci_virtual_network" "vnet" {
name = "tf-vnet"
network_cidr = "192.168.0.0/16"
tags = { managed-by = "terraform" }
}

resource "eci_subnet" "subnet" {
attached_network_id = eci_virtual_network.vnet.id
name = "tf-subnet"
purpose = "virtual_machine"
network_gw = "192.168.0.1/24"
tags = { managed-by = "terraform" }
}

# 3) VM definition
resource "eci_virtual_machine" "vm" {
name = "tf-vm-01"
instance_type_id = data.eci_instance_type.m8.id
pricing_id = data.eci_pricing.vm_pricing.id
username = "elice"
password = "ChangeMe-Strong-Pa$$w0rd!"
on_init_script = "#!/bin/bash\necho 'Hello' > /home/elice/hello.txt"
always_on = false
dr = false
tags = { managed-by = "terraform" }
}

# 4) Block storage (OS disk)
resource "eci_block_storage" "boot_disk" {
attached_machine_id = eci_virtual_machine.vm.id
name = "tf-vm-01-boot"
size_gib = 100
pricing_id = data.eci_pricing.storage_pricing.id
image_id = data.eci_block_storage_image.ubuntu.id # OS boot image
dr = false
tags = { managed-by = "terraform" }
}

# 5) Network interface
resource "eci_network_interface" "ni" {
attached_subnet_id = eci_subnet.subnet.id
attached_machine_id = eci_virtual_machine.vm.id
name = "tf-vm-01-ni"
dr = false
tags = { managed-by = "terraform" }
}

# 6) Public IP (optional)
resource "eci_public_ip" "ip" {
attached_network_interface_id = eci_network_interface.ni.id
pricing_id = data.eci_pricing.ip_pricing.id
dr = false
tags = { managed-by = "terraform" }
}

# 7) Allocation: the resource that actually runs the VM
resource "eci_virtual_machine_allocation" "run" {
machine_id = eci_virtual_machine.vm.id
tags = { managed-by = "terraform" }

depends_on = [
eci_block_storage.boot_disk,
eci_network_interface.ni,
]
}

output "public_ip" {
value = eci_public_ip.ip.ip
}

2. Basic workflow

# Init (download providers)
terraform init

# Preview changes
terraform plan

# Apply
terraform apply

# Stop only the VM (deleting just the allocation keeps the VM and disk)
terraform destroy -target=eci_virtual_machine_allocation.run

# Destroy everything
terraform destroy
Stop vs delete
  • Stop the VM: remove only the eci_virtual_machine_allocation resource. The VM, disk, NIC, and public IP remain, and storage and IP charges continue.
  • Delete the VM: also remove eci_virtual_machine. Include any attached resources in the destroy target.

Next steps