Trouble Assigning Public IP to GCP Compute Instance with CDKTF in Python

69 views Asked by At

I am currently exploring CDKTF (Terraform's Cloud Development Kit) with Python to manage GCP resources, specifically to create a GCP compute instance with a public IP address. While I've successfully done this using HCL in Terraform, I'm encountering challenges in achieving the same with CDKTF (converted from HCL using cdktf convert).

Here's my CDKTF Python code:

from cdktf import Fn, Token, TerraformOutput, TerraformStack, App

from cdktf_cdktf_provider_google.provider import GoogleProvider
from cdktf_cdktf_provider_google.compute_instance import ComputeInstance
from cdktf_cdktf_provider_google.compute_firewall import ComputeFirewall
from cdktf_cdktf_provider_google.compute_address import ComputeAddress

class MyConvertedCode(TerraformStack):
    def __init__(self, scope, name):
        super().__init__(scope, name)

        GoogleProvider(
            self, "google", project="example-project", region="us-west1"
        )

        google_compute_instance_example_instance = ComputeInstance(
            self,
            "example_instance",
            boot_disk={"initialize_params": {"image": "debian-cloud/debian-11"}},
            machine_type="f1-micro",
            name="example-instance--via-cdktf",
            network_interface=[
                {
                    "network": "default",
                    "access_config":[{}],
                }
            ],
            zone="us-west1-a",
        )

        TerraformOutput(
            self,
            "instance_network_interface",
            value=google_compute_instance_example_instance.network_interface,
        )

Here's the console output (notice the empty access_config):

  instance_network_interface = [
    {
      "access_config": [],
      "alias_ip_range": [],
      "internal_ipv6_prefix_length": 0,
      "ipv6_access_config": [],
      "ipv6_access_type": "",
      "ipv6_address": "",
      "name": "nic0",
      "network": "https://www.googleapis.com/compute/v1/projects/example-project/global/networks/default",
      "network_ip": "10.111.2.33",
      "nic_type": "",
      "queue_count": 0,
      "stack_type": "IPV4_ONLY",
      "subnetwork": "https://www.googleapis.com/compute/v1/projects/example-project/regions/us-west1/subnetworks/default",
      "subnetwork_project": "example-project"
    }
  ]

Expectations

  • I expect a non-empty access_config containing the public IP or nat_ip.

Questions

  1. Why does the CDKTF approach not automatically assign a public IP when access_config is left empty, unlike the HCL Terraform script?
  2. Is there a specific configuration or approach I am missing in CDKTF to ensure the GCP compute instance gets a public IP?

Any insights or suggestions would be greatly appreciated. I am open to alternative approaches or corrections in my current implementation.


Appendix

Environment and Versions

  • Terraform version: v1.5.7
  • Python version: 3.12.1
  • CDKTF version: 0.19.2
  • CDKTF provider versions (from Poetry's pyproject.toml):
    [tool.poetry.dependencies]
    python = "^3.9"
    cdktf-cdktf-provider-google = "^12.1.0"
    
0

There are 0 answers