"Failed to load config file" with lxd_container

Hello all,

(I am using Terraform v1.3.1)

it seems that I cannot use terraform’s file function while trying to read a file content:

$ cat z-1.tf; echo =======; cat provider.tf; echo =======; cat lxc-config.conf 
resource "lxd_container" "z-1" {
  name      = "z-1"
  image     = "images:ubuntu/22.04/amd64"
  ephemeral = false
  profiles  = ["default"]

  config = {
    "raw.lxc" = file("${path.module}/lxc-config.conf")
  }
}

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

=======
boot.autostart          = true
security.privileged     = true
security.nesting        = true
linux.kernel_modules    = "ip_tables,ip6_tables,netlink_diag,nf_nat,overlay,br_netfilter,loop"
lxc.kmsg                = 1
lxc.apparmor.profile    = unconfined
lxc.cap.drop            =
lxc.cgroup.devices.allow= a
lxc.mount.auto          = proc:rw sys:rw


$ terraform providers

Providers required by configuration:
.
└── provider[registry.terraform.io/terraform-lxd/lxd]

$ terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # lxd_container.z-1 will be created
  + resource "lxd_container" "z-1" {
      + config           = {
          + "raw.lxc" = <<-EOT
                boot.autostart          = true
                security.privileged     = true
                security.nesting        = true
                linux.kernel_modules    = "ip_tables,ip6_tables,netlink_diag,nf_nat,overlay,br_netfilter,loop"
                lxc.kmsg                = 1
                lxc.apparmor.profile    = unconfined
                lxc.cap.drop            =
                lxc.cgroup.devices.allow= a
                lxc.mount.auto          = proc:rw sys:rw
            EOT
        }
      + ephemeral        = false
      + id               = (known after apply)
      + image            = "images:ubuntu/22.04/amd64"
      + ip_address       = (known after apply)
      + ipv4_address     = (known after apply)
      + ipv6_address     = (known after apply)
      + mac_address      = (known after apply)
      + name             = "z-1"
      + privileged       = false
      + profiles         = [
          + "default",
        ]
      + start_container  = true
      + status           = (known after apply)
      + target           = (known after apply)
      + type             = (known after apply)
      + wait_for_network = true
    }

Plan: 1 to add, 0 to change, 0 to destroy.
╷
│ Warning: provider set empty string as default value for bool generate_client_certificates
│ 
│   with provider["registry.terraform.io/terraform-lxd/lxd"],
│   on <empty> line 0:
│   (source code not available)
│ 
╵
╷
│ Warning: provider set empty string as default value for bool accept_remote_certificate
│ 
│   with provider["registry.terraform.io/terraform-lxd/lxd"],
│   on <empty> line 0:
│   (source code not available)
│ 
╵

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

lxd_container.z-1: Creating...
╷
│ Warning: provider set empty string as default value for bool generate_client_certificates
│ 
│   with provider["registry.terraform.io/terraform-lxd/lxd"],
│   on <empty> line 0:
│   (source code not available)
│ 
╵
╷
│ Warning: provider set empty string as default value for bool accept_remote_certificate
│ 
│   with provider["registry.terraform.io/terraform-lxd/lxd"],
│   on <empty> line 0:
│   (source code not available)
│ 
╵
╷
│ Error: failed to start container (z-1): Failed to load config file "/tmp/lxd_config_1545272787": loading config file for the container failed
│ 
│   with lxd_container.z-1,
│   on z-1.tf line 1, in resource "lxd_container" "z-1":
│    1: resource "lxd_container" "z-1" {
│ 
╵

Can anybody help me with investigation why it failes ?

If you look at the filename it is mentioning (/tmp/lxd_config_1545272787) that isn’t the filename in your code. So presumably this is something internal to the provider. I’d imagine the failure could be due to something being incorrectly formatted.

Try to see if you can capture that file to see what is in it. Maybe the file you are loading needs to be in a different structure?

Please refer to the provider’s documentation. You will see you are not using the config argument in the way that it is documented.

Well, for some it does work:

I couldn’t find why it doesn’t work for me and that is why I asked here.

Can I print the content of the file in TF ?