Terraform & LXD

Hello,
I have the following piece of code to create LXD container:

terraform {
  required_providers {
    lxd = {
      source = "terraform-lxd/lxd"
    }
  }
}

provider "lxd" {
  generate_client_certificates = true
  accept_remote_certificate    = true
}

resource "lxd_container" "myvm" {
  name      = "myvm"
  image     = "images:ubuntu/jammy"
  ephemeral = false

  config = {
    "boot.autostart" = true
  }

  limits = {
    cpu    = 1
    memory = "8GB"
  }

  provisioner "remote-exec" {
    inline = [
      "touch /tmp/test"
    ]
  }
}

But the remote-exec provisioner doesn’t work. Adding the following:

    connection {
      private_key = file("/home/user1/.ssh/id_rsa")
      #type     = "ssh"
      #user     = "root"
      host     = self.ipv4_address
    }

doesn’t help.
Any ideas what should I add to make the remote-exec provisioner working with LXD ?

Hi @przemolb,

I think it would help if you would describe exactly what happens when you try the two configurations you shared – both the original one and how the result changes when you add the connection block you showed.

It’s best to show the full output of Terraform exactly as Terraform printed it, without any summarization, because others who might help here can then match the result with their own experience to hopefully determine what’s going wrong.


Please note also that provisioners are a last resort. It may be better to remove the provisioner and try a different strategy instead.

The Terraform documentation recommends provisioning using cloud-init. The documentation doesn’t talk directly about LXD but you can hopefully combine that guide with LXD’s documentation on using cloud-init to achieve a working solution where the actions get carried out without any need for Terraform itself to log in to the new server.

Hi @apparentlymart ,

thanks for the suggestion on cloud-init - I think I will give up on provisioners and try cloud-init.