https://pulumi.com logo
#general
Title
# general
b

big-restaurant-51338

03/23/2020, 12:02 PM
hi! how do I retrieve the internal IP of a newly created
compute.Instance
? I tried this, but I get a KeyError
Copy code
# actual vm creation
instance = compute.Instance("instance",
        machine_type=config.require('type'),
        boot_disk={
            "initializeParams": {
                "image": config.require('image')
                }
            },
        network_interfaces=[
            {
                "subnetwork": parentStack.get_output('subnet_id'),
                }
            ],
        # metadata={
        #     "startup-script": init_script
        #     },
        zone = config.require('zone'),
        tags = ["mytags"]
        )
# Register the DNS record
dns_name = parentStack.get_output('private_dns_zone').apply(lambda a: f"instance.{a}")
instance_recordset = dns.RecordSet("instance-recordset",
        managed_zone = parentStack.get_output('private_dns_name'),
        name = dns_name,
        rrdatas = [ instance.network_interfaces[0]['networkIp'] ],
        ttl = 3600,
        type = "A",
        opts=pulumi.ResourceOptions(depends_on=[instance]))
t

tall-librarian-49374

03/23/2020, 1:37 PM
b

big-restaurant-51338

03/23/2020, 1:38 PM
Thanks, I'll try
It didn't work. I'm using python and tried
pulumi.export("ip", compute.network_interfaces[0]['accessConfigs'][0]['natIp'])
. If the instance was already created (in a previous run) the code works just fine
Ok, now it worked
the correct code is
Copy code
pulumi.export("compute_ext_ip", compute.network_interfaces.apply(lambda a: a[0].get('accessConfigs')[0].get('natIp')))
pulumi.export("compute_int_ip", compute.network_interfaces.apply(lambda a: a[0].get('networkIp')))