# Terraform & LXD

**URL:** <https://discuss.hashicorp.com/t/terraform-lxd/45103>\
**Category:** Terraform Providers\
**Created:** [October 3, 2022, 4:28pm UTC](https://discuss.hashicorp.com/t/terraform-lxd/45103 "2022-10-03T16:28:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![przemolb](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/przemolb/32/1198_2.png) [@przemolb](https://discuss.hashicorp.com/u/przemolb)\
**Post date:** [October 3, 2022, 4:28pm UTC](https://discuss.hashicorp.com/t/terraform-lxd/45103/1 "2022-10-03T16:28:28Z")

</div>

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

```auto
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:

```auto
    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 ?

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [October 3, 2022, 11:42pm UTC](https://discuss.hashicorp.com/t/terraform-lxd/45103/2 "2022-10-03T23:42:31Z")

</div>

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](https://www.terraform.io/language/resources/provisioners/syntax#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](https://learn.hashicorp.com/tutorials/terraform/cloud-init?in=terraform/provision). The documentation doesn’t talk directly about LXD but you can hopefully combine that guide with [LXD’s documentation on using cloud-init](https://linuxcontainers.org/lxd/docs/master/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.

---

<div class="post-metadata">

**Author:** ![przemolb](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/przemolb/32/1198_2.png) [@przemolb](https://discuss.hashicorp.com/u/przemolb)\
**Post date:** [October 4, 2022, 9:13am UTC](https://discuss.hashicorp.com/t/terraform-lxd/45103/3 "2022-10-04T09:13:05Z")

</div>

Hi @apparentlymart ,

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