https://pulumi.com logo
#getting-started
Title
# getting-started
c

cuddly-jewelry-42389

12/31/2023, 2:28 PM
The following code fails with
reading Service Discovery test.svc.cluster.local Namespace (DNS_PRIVATE): couldn't find resource
during
pulumi preview
. Because the call to
getDnsNamespaceOutput
uses the output of
new aws.servicediscovery.PrivateDnsNamespace
as input, I would've expected the code to work, because the call to the get method should only be executed after the resource has been created and its name is available. Code:
Copy code
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const vpc = new aws.ec2.Vpc("vpc");

const serviceDiscoveryDnsNamespace = new aws.servicediscovery.PrivateDnsNamespace("services", {
    name: `test.svc.cluster.local`,
    vpc: vpc.id
});

const serviceDiscoveryNamespace = aws.servicediscovery.getDnsNamespaceOutput({
    name: serviceDiscoveryDnsNamespace.name,
    type: "DNS_PRIVATE"
});
What am I missing here?
d

dry-keyboard-94795

12/31/2023, 2:45 PM
This is a quirk in how dependencies work with invoke (pulumi functions). As you're using a constant string, the value is "known", so doesn't respect the resource not being created yet. A workaround is to use the resource id as well:
Copy code
name: serviceDiscoveryDnsNamespace.id.apply(_ => serviceDiscoveryDnsNamespace.name),
c

cuddly-jewelry-42389

12/31/2023, 2:55 PM
Why is name a constant string? It is of type
pulumi.Output<string>
d

dry-keyboard-94795

12/31/2023, 2:57 PM
The input of the resource is a constant string. Invokes don't follow the same dependency rules as resources. There's a related issue here: https://github.com/pulumi/pulumi/issues/14243
c

cuddly-jewelry-42389

12/31/2023, 2:59 PM
I see, thanks
b

billowy-army-68599

12/31/2023, 3:31 PM
Out of curiosity, why are you doing a
getDnsNamespace
on a resource you just created?
c

cuddly-jewelry-42389

12/31/2023, 3:35 PM
Because of encapsulation. I've a ComponentResource in which I need both the ID as well as the name of the Service Discovery DNS namespace - because registering a new service with the namespace requires the ID and the service discovery node in an App Mesh virtual node requires the name - and I don't want to polute the interface of the ComponentResource just because AWS is inconsistent in its inputs.
b

billowy-army-68599

12/31/2023, 3:37 PM
the ID and name both appear to be in the resource registration, all inputs are also outputs
c

cuddly-jewelry-42389

12/31/2023, 3:38 PM
Ok?
d

dry-keyboard-94795

12/31/2023, 3:39 PM
I think what you're saying is you don't want to pass both name and id in at the same time to the component?
c

cuddly-jewelry-42389

12/31/2023, 3:40 PM
Yes. If the namespace were an existing one - which the component absolutely allows - the caller of the component would be required to first fetch the namespace to have both name and id.
b

billowy-army-68599

12/31/2023, 3:41 PM
there is a better way of solving for that problem, but good luck 👍
c

cuddly-jewelry-42389

12/31/2023, 3:44 PM
What would be the better way? I just started with pulumi, trying to find my way around. So, I would be very happy to learn better ways. My "Ok?" answer wasn't dismissive, I just didn't understand what you wanted to tell me with your comment
d

dry-keyboard-94795

12/31/2023, 4:22 PM
You can pass the resource itself into the component constructor, instead of its outputs. This would negate the need for using the function to fetch the details
c

cuddly-jewelry-42389

12/31/2023, 4:47 PM
I see, thanks. Is this the recommended way?
d

dry-keyboard-94795

12/31/2023, 4:48 PM
It's something I do for more complex interactions between components. I'm not sure I've seen it in examples