anyone know how i can avoid resource replacement i...
# general
b
anyone know how i can avoid resource replacement in case of changing the provider? i just want to apply to provider without doing anything else
l
I've always done this by manually editing the state. It may be possible via aliases, but I doubt it.
b
ive manual too, but its alot of them this time 🥵
aliases will not help
l
It's usually a straight-forward search and replace, I think?
b
i got huge stack with many changes, imma mess it up for sure
in the past just setting
--target
to something random used to just ignore provider changes, for some reason it doesnt work for me this time
l
Definitely back your stack up! 🙂
b
manual is not an option for me here
l
Why not?
b
its too many changes in too many places, those places are also very sensitive
l
The number shouldn't matter. Search and replace. So long as you have a backup, it won't be a problem. You're not going to push any changes to your provider, all changes will be state-only. If you see a change that would go out to the cloud, then you don't make that change.
You can even sort-of test by importing your exported stack to a new stack, and work there. That's probably not worth it though, since you need to make code and stack changes in parallel, to prevent anything being pushed to the cloud.
b
im going to use both
pulumi preview --json
and
pulumi stack export
to automate it
heres the script:
Copy code
const { stdout } = await runProcess({
  command: 'pulumi',
  commandArgs: ['stack', 'export'],
});
const state = JSON.parse(stdout.toString());
const resources: any[] = [];
const { stdout } = await runProcess({
  command: 'pulumi',
  commandArgs: ['preview', '--json'],
});
const diff = JSON.parse(stdout.toString());
const providerReplaces: { urn: string; newProvider: string }[] = [];
for (const step of diff.steps) {
  if (step.op === 'replace' && step.replaceReasons.includes('provider')) {
    providerReplaces.push({ urn: step.urn, newProvider: step.newState.provider });
  }
}
for (const resource of state.deployment.resources) {
  const replacement = providerReplaces.find((r) => r.urn === resource.urn);
  if (replacement) {
    resource.provider = replacement.newProvider;
  }
  resources.push(resource);
}
state.deployment.resources = resources;
console.log(JSON.stringify(state));