Pulumi Command Provider 1.0 Release
Today, we’re happy to announce the 1.0 release of the Pulumi Command provider. This release marks the provider’s official transition from preview status to general availability (GA). Since its first preview release in late 2021, thousands of Pulumi users incorporated the Command package into their cloud infrastructure projects to help manage local and remote command execution and filesystem operations. In fact, the Command provider is already our ninth-most popular provider, so it's time to make things official!
The Pulumi Command Provider enables you to execute commands and scripts either locally or remotely as part of the Pulumi resource model, enabling stateful command execution. It also has convenient support for copying assets via SSH. Being able to run arbitrary commands opens up a wide variety of scenarios and integrations with other tools and systems.
Some of the provider's popular uses include:
- Running a command locally after creating a resource, to register it with an external service
- Running a command locally before deleting a resource, to deregister it with an external service
- Running a command on a remote host immediately after creating it
- Copying a file to a remote host after creating it (potentially as a script to be executed afterwards)
- As a simple alternative to some use cases for Dynamic Providers, by running appropriate commands in the different stages of the Pulumi life cycle.
Here’s a simple example of running an arbitrary command when another resource changes: once an AWS Lambda function is deployed, we call it and it responds with a message that is custom to the current Pulumi stack. In a real-world scenario, we could wait for a number of other resources to be deployed as well, and have the Lambda perform registration or initialization of services.
import * as aws from "@pulumi/aws";
import { local } from "@pulumi/command";
import { getStack } from "@pulumi/pulumi";
const f = new aws.lambda.CallbackFunction("f", {
publish: true,
callback: async (ev: any) => {
return `Stack ${ev.stackName} is deployed!`;
}
});
const invoke = new local.Command("execf", {
create: `aws lambda invoke --function-name "$FN" --payload '{"stackName": "${getStack()}"}' --cli-binary-format raw-in-base64-out out.txt >/dev/null && cat out.txt | tr -d '"' && rm out.txt`,
environment: {
FN: f.qualifiedArn,
AWS_REGION: aws.config.region!,
AWS_PAGER: "",
},
}, { dependsOn: f })
export const output = invoke.stdout;What's new
The 1.0 release of the Command provider marks a stable API for the 1.x series. Rather than simply putting a “v1” label on the existing provider, we also used this opportunity to enhance it with some previously requested capabilities to provide a more complete set of capabilities.
- The API documentation in the Pulumi registry has examples in all Pulumi languages and is expanded.
- Capturing stdout and stderr of commands can now be switched off, which is useful when they might contain secrets or are very noisy.
- Environment handling for remote commands has better error handling and is better documented.
- The
CopyFileresource is superseded by the newCopyToRemoteresource. It can copy whole directories in addition to individual files. The source of the copy is now a Pulumi asset or archive which provides full interoperability with the Pulumi ecosystem. The use of assets and archives also makes Pulumi run copy operations only if the source has changed. For an easy transition, the previousCopyFileresource will remain available with a deprecation notice until the next major version.
Here’s an example of copying a directory to a remote host. For brevity, the remote server is assumed to exist, but it could also be provisioned in the same Pulumi program.
import * as pulumi from "@pulumi/pulumi";
import { remote, types } from "@pulumi/command";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
export = async () => {
const config = new pulumi.Config();
// Get the private key to connect to the server. If a key is
// provided, use it, otherwise default to the standard id_rsa SSH key.
const privateKeyBase64 = config.get("privateKeyBase64");
const privateKey = privateKeyBase64 ?
Buffer.from(privateKeyBase64, 'base64').toString('ascii') :
fs.readFileSync(path.join(os.homedir(), ".ssh", "id_rsa")).toString("utf8");
const serverPublicIp = config.require("serverPublicIp");
const userName = config.require("userName");
// The configuration of our SSH connection to the instance.
const connection: types.input.remote.ConnectionArgs = {
host: serverPublicIp,
user: userName,
privateKey: privateKey,
};
// Set up source and target of the remote copy.
const from = config.require("payload")!;
const archive = new pulumi.asset.FileArchive(from);
const to = config.require("destDir")!;
// Copy the files to the remote.
const copy = new remote.CopyToRemote("copy", {
connection,
source: archive,
remotePath: to,
});
// Verify that the expected files were copied to the remote.
// We want to run this after each copy, i.e., when something changed,
// so we use the asset to be copied as a trigger.
const find = new remote.Command("ls", {
connection,
create: `find ${to}/${from} | sort`,
triggers: [archive],
}, { dependsOn: copy });
return {
remoteContents: find.stdout
}
}Getting started
If you're currently using one of the v0 pre-release versions of the Command provider, you don't need to make any changes to your existing code. However, the enhanced functionality of copying assets and archives is only available in the new CopyToRemote resource. If you're using CopyFile, you should consider migrating to the new resource after the upgrade to 1.0.
Enjoy the new features, and we’re looking forward to seeing what you build with the Command provider!