Is there a way to use resource transformation with...
# getting-started
n
Is there a way to use resource transformation with the automation API? I can't find a way to do it
c
Resource transformation is a feature of the runtime and not the CLI. You'll have to declare the transformation in your infra code. You should be able to do this even if you were executing an inline program using Automation API.
n
Makes sense, but I just can't find the specific place to set it in the code... In the CLI app, with C#, it's simple:
Copy code
return await Deployment.RunAsync(
	PulumiApp,
	new StackOptions {
		ResourceTransformations = [
			MyTransformations.GetTransformation()
		]
	});
But even though I searched for hours I couldn't find any call that receives that type (of the transformation):
ResourceTransformation
. Neither the
LocalWorkspace
methods, nor any
*ProgramArgs
type or anything else in the automation API seems to have any parameter that receives it. So I'm kindda lost...
c
Hmm I am not following what you are doing with Automation API. Any chance you can post more code snippets to add more context?
n
I'm not sure what to post, as the problem is that I can't find the place where I can provide my transformations. I can show what I'm doing today with the CLI:
Copy code
return await Deployment.RunAsync(
	PulumiApp,
	new StackOptions {
		ResourceTransformations = [
			AutoTagger.CreateAutoTaggingTransformation(),
		]
	});

async Task<IDictionary<string, object?>> PulumiApp() {
	// ... not really important ...
	return outputs;
}
And that's the
AutoTagger
that's being referenced above:
Copy code
public static class AutoTagger {
	public static ResourceTransformation CreateAutoTaggingTransformation(ICloudResources cloudResources) =>
		args => {
			// ... add tags to resources ...
			return new ResourceTransformationResult(args.Args, args.Options);
		};
}
And that's working great, but when I want to use that same
AutoTagger
, then there is just no place to call it with the automation API
This is roughly the automation basis I'm using:
Copy code
public class PulumiAutomationWorkspaceRunner(string projectName, string stackName, Func<AppName, Task<Dictionary<string, object?>>> program) {
	private readonly Task<WorkspaceStack> stackTask = LocalWorkspace.CreateOrSelectStackAsync(
		new InlineProgramArgs(projectName, stackName, PulumiFn.Create(async () => await program(new AppName()))
		) {
			EnvironmentVariables = new Dictionary<string, string?> {
				["AWS_PROFILE"] = Hub.AwsProfile,
			},
		}
	);

		public async Task<IImmutableDictionary<string, OutputValue>> Up() {
		try {
			WorkspaceStack stack = await stackTask;
			var version = typeof(Provider).Assembly.GetName().Version!.ToString()[..^2];
			await stack.Workspace.InstallPluginAsync("aws", version);
			UpResult upResult = await stack.UpAsync(new UpOptions { OnEvent = handler.Hanlder });
			return upResult.Outputs;
		} catch (Exception e) {
			Console.Error.WriteLine(e);
			throw;
		}
	}

	/*
		...
	*/
}
But no method seems to accept a parameter of type
ResourceTransformation
, which is what the
AutoTagger.CreateAutoTaggingTransformation
returns
So I can't find any place to use it
c
What is the async method
program
in that second snippet? It looks like you are defining an inline program based on your use of
InlineProgramArgs
. Whatever that method is doing, you should define the autotagger transformation in there.
Does PulumiFn.Create not have a way for you to set the transformation for the stack when you have an inline program? If not, that is likely an oversight. Your workaround could be to not use an inline program definition and instead have your infra/stack code like you would normally with a CLI-based execution and point the Automation API to run that. Here's an example of that: https://github.com/pulumi/automation-api-examples/blob/main/dotnet/LocalProgram/automation/Program.cs
n
Yea I didn't want to burden with too much code... Here it is:
Copy code
public class DbTasks(AppName appName) {
	public static void Up() => Stacker.Up().Wait();

	private static readonly PulumiAutomationWorkspaceRunner Stacker = new(
		"ca",
		"autotestingstack",
		async appName => await new DbTasks(appName).Run()
	);

	private async Task<Dictionary<string, object?>> Run() {
		Cluster dbCluster = new Cluster(appName.UnderScored, new ClusterArgs { /* ... */ });
		return new() {
			["cluster"] = dbCluster.Endpoint,
		};
	}
}
This is basically the entry point of the application, which is essentially just creating a DB
Cluster
(it's a testing code). But there too - there is no place to insert a
ResourceTransformation
, unless I'm missing something.
PulumiFn.Create
doesn't have any parameter of that type, or any other type that may contain such thing: https://www.pulumi.com/docs/reference/pkg/dotnet/Pulumi.Automation/Pulumi.Automation.PulumiFn.html
I tried to somehow use my own code, but they seem to collide, because the CLI code must have a
Deployment.RunAsync
call to launch it (where I also place the transformation), but when calling that
Deployment.RunAsync
call from the automation API, it's throwing an error: `Program run without the Pulumi engine available; re-run using the
pulumi
CLI`
The link that you provided is a normal automation API - it doesn't have that CLI's
Deployment.RunAsync
call
b
I'm having the same issue as well. Have you found a solution to this? @nice-bear-3100
n
Unfortunately not 🫤 It looks like it hasn't been implemented yet. Hoping that they'll implement it sometime soon.