Deploying Mistral-7B on a Nebius L40S GPU with Terraform and vLLM

Spinning up an on-demand GPU instance for a Retrieval-Augmented Generation (RAG) Proof of Concept sounds straightforward, until regional hardware constraints, cloud-init race conditions, and Docker runtime quirks get in the way.
Because GPU instances are billed by the minute, leaving an L40S idle wastes budget—while keeping Terraform state on a single laptop prevents clean automation from CI/CD pipelines.

This post walks through an automated, repeatable Terraform deployment on Nebius AI Cloud (eu-north1) that provisions the network, SSH access, and an L40S GPU VM (gpu-l40s-a), then bootstraps Docker, the NVIDIA Container Toolkit, and vllm serving mistralai/Mistral-7B-Instruct-v0.3 behind an OpenAI-compatible API on port 8000.


Architecture Overview

The deployment runs identically from a local workstation or via CI against a shared AWS S3 state backend:

  1. Networking & Access: Creates a dedicated VPC network, a 10.0.0.0/24 private subnet (explicitly avoiding 172.17.0.0/16 to prevent Docker default bridge conflicts), and an Ed25519 SSH keypair generated at terraform plan time.
  2. GPU Compute: Provisions a gpu-l40s-a VM (1gpu-16vcpu-64gb) with a 100 GB NVMe SSD boot disk running ubuntu24.04-cuda12 and an attached public IP.
  3. Bootstrap via cloud-init: Sequentially creates the ubuntu sudo user, installs docker.io, configures nvidia-container-toolkit, and launches the vllm_server container.
  4. Remote State & CI/CD: Stores Terraform state in an AWS S3 bucket supplied at init time via -backend-config, allowing both CLI runs and GitHub Actions (plan, apply, and destroy via workflow_dispatch) to manage the exact same stack.

Prerequisites and State Configuration

Before initializing Terraform, ensure the following are in place:

  • Terraform >= 1.5 and the nebius/nebius provider (>= 0.6.8).
  • Nebius Credentials: A project ID (project-...) scoped to eu-north1 and a Service Account with an authorized keypair.
  • AWS S3 State Bucket: An S3 bucket provisioned separately to hold the .tfstate file, along with IAM credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION).

Unlike gated models such as meta-llama/Meta-Llama-3-8B-Instruct—which require accepting a license agreement on HuggingFace and passing HUGGING_FACE_HUB_TOKEN into the container—mistralai/Mistral-7B-Instruct-v0.3 is licensed under Apache 2.0 and pulls directly without authentication.

To keep account-specific bucket names out of the repository, main.tf defines only the static S3 backend parameters while the bucket name is injected via environment variables at init time:

export TF_VAR_nebius_sa_id="serviceaccount-..."
export TF_VAR_nebius_public_key_id="publickey-..."
export TF_VAR_nebius_private_key_path="/path/to/private.pem"
export TF_VAR_parent_id="project-..."

export TF_STATE_BUCKET="nebius-rag-poc-state"
export AWS_ACCESS_KEY_ID="..."
export AWS_SECRET_ACCESS_KEY="..."
export AWS_DEFAULT_REGION="eu-central-1"

# First-time migration from local state to S3:
terraform init -backend-config="bucket=$TF_STATE_BUCKET" -migrate-state

# Standard initialization and deployment:
terraform init -backend-config="bucket=$TF_STATE_BUCKET" -reconfigure
terraform plan -out=poc.plan
terraform apply poc.plan

First-Boot Provisioning (cloud-init)

The cloud_init_user_data block executes sequentially on initial boot. Because cloud-init does not parallelize runcmd steps, Docker installation and NVIDIA runtime registration complete cleanly before the vLLM container starts:

#cloud-config
users:
  - name: ubuntu
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: /bin/bash
    ssh_authorized_keys:
      - <public_key_openssh from tls_private_key>

runcmd:
  - apt-get update
  - apt-get install -y docker.io python3-pip git
  - systemctl enable --now docker
  - usermod -aG docker ubuntu
  - curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
  - curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/amd64/libnvidia-container.list | sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' > /etc/apt/sources.list.d/nvidia-container-toolkit.list
  - apt-get update
  - apt-get install -y nvidia-container-toolkit
  - nvidia-ctk runtime configure --runtime=docker
  - systemctl restart docker
  - |
    docker run -d \
      --runtime=nvidia \
      --restart always \
      --name vllm_server \
      -p 8000:8000 \
      --ipc=host \
      vllm/vllm-openai:latest \
      --model mistralai/Mistral-7B-Instruct-v0.3 \
      --gpu-memory-utilization 0.90 \
      --max-model-len 8192

Key vLLM Runtime Parameters

Flag Value Purpose
--runtime=nvidia — Routes GPU access through the registered NVIDIA Container Toolkit runtime without hanging.
--ipc=host — Shares the host IPC namespace, required by vLLM/PyTorch for CUDA tensor operations.
--gpu-memory-utilization 0.90 Allocates 90% of L40S VRAM (48 GB) to model weights and the KV cache.
--max-model-len 8192 Caps context length at 8,192 tokens for RAG workloads, balancing context size against concurrent batch capacity.

Cold-Start Timeline and Verification

On a fresh deployment, expect roughly 5 to 7 minutes before port 8000 responds:

  1. VM Provisioning & OS Packages (~2 min): Nebius allocates the instance (~50s), installs docker.io (~30s), and configures nvidia-container-toolkit (~30s).
  2. Container & Weight Download (~5.5 min): Docker pulls vllm/vllm-openai:latest (~5 GB compressed, ~90s), and vLLM downloads the Mistral-7B-Instruct-v0.3 weights from HuggingFace (~14 GB, ~240s).
  3. CUDA Graph Capture (~30s): Weights load into VRAM and the OpenAI-compatible HTTP server binds to port 8000.

Subsequent container restarts (docker restart vllm_server) reuse the cached image and weights inside the container filesystem and complete in under a minute.

Once terraform apply finishes, retrieve the SSH command from outputs and verify the service:

terraform output ssh_command
ssh -i nebius_rag_id_ed25519 ubuntu@<PUBLIC_IP>

# Confirm container status and watch startup logs
docker ps
docker logs -f vllm_server

# Verify loaded model endpoint
curl -s http://localhost:8000/v1/models | jq .

# Run a smoke-test chat completion
curl -s http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mistralai/Mistral-7B-Instruct-v0.3",
    "messages": [{"role": "user", "content": "Say hello"}]
  }' | jq .

CI/CD Automation and Lifecycle Teardown

For hands-off lifecycle management, .github/workflows/terraform.yml runs terraform plan automatically on pushes and pull requests to main, and exposes a manual workflow_dispatch trigger with a command selector (plan, apply, or destroy). When apply runs in GitHub Actions, the generated Ed25519 private key is uploaded as a build artifact (ssh-access) so you can connect immediately.

Because GPU VMs accrue charges continuously while active, destroy the environment as soon as testing completes:

terraform destroy

Running terraform destroy (or triggering destroy in GitHub Actions) tears down the VM, boot disk, subnet, VPC network, and local key file while leaving your external AWS S3 state bucket intact.


Production Gotchas and Lessons Learned

  • Regional Hardware Availability: gpu-l40s-* instances are only available in eu-north1. Attempting to schedule them in eu-west1 fails at apply time, while NotEnoughResources errors indicate transient pool capacity rather than a quota block.
  • Use ubuntu24.04-cuda12: The ubuntu22.04-driverless image is deprecated and ships without host NVIDIA drivers; ubuntu24.04-cuda12 provides working nvidia-smi immediately on boot.
  • Use --runtime=nvidia, Not --gpus all: Once nvidia-ctk runtime configure registers the nvidia runtime in /etc/docker/daemon.json, passing --gpus all causes docker run to hang.
  • Pin the NVIDIA Apt Repository: Hardcode stable/deb/amd64 in the apt source URL. Relying on $ID$VERSION_ID variable expansion can return a 404 error page and silently write HTML into /etc/apt/sources.list.d/nvidia-container-toolkit.list.
  • Nebius Subnet Schema & IP Outputs: Define ipv4_private_pools with an explicit cidrs list and use_network_pools = false (a bare cidr attribute creates a subnet with zero usable IPs), and wrap the public IP output in cidrhost() to strip the trailing /32 mask.
  • SSH Key Format & Ownership: Export tls_private_key.private_key_openssh rather than private_key_pem (OpenSSH rejects PKCS#8-encoded Ed25519 keys), and ensure the user running ssh owns the key file with chmod 600.

See the code here: Terraform Code and README.md