lxc / terraform-provider-incus

Incus provider for Terraform/OpenTofu
https://linuxcontainers.org/incus
Mozilla Public License 2.0
34 stars 8 forks source link

Bug: terraform-provider-incus provider fails to create the instance in the project not equal to "default" if image fingerprint assigned to `image` attribute instead of `image name #94

Open tregubovav-dev opened 1 week ago

tregubovav-dev commented 1 week ago

Issue

[Description was rewritten on July 7, 2024 after addition investigation] terraform-provider-incus provider fails to create the instance in the project not equal to "default" if image fingerprint assigned to image attribute instead of image name. Instance creation fails in this case with message like below:

incus_instance.test: Creating...
╷
│ Error: Failed to retrieve image info for instance "test"
│
│   with incus_instance.test,
│   on install_via_image.tf line 20, in resource "incus_instance" "test":
│   20: resource "incus_instance" "test" {
│
│ Image not found

Steps to reproduce

Failed configuration

terraform {
  required_providers {
    incus = {
      source = "lxc/incus"
      version = "0.1.2"
    }
  }
}

provider "incus" {
}

resource "incus_image" "alpine" {
    source_remote       = "images"
    source_image        = "alpine/3.18"
    aliases             = ["alpine-3.18"]
    project             = "test"
}

resource "incus_instance" "test" {
    name        = "test"
    image       = incus_image.alpine.fingerprint
    project     = "test"
}

Working configurations

terraform {
  required_providers {
    incus = {
      source = "lxc/incus"
      version = "0.1.2"
    }
  }
}

provider "incus" {
}

resource "incus_image" "alpine" {
    source_remote       = "images"
    source_image        = "alpine/3.18"
    aliases             = ["alpine-3.18"]
    project             = "default"
}

resource "incus_instance" "test" {
    name        = "test"
    image       = incus_image.alpine.fingerprint
    project     = "default"
}

OR

terraform {
  required_providers {
    incus = {
      source = "lxc/incus"
      version = "0.1.2"
    }
  }
}

provider "incus" {
}

resource "incus_image" "alpine" {
    source_remote       = "images"
    source_image        = "alpine/3.18"
    aliases             = ["alpine-3.18"]
}

resource "incus_instance" "test" {
    name        = "test"
    image       = incus_image.alpine.fingerprint
}

incus_image documentation shows how to use 'incus_instance' with 'incus_image` resource. However it does not work.

I also tried to assign short or long hardcoded 'fingerprint' value of existing image getting by command incus image list --project test with the same result.

terraform-provider-lxd has the same issue.

tregubovav-dev commented 2 days ago

Below is a workaround:

Modifying image attribute in incus_instance resource from image = incus_image.alpine.fingerprint to image = format("${incus_cached_image.alpine.source_remote}:${incus_cached_image.alpine.fingerprint}") makes the configuration work. But, it's still not clear why image remote is required to create instance from cached image.

tregubovav-dev commented 1 day ago

The LXD team fixed and closed the same bug in terraform-provider-lxd already. I would suggest to merge the same fix to the terraform-provider-incus