Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

License

NotificationsYou must be signed in to change notification settings

pulumi/pulumi-command

Actions StatusSlackNPM versionPython versionNuGet versionPkgGoDevLicense

Pulumi Command Provider (preview)

The Pulumi Command Provider enables you to execute commands and scripts either locally or remotely as part of the Pulumi resource model. Resources in the command package support running scripts oncreate anddestroy operations, supporting stateful local and remote command execution.

There are many scenarios where the Command package can be useful:

  • 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 remotely 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 (especially in languages which do not yet support Dynamic Providers).

Some users may have experience with Terraform "provisioners", and the Command package offers support for similar scenarios. However, the Command package is provided as independent resources which can be combined with other resources in many interesting ways. This has many strengths, but also some differences, such as the fact that a Command resource failing does not cause a resource it is operating on to fail.

You can use the Command package from a Pulumi program written in any Pulumi language: C#, Go, JavaScript/TypeScript, Python, and YAML.You'll need toinstall and configure the Pulumi CLI if you haven't already.

NOTE: The Command package is in preview. The API design may change ahead of general availability based onuser feedback.

Examples

A simple local resource (random)

The simplest use case forlocal.Command is to just run a command oncreate, which can return some value which will be stored in the state file, and will be persistent for the life of the stack (or until the resource is destroyed or replaced). The example below uses this as an alternative to therandom package to create some randomness which is stored in Pulumi state.

import{local}from"@pulumi/command";constrandom=newlocal.Command("random",{create:"openssl rand -hex 16",});exportconstoutput=random.stdout;
package mainimport ("github.com/pulumi/pulumi-command/sdk/go/command/local""github.com/pulumi/pulumi/sdk/v3/go/pulumi")funcmain() {pulumi.Run(func(ctx*pulumi.Context)error {random,err:=local.NewCommand(ctx,"my-bucket",&local.CommandInput{Create:pulumi.String("openssl rand -hex 16"),})iferr!=nil {returnerr}ctx.Export("output",random.Stdout)returnnil})}

Remote provisioning of an EC2 instance

This example creates and EC2 instance, and then usesremote.Command andremote.CopyFile to run commands and copy files to the remote instance (via SSH). Similar things are possible with Azure, Google Cloud and other cloud provider virtual machines. Support for Windows-based VMs is being trackedhere.

Note that implicit and explicit (dependsOn) dependencies can be used to control the order that theseCommand andCopyFile resources are constructed relative to each other and to the cloud resources they depend on. This ensures that thecreate operations run after all dependencies are created, and thedelete operations run before all dependencies are deleted.

Because theCommand andCopyFile resources replace on changes to their connection, if the EC2 instance is replaced, the commands will all re-run on the new instance (and thedelete operations will run on the old instance).

Note also thatdeleteBeforeReplace can be composed withCommand resources to ensure that thedelete operation on an "old" instance is run before thecreate operation of the new instance, in case a scarce resource is managed by the command. Similarly, other resource options can naturally be applied toCommand resources, likeignoreChanges.

import{interpolate,Config}from"@pulumi/pulumi";import{local,remote,types}from"@pulumi/command";import*asawsfrom"@pulumi/aws";import*asfsfrom"fs";import*asosfrom"os";import*aspathfrom"path";import{size}from"./size";constconfig=newConfig();constkeyName=config.get("keyName")??newaws.ec2.KeyPair("key",{publicKey:config.require("publicKey")}).keyName;constprivateKeyBase64=config.get("privateKeyBase64");constprivateKey=privateKeyBase64 ?Buffer.from(privateKeyBase64,'base64').toString('ascii') :fs.readFileSync(path.join(os.homedir(),".ssh","id_rsa")).toString("utf8");constsecgrp=newaws.ec2.SecurityGroup("secgrp",{description:"Foo",ingress:[{protocol:"tcp",fromPort:22,toPort:22,cidrBlocks:["0.0.0.0/0"]},{protocol:"tcp",fromPort:80,toPort:80,cidrBlocks:["0.0.0.0/0"]},],});constami=aws.ec2.getAmiOutput({owners:["amazon"],mostRecent:true,filters:[{name:"name",values:["amzn2-ami-hvm-2.0.????????-x86_64-gp2"],}],});constserver=newaws.ec2.Instance("server",{instanceType:size,ami:ami.id,keyName:keyName,vpcSecurityGroupIds:[secgrp.id],},{replaceOnChanges:["instanceType"]});// Now set up a connection to the instance and run some provisioning operations on the instance.constconnection:types.input.remote.ConnectionInput={host:server.publicIp,user:"ec2-user",privateKey:privateKey,};consthostname=newremote.Command("hostname",{    connection,create:"hostname",});newremote.Command("remotePrivateIP",{    connection,create:interpolate`echo${server.privateIp} > private_ip.txt`,delete:`rm private_ip.txt`,},{deleteBeforeReplace:true});newlocal.Command("localPrivateIP",{create:interpolate`echo${server.privateIp} > private_ip.txt`,delete:`rm private_ip.txt`,},{deleteBeforeReplace:true});constsizeFile=newremote.CopyFile("size",{    connection,localPath:"./size.ts",remotePath:"size.ts",})constcatSize=newremote.Command("checkSize",{    connection,create:"cat size.ts",},{dependsOn:sizeFile})exportconstconfirmSize=catSize.stdout;exportconstpublicIp=server.publicIp;exportconstpublicHostName=server.publicDns;exportconsthostnameStdout=hostname.stdout;

Invoking a Lambda during Pulumi deployment

There may be cases where it is useful to run some code within an AWS Lambda or other serverless function during the deployment. For example, this may allow running some code from within a VPC, or with a specific role, without needing to have persistent compute available (such as the EC2 example above).

Note that the Lambda function itself can be created within the same Pulumi program, and then invoked after creation.

The example below simply creates some random value within the Lambda, which is a very roundabout way of doing the same thing as the first "random" example above, but this pattern can be used for more complex scenarios where the Lambda does things a local script could not.

import{local}from"@pulumi/command";import*asawsfrom"@pulumi/aws";import*ascryptofrom"crypto";constf=newaws.lambda.CallbackFunction("f",{publish:true,callback:async(ev:any)=>{returncrypto.randomBytes(ev.len/2).toString('hex');}});constrand=newlocal.Command("execf",{create:`aws lambda invoke --function-name "$FN" --payload '{"len": 10}' --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:"",},})exportconstoutput=rand.stdout;

Usinglocal.Commandwith CURL to manage external REST API

This example useslocal.Command to create a simple resource provider for managing GitHub labels, by invokingcurl commands oncreate anddelete commands against the GitHub REST API. A similar approach could be applied to build other simple providers against any REST API directly from within Pulumi programs in any language. This approach is somewhat limited by the fact thatlocal.Command does not yet supportdiff/read. Support forRead andDiff may be added in the future.

This example also shows howlocal.Command can be used as an implementation detail inside a nicer abstraction, like theGitHubLabel component defined below.

import*aspulumifrom"@pulumi/pulumi";import*asrandomfrom"@pulumi/random";import{local}from"@pulumi/command";interfaceLabelArgs{owner:pulumi.Input<string>;repo:pulumi.Input<string>;name:pulumi.Input<string>;githubToken:pulumi.Input<string>;}classGitHubLabelextendspulumi.ComponentResource{publicurl:pulumi.Output<string>;constructor(name:string,args:LabelArgs,opts?:pulumi.ComponentResourceOptions){super("example:github:Label",name,args,opts);constlabel=newlocal.Command("label",{create:"./create_label.sh",delete:"./delete_label.sh",environment:{OWNER:args.owner,REPO:args.repo,NAME:args.name,GITHUB_TOKEN:args.githubToken,}},{parent:this});constresponse=label.stdout.apply(JSON.parse);this.url=response.apply((x:any)=>x.urlasstring);}}constconfig=newpulumi.Config();constrand=newrandom.RandomString("s",{length:10,special:false});constlabel=newGitHubLabel("l",{owner:"pulumi",repo:"pulumi-command",name:rand.result,githubToken:config.requireSecret("githubToken"),});exportconstlabelUrl=label.url;
# create_label.shcurl \  -s \  -X POST \  -H"authorization: Bearer$GITHUB_TOKEN" \  -H"Accept: application/vnd.github.v3+json" \  https://api.github.com/repos/$OWNER/$REPO/labels \  -d"{\"name\":\"$NAME\"}"
# delete_label.shcurl \  -s \  -X DELETE \  -H"authorization: Bearer$GITHUB_TOKEN" \  -H"Accept: application/vnd.github.v3+json" \  https://api.github.com/repos/$OWNER/$REPO/labels/$NAME

Graceful cleanup of workloads in a Kubernetes cluster

There are cases where it's important to run some cleanup operation before destroying a resource such as when destroying the resource does not properly handle orderly cleanup. For example, destroying an EKS Cluster will not ensure that all Kubernetes object finalizers are run, which may lead to leaking external resources managed by those Kubernetes resources. This example shows how we can use adelete-onlyCommand to ensure some cleanup is run within a cluster before destroying it.

resources:cluster:type:eks:ClustercleanupKubernetesNamespaces:# We could also use `RemoteCommand` to run this from# within a node in the cluster.type:command:local:Commandproperties:# This will run before the cluster is destroyed.# Everything else will need to depend on this resource# to ensure this cleanup doesn't happen too early.delete:|        kubectl --kubeconfig <(echo "$KUBECONFIG_DATA") delete namespace nginx# Process substitution "<()" doesn't work in the default interpreter sh.interpreter:["/bin/bash", "-c"]environment:KUBECONFIG_DATA:"${cluster.kubeconfigJson}"
import*aspulumifrom"@pulumi/pulumi";import*ascommandfrom"@pulumi/command";import*aseksfrom"@pulumi/eks";constcluster=neweks.Cluster("cluster",{});// We could also use `RemoteCommand` to run this from within a node in the clusterconstcleanupKubernetesNamespaces=newcommand.local.Command("cleanupKubernetesNamespaces",{// This will run before the cluster is destroyed.  Everything else will need to// depend on this resource to ensure this cleanup doesn't happen too early."delete":"kubectl --kubeconfig <(echo \"$KUBECONFIG_DATA\") delete namespace nginx\n",// Process substitution "<()" doesn't work in the default interpreter sh.interpreter:["/bin/bash","-c",],environment:{KUBECONFIG_DATA:cluster.kubeconfigJson,},});

Working with Assets and Paths

When a local command creates assets as part of its execution, these can be captured by specifyingassetPaths orarchivePaths.

constlambdaBuild=local.runOutput({dir:"../my-function",command:`yarn && yarn build`,archivePaths:["dist/**"],});newaws.lambda.Function("my-function",{code:lambdaBuild.archive,// ...});

When using theassetPaths andarchivePaths, they take a list of 'globs'.

  • We only include files not directories for assets and archives.
  • Path separators are/ on all platforms - including Windows.
  • Patterns starting with! are 'exclude' rules.
  • Rules are evaluated in order, so exclude rules should be after inclusion rules.
  • * matches anything except/
  • ** matches anything,including/
  • All returned paths are relative to the working directory (without leading./) e.g.file.text orsubfolder/file.txt.
  • For full details of the globbing syntax, seegithub.com/gobwas/glob

Asset Paths Example

Given the rules:

-"assets/**"-"src/**.js"-"!**secret.*"

When evaluating against this folder:

-assets/  -logos/    -logo.svg-src/  -index.js  -secret.js

The following paths will be returned:

-assets/logos/logo.svg-src/index.js

Building

Dependencies

  • Go 1.17
  • NodeJS 10.X.X or later
  • Python 3.6 or later
  • .NET Core 3.1

Please refer toContributing to Pulumi for installationguidance.

Building locally

Run the following commands to install Go modules, generate all SDKs, and build the provider:

$ make ensure$ make build$ make install

Add thebin folder to your$PATH or copy thebin/pulumi-resource-command file to another location in your$PATH.

Running an example

Navigate to the simple example and run Pulumi:

$ cd examples/simple$ yarn link @pulumi/command$ yarn install$ pulumi up

About

No description, website, or topics provided.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp