https://pulumi.com logo
a

average-horse-29207

08/31/2023, 12:14 PM
Hi, I'm trying to import some existing resources into pulumi state. When I run
pulumi up
, it complains that it can't create the resource (as it already exists). Based on https://www.pulumi.com/blog/repairing-state-with-pulumi-refresh/ I was expecting to be able to use
pulumi refresh
to import the existing resource into the state, however
pulumi refresh
command doesn't even seem to see the resource group or storage account, and doesn't seem to do anything. Any ideas why the refresh command isn't seeing any resources to refresh? pulumi up:
Copy code
Enter your passphrase to unlock config/secrets
    (set PULUMI_CONFIG_PASSPHRASE or PULUMI_CONFIG_PASSPHRASE_FILE to remember):
Previewing update (dev):
     Type                                     Name             Plan
     pulumi:pulumi:Stack                      pulumi-test-dev
 +   ├─ azure-native:resources:ResourceGroup  resourceGroup    create
 +   └─ azure-native:storage:StorageAccount   storage          create

Resources:
    + 2 to create
    1 unchanged

Do you want to perform this update? yes
Updating (dev):
     Type                                     Name             Status                  Info
     pulumi:pulumi:Stack                      pulumi-test-dev  **failed**              1 error
 +   └─ azure-native:resources:ResourceGroup  resourceGroup    **creating failed**     1 error

Diagnostics:
  azure-native:resources:ResourceGroup (resourceGroup):
    error: cannot create already existing subresource '/subscriptions/REDACTED/resourcegroups/AC-PulumiTest-dev'

  pulumi:pulumi:Stack (pulumi-test-dev):
    error: update failed

Resources:
    1 unchanged

Duration: 5s
pulumi refresh
Copy code
Previewing refresh (dev):
     Type                 Name             Plan
     pulumi:pulumi:Stack  pulumi-test-dev

Resources:
    1 unchanged

Do you want to perform this refresh?
No resources will be modified as part of this refresh; just your stack's state will be.
 yes
Refreshing (dev):
     Type                 Name             Status
     pulumi:pulumi:Stack  pulumi-test-dev

Resources:
    1 unchanged
resource in Program.cs
Copy code
var config = new Pulumi.Config();
    var shortName = config.Require("shortName");
    ResourceGroup resourceGroup = new("resourceGroup", new()
    {
        ResourceGroupName = $"AC-PulumiTest-{shortName}"
    });
b

brave-planet-10645

08/31/2023, 12:32 PM
Hi, to import you’ll either need to run the
pulumi import
command or the add the id to the resource. There’s some guidance on what to do (I would use the
pulumi import
command personally) on this page: https://www.pulumi.com/docs/using-pulumi/adopting-pulumi/import/
a

average-horse-29207

08/31/2023, 12:36 PM
Thanks, I'll give that a go. I am a bit confused though, based on "The Solution" section of https://www.pulumi.com/blog/repairing-state-with-pulumi-refresh/ blog post, I was expecting
pulumi refresh
to interactively walk me through the imports I needed Are these two different kinds of imports?
b

brave-planet-10645

08/31/2023, 12:37 PM
Are you talking about “out of band” changes?
Oh I see what you’re referring to
No that’s if the update is cancelled before the resources is created, but it’s still in Pulumi’s internal “todo list” (so it knows that it needs to create the resource but it hasn’t yet)
a

average-horse-29207

08/31/2023, 12:56 PM
Thanks for clarifying that. Is there a technical reason why
pulumi refresh
couldn't detect these changes, or is simply something that hasn't been written. I'm coming with a lot of familiarity deploying with ARM / bicep templates which don't include a state file, so I'm still trying to wrap my head around what the state file is doing)
b

brave-planet-10645

08/31/2023, 12:57 PM
Do you mean why doesn’t refresh know about resources that you want to import?
a

average-horse-29207

08/31/2023, 12:57 PM
yup
b

brave-planet-10645

08/31/2023, 12:58 PM
Pulumi can only refresh what it knows about, i.e. what’s in the state. It can’t just look through Azure and hoover up all the resources that you can read in Azure Azure kinda has state in that the whole of the ARM API is the state.
a

average-horse-29207

08/31/2023, 1:08 PM
ah ok, I think I see.
pulumi refresh
is purely looking at the state. And because pulumi's initial creation never got far enough to insert into the statefile,
pulumi refresh
never knows about the other resources. My thinking was that since
plumui up
knew enough about what resource to create (and that conflicted), that it could therefore be able to work out the specific resource to import. However I never see a call to
dotnet build
when running
pulumi refresh
, so it never has any of that information available to it.
Thank you very much for helping explain this to me Piers
b

brave-planet-10645

08/31/2023, 1:08 PM
exactly
no problem. Happy to help 🙂