How do I build local commands with infrastructure as code?
In this tutorial, we’ll demonstrate how to build a local command to execute tasks directly from our infrastructure code. This can be particularly useful for running scripts or commands that need to interact with your local system or CI/CD pipelines.
First, we will define a local null_resource
to represent the command execution. The null_resource
is a useful abstraction that allows us to attach provisioners or triggers without requiring an actual resource to manage. For this case, we’ll use the local-exec
provisioner to run our command locally.
Below is the example code block:
import * as pulumi from "@pulumi/pulumi";
import * as _null from "@pulumi/null";
import * as command from "@pulumi/command";
import * as std from "@pulumi/std";
// Define a null_resource to execute a local command
const exampleCommand = new _null.Resource("example_command", {triggers: {
always_run: std.timestampOutput({}).apply(invoke => invoke.result),
}});
const exampleCommandProvisioner0 = new command.local.Command("exampleCommandProvisioner0", {create: "echo Hello, World!"}, {
dependsOn: [exampleCommand],
});
export const commandOutput = "Local command executed successfully!";
Key Points:
- Provider Definition: Configures the provider required for our resources.
- null_resource: Acts as a placeholder for our local command execution.
- local-exec Provisioner: Executes the specified command on the local system.
- Triggers: Optional configuration to re-run the command based on timestamp changes.
- Output: Confirms the successful execution of our command.
Summary
By defining a null_resource
with a local-exec
provisioner, we can run local commands from our infrastructure as code setup. This approach enables a seamless integration of local scripts and commands within our infrastructure workflows, ensuring tasks like configuration and initial setup are executed efficiently.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.