terraform-cdk icon indicating copy to clipboard operation
terraform-cdk copied to clipboard

Google provider basic compute_instance creation fails

Open madeddie opened this issue 3 years ago • 7 comments

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

cdktf & Language Versions

cdktf 0.5.0 installed with Homebrew python 3.9.5 google@~> 3.0 with cdktf get Aug 24

Affected Resource(s)

google.ComputeInstance

Expected Behavior

Using the example from https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/getting_started

resource "google_compute_instance" "vm_instance" {
  name         = "terraform-instance"
  machine_type = "f1-micro"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    # A default network is created for all GCP projects
    network = "default"
    access_config {
    }
  }
}

I tried to create a basic compute instance.

cdktf convert --language python generates:

google.ComputeInstance(self, "vm_instance",
    boot_disk=[{
        "initialize_params": [{
            "image": "debian-cloud/debian-9"
        }
        ]
    }
    ],
    machine_type="f1-micro",
    name="terraform-instance",
    network_interface=[{
        "access_config": [{}],
        "network": "default"
    }
    ]
)

Which looks fine. I was expecting cdktf synth and deploy to create this instance.

Actual Behavior

cdktf synth generated snippet of the compute instance resource:

  "resource": {
    "google_compute_instance": {
      "vm_instance": {
        "machine_type": "f1-micro",
        "name": "terraform-instance",
        "boot_disk": [
          {}
        ],
        "network_interface": [
          {
            "network": "default"
          }
        ],
        "//": {
          "metadata": {
            "path": "infra/vm_instance",
            "uniqueId": "vm_instance",
            "stackTrace": [
              "new TerraformElement (/private/var/folders/pv/4vf3cvx91q3gqkr23xgfz0tr0000gn/T/jsii-kernel-1NziaX/node_modules/cdktf/lib/terraform-element.js:21:28)",
              "new TerraformResource (/private/var/folders/pv/4vf3cvx91q3gqkr23xgfz0tr0000gn/T/jsii-kernel-1NziaX/node_modules/cdktf/lib/terraform-resource.js:17:9)",
              "new ComputeInstance (/private/var/folders/pv/4vf3cvx91q3gqkr23xgfz0tr0000gn/T/jsii-kernel-1NziaX/node_modules/google/providers/google/compute-instance.js:194:9)",
              "/private/var/folders/pv/4vf3cvx91q3gqkr23xgfz0tr0000gn/T/tmpaodynvxk/lib/program.js:8367:58",
              "Kernel._wrapSandboxCode (/private/var/folders/pv/4vf3cvx91q3gqkr23xgfz0tr0000gn/T/tmpaodynvxk/lib/program.js:8795:24)",
              "Kernel._create (/private/var/folders/pv/4vf3cvx91q3gqkr23xgfz0tr0000gn/T/tmpaodynvxk/lib/program.js:8367:34)",
              "Kernel.create (/private/var/folders/pv/4vf3cvx91q3gqkr23xgfz0tr0000gn/T/tmpaodynvxk/lib/program.js:8108:29)",
              "KernelHost.processRequest (/private/var/folders/pv/4vf3cvx91q3gqkr23xgfz0tr0000gn/T/tmpaodynvxk/lib/program.js:9692:36)",
              "KernelHost.run (/private/var/folders/pv/4vf3cvx91q3gqkr23xgfz0tr0000gn/T/tmpaodynvxk/lib/program.js:9655:22)",
              "Immediate._onImmediate (/private/var/folders/pv/4vf3cvx91q3gqkr23xgfz0tr0000gn/T/tmpaodynvxk/lib/program.js:9656:46)",
              "processImmediate (node:internal/timers:464:21)"
            ]
          }
        }
      }
    }
  }

which is missing the initialize_params part of bootdisk.

Steps to Reproduce

As described in expected behavior.

madeddie avatar Aug 26 '21 17:08 madeddie

@madeddie thank you for reporting this! If this you're blocked on this before we're able to fix it, one option is to use the escape hatch to fix the generated config.

danieldreier avatar Aug 27 '21 21:08 danieldreier

@danieldreier thank you, will do. I actually tested it with typescript and there's no problem there, so I'm guessing it could be something with the 2 levels of Sequence that happens. Will try to debug myself, although I find jsii quite magically confusing :)

madeddie avatar Aug 28 '21 01:08 madeddie

I suspect this may be related to https://github.com/aws/jsii/issues/1919. Could you try explicitly specifying type names where the properties are being omitted?

jsteinich avatar Aug 28 '21 04:08 jsteinich

@jsteinich yeah, replacing the boot_disk definition with

boot_disk=[ComputeInstanceBootDisk(
    initialize_params=[ComputeInstanceBootDiskInitializeParams(
        image="debian-cloud/debian-9"
    )]
)],

fixes it. terraform output then looks like expected:

        "boot_disk": [
          {
            "initialize_params": [
              {
                "image": "debian-cloud/debian-9"
              }
            ]
          }
        ],

madeddie avatar Aug 28 '21 17:08 madeddie

I guess this should be closed since it's an issue with jsii

madeddie avatar Aug 28 '21 21:08 madeddie

I guess this should be closed since it's an issue with jsii

Likely, but we might be able to adjust the intermediate typescript code in a way that generates the working python code.

jsteinich avatar Aug 30 '21 13:08 jsteinich

I've removed this from the 0.7 milestone because that release is super full. I'm hoping we can revisit it in 0.8.

danieldreier avatar Sep 14 '21 21:09 danieldreier

This seems to be fixed, I tried it in 0.20.1, convert gave me

from constructs import Construct
#
# Provider bindings are generated by running `cdktf get`.
# See https://cdk.tf/provider-generation for more details.
#
from imports.google.compute_instance import ComputeInstance
class MyConvertedCode(Construct):
    def __init__(self, scope, name):
        super().__init__(scope, name)
        ComputeInstance(self, "vm_instance",
            boot_disk={
                "initialize_params": {
                    "image": "debian-cloud/debian-9"
                }
            },
            machine_type="f1-micro",
            name="terraform-instance",
            network_interface=[{
                "access_config": [{}],
                "network": "default"
            }
            ]
        )

I added the provider definition and ran synth and got

{
  "resource": {
    "google_compute_instance": {
      "vm_instance": {
        "//": {
          "metadata": {
            "path": "tmp.678jvplfay/vm_instance",
            "uniqueId": "vm_instance"
          }
        },
        "boot_disk": {
          "initialize_params": {
            "image": "debian-cloud/debian-9"
          }
        },
        "machine_type": "f1-micro",
        "name": "terraform-instance",
        "network_interface": [
          {
            "network": "default"
          }
        ]
      }
    }
  }
}

Which seems correct to me :)

DanielMSchmidt avatar Jan 17 '24 13:01 DanielMSchmidt

I'm going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

github-actions[bot] avatar Feb 17 '24 01:02 github-actions[bot]