Skip to content

Instantly share code, notes, and snippets.

@clstokes
Last active December 11, 2020 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clstokes/b525400df424310cb48d747da634cb30 to your computer and use it in GitHub Desktop.
Save clstokes/b525400df424310cb48d747da634cb30 to your computer and use it in GitHub Desktop.
Pulumi - Migrate an existing Azure application to the Azure NextGen provider using import.

Migrating from Pulumi Azure to Pulumi Azure NextGen

The Pulumi Azure and Azure NextGen providers can be used from within the same Pulumi project to allow you to migrate to the new provider at your own pace. This means it is also possible to migrate existing resources from the Azure provider to the Azure NextGen provider with no impact or downtime to existing resources.

At a high-level the steps to do this are:

  1. Add the Pulumi Azure NextGen provider to your current Pulumi project - e.g. npm install @pulumi/azure-nextgen.
  2. Use the pulumi import command to import your existing resource into your stack as a NextGen resource.
  3. Update all references to the old resource to reference the new NextGen resource.
  4. Use the pulumi state delete command to remove the old resource from your state file.
  5. Remove the code for the old resource from your Pulumi project.
  6. Done.

Example

Example Code

This Pulumi application will create two resources, a azure.core.ResourceGroup and a azure.storage.Account, and we will migrate the azure.core.ResourceGroup to the equivalent azure_nextgen.resources.latest.ResourceGroup in the Azure NextGen provider.

Steps

Using the Example Code below the steps would be:

  1. Create resources to create resources using the Azure provider that we will migrate to the Azure NextGen provider.
    1. pulumi up -y
  2. Install the Azure NextGen provider.
    1. npm install @pulumi/azure-nextgen
  3. Import the Azure ResourceGroup as a NextGen ResourceGroup.
    1. pulumi import azure-nextgen:resources/latest:ResourceGroup main /subscriptions/32b9cb2e-69be-4040-80a6-02cd6b2cc5ec/resourceGroups/mainfbed5cc7
  4. Copy and paste the code output from the previous command into your index.ts and update all references to the old resource to reference the new main NextGen resource.
  5. Run pulumi up to ensure no changes are anticipated in the preview.
  6. Remove the Azure ResourceGroup from Pulumi's state file.
    1. pulumi state delete 'urn:pulumi:dev::p-azure-nextgen-migration::azure:core/resourceGroup:ResourceGroup::main'.
    2. Note: the urn value will be different for your Pulumi stack. You can see the urn for your ResourceGroup with pulumi stack --show-urns.
  7. Remove the code for the Azure ResourceGroup from your index.ts.
  8. Run pulumi up to ensure no changes are anticipated in the preview.
import * as azure from "@pulumi/azure";
import * as azure_nextgen from "@pulumi/azure-nextgen";
const resourceGroup = new azure.core.ResourceGroup("main");
const account = new azure.storage.Account("main", {
resourceGroupName: resourceGroup.name, // update to the new resource after importing
accountTier: "Standard",
accountReplicationType: "LRS",
});
export const resourceGroupId = resourceGroup.id; // update to the new resource after importing
export const accountId = account.id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment