With regard the Pulumi Automation API. Lets say I ...
# general
n
With regard the Pulumi Automation API. Lets say I have a web page that gives users the option to select a project (DataScienceProject1) and option to create a VM . The problem I am finding is that if I create one VM and then try to create another with a different name, what actually happens is that Pulumi just renames the existing VM in that project, rather than adding to it. How do we handle creating the same resource type at different times with the same program? Or is this where Pulumi Tags come in. Each resource in this case would have to be in it's own project but a virtual project be defined and tagged to each resource.
I am thinking that perhaps when a new VM is created, I create a database entry for that resource and use the unique DB key as the name of pulumi project. Then tag the resource with a common name such as {project: "DataScienceProject1"}. Is this the correct way to go about it?
b
@narrow-monitor-83965 Ensure that the resource name is unique. Either manually by having some sort of algorithm that uniquely derives the resource name from "the context". Or just omit the resource name and use Pulumi's autonaming functionality.
n
@big-architect-71258 Thanks Thomas, I think the project also has to be unique. If I try to create another resource of the same type with a different name in the same project with a different pulumi Programme, it will just rename the existing resource. Bear in mind it is the Automation API workflow I am referring to.
b
It shouldn't do that. The logical name needs to be different too. So whatever language you're using, you should be able to simulate what you want by generating the VM from a dictionary or list. Start with 1 item and then add to it.
n
Just want to be sure were both talking about apples. I am talking about the Pulumi "Automation" API. If I have a single function called createVM(VMName) which contains the code to create a single VM. If my API calls that function twice with a different name but for the same project, it just renames the first VM. I understand why this would happen, because as far as pulumi is concerned I am just renaming the first VM. So my question was what is the best way (pulumi way) to create two VM's in the same "Business Project" , I suggested that it might be to use a Pulumi Tag {"project": "DataScienceProject1"} and each VM will have to be a different Pulumi project
b
@narrow-monitor-83965 Without some sort of code hard to guess what's going on, but typically you'd have a workspace (e.g. LocalWorkspace) and the workspace selects a stack. With a stack selected you're ready to deploy resources. Inside the stack all resources must be unique by their Pulumi resource names, not the name of the resource in the cloud. If you keep the Pulumi resource name constant it's logical that Pulumi changes the name of the existing VM and won't create a new VM instance with a new name.
n
Ah, I see what you mean for the resource name. I will give that a try, it sounds like it’s the solution. Thank you
b
Ensure that you re-create all VM instances at every run of Pulumi. So you have to save the names of the VMs somewhere to keep track which shall be deployed. Pulumi will destroy a VM automatically when you don't create it in a run.
n
Thanks I’ll keep that important point in mind
b
Another option would be to create a Pulumi stack named after the VM instance. Then you don't have worry about that. To delete a VM you'd call
pulumi destroy
when the stack is selected and remove the stack using the Workspace instance. I think this is easier to manage. The list of VM is then equal to the list of Pulumi stacks.
If you have shared resources you could use stack, let's say
shared
and reference the resources in it from the VM stacks using Pulumi stack references https://www.pulumi.com/learn/building-with-pulumi/stack-references/