The following code fails with `reading Service Dis...
# getting-started
c
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
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
Why is name a constant string? It is of type
pulumi.Output<string>
d
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
I see, thanks
b
Out of curiosity, why are you doing a
getDnsNamespace
on a resource you just created?
c
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
the ID and name both appear to be in the resource registration, all inputs are also outputs
c
Ok?
d
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
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
there is a better way of solving for that problem, but good luck 👍
c
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
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
I see, thanks. Is this the recommended way?
d
It's something I do for more complex interactions between components. I'm not sure I've seen it in examples