https://pulumi.com logo
Title
i

important-receptionist-30715

12/06/2022, 9:10 PM
I am using pulumi with c# and I want to run some c# code ONLY if specific resource is created. how do I do it ?
b

billowy-army-68599

12/06/2022, 9:12 PM
Run it inside an apply for that resource
i

important-receptionist-30715

12/06/2022, 9:14 PM
where is the apply ?
b

billowy-army-68599

12/06/2022, 9:15 PM
It exists in an output from the resource in question.
i

important-receptionist-30715

12/06/2022, 9:18 PM
suppose I have this command: var sqlserver = new Azure.Sql.SqlServer(“sqlserversolargikk”, new() {…}
what is the apply command ? lets say I want to print the resource name
there is no apply method for variable sqlserver
b

billowy-army-68599

12/06/2022, 9:22 PM
i

important-receptionist-30715

12/06/2022, 9:24 PM
I saw this page, but there is no support for the resources I want to create like sqlserver…
b

billowy-army-68599

12/06/2022, 9:25 PM
There is….I can’t write any code as I’m on mobile, but it’s definitely possible
i

important-receptionist-30715

12/06/2022, 9:30 PM
will be happy if you could later write a few lines in c# to show how to run console.write(), at resource creation, thanks !
isn’t this what I need ?
it says here that it only support javascript
b

billowy-army-68599

12/06/2022, 9:32 PM
No, you just need to run the console writeline inside an apply
i

important-receptionist-30715

12/06/2022, 9:33 PM
I wish I saw the apply…
m

miniature-musician-31262

12/06/2022, 9:37 PM
Hi Asaf, let me see if I can get you a good example in C# --
i

important-receptionist-30715

12/06/2022, 9:38 PM
I am using azure and not azureNative, does it matter ?
m

miniature-musician-31262

12/06/2022, 9:40 PM
Nope, it’s a method that exists on all Pulumi
Output
types. One sec here
What you want is basically this:
var someResource = new FooResource("name", new 
{
    // ...
}).SomePropertyOfFoo.Apply(unwrappedValue => {

    // Do something with the value.
    <http://Pulumi.Log.Info|Pulumi.Log.Info>(unwrappedValue);

    // Return the value.
    return unwrappedValue;
});
But the linked example works end to end if a complete example is clearer. That should work for
Azure.Sql.SqlServer
as well — all Pulumi Output values have an
Apply
method that returns the resolved, unwrapped (“raw”) value. The linked doc Lee shared describes this in more detail: https://www.pulumi.com/docs/intro/concepts/inputs-outputs/#apply
i

important-receptionist-30715

12/07/2022, 5:57 AM
@miniature-musician-31262, @billowy-army-68599 Thanks for all the reply but I want the code to run ONLY if the resource created now. if it exists I DON’T want the code to run !
b

billowy-army-68599

12/07/2022, 5:59 AM
If you define a resource in your Pulumi program it will always create. If it exists it will only modify it if you change the code
i

important-receptionist-30715

12/07/2022, 6:07 AM
by created I mean not existing in the azure cloud
b

billowy-army-68599

12/07/2022, 6:08 AM
Yes I understand. Any resource you define in your Pulumi program will always exist because that’s how Pulumi works
You can’t define a resource in a Pulumi program and then expect it not to exist
i

important-receptionist-30715

12/07/2022, 9:00 AM
but the apply is running after the resource is created in the cloud or it is internal to pulumi before running the cloud deploy ?
e

echoing-dinner-19531

12/07/2022, 9:46 AM
@important-receptionist-30715 this feature is currently unsupported in C#. For python or nodejs you could use dynamic providers to hook a second dynamic resource into the lifecycle chain, but we don't (currently) have dynamic providers for dotnet. There are other plans to support life cycle hooks, which would also cover this.
m

miniature-musician-31262

12/07/2022, 4:08 PM
I see, thanks for the clarification. Can I ask, what is it (or what kind of thing is it) that you’re trying to do when the resource is created for the first time, but not thereafter? Depending on what it is, you might be able to use the Command provider, which I believe does expose the kind of interface you may be looking for: https://www.pulumi.com/registry/packages/command/
The first few bullets on the overview page there may speak to what you’re trying to do @important-receptionist-30715
Ah, and looks like Luke pointed this out (using Command) as well in that lifecycle-hooks issue: https://github.com/pulumi/pulumi/issues/1691#issuecomment-1003445778
i

important-receptionist-30715

12/07/2022, 5:59 PM
great @miniature-musician-31262 Many thanks for all the replies. My use case is that I run Pulumi in c# with Azure as cloud provider, and I create a APP FUNCTION. if it is the first time, I want upload a LAMBDA function to the APP FUNCTION, either by downloading specific git repository, build and upload, or calling Git action Ci/Cd workflow I want to upload the LAMBDA function only the first time the APP FUNCTION is created
m

miniature-musician-31262

12/07/2022, 6:31 PM
Ok. If you have any code you could share, that would help. I think based on what you’re describing that you should be able to use regular Pulumi resources for all of this, but without some code that shows me which Azure resources you’re working with (whether/how you’re using FunctionApp, FunctionAppFunction, etc.), it’s hard to say for sure. A GitHub gist of your program code would probably work.