Hi there! Is there a way of passing a callback to ...
# general
w
Hi there! Is there a way of passing a callback to the Pulumi script to execute after it creates the resources? For example
Copy code
// Create an Azure resource (Storage Account)
const storageAccount = new storage.StorageAccount('sa', {
  resourceGroupName: resourceGroup.name,
  sku: {
    name: storage.SkuName.Standard_LRS,
  },
  kind: storage.Kind.StorageV2,
});

// Export the primary key of the Storage Account
const storageAccountKeys = pulumi
  .all([resourceGroup.name, storageAccount.name])
  .apply(([resourceGroupName, accountName]) =>
    storage.listStorageAccountKeys({ resourceGroupName, accountName })
  );
export const primaryStorageKey = storageAccountKeys.keys[0].value;

// MAYBE A FUNCTION HERE TO BE EXECUTED AFTER SA IS CREATED/MODIFIED
const callback = () => { axios.get(`<http://mycustomserver.com/blabla/${> primaryStorageKey }`) }
c
What’s the problem with the code as it is? Pulumi should be waiting for the sa to be created because you are referencing it in
pulumi.all
.
w
Hey @cool-egg-852 thanks for the quick reply. I pressed enter too soon - edited the post now. So
pulumi.all
is the callback I am looking for then?
c
With your edit I can see what you are looking for, and I think there is a very hacky way to do it, but nothing native if I recall.
đź‘€ 1
I think https://github.com/pulumi/examples/tree/master/aws-ts-ec2-provisioners may be what you are looking for, but someone else may have a much better answer.
w
Thanks for the input, I appreciate it
b
With automation api you can subscribe to engine events and check for actions against specific resources and take action.
If you just want to do it in your Pulumi program, you can do in your code within an apply on an output from the storage account (which tells it to wait for the SA to be finished) and you’ll want to check for IsDryRun on the current deployment so that you aren’t taking your action during Pulumi preview
w
Hey @bored-oyster-3147, that is exactly the problem I was currently facing. Also when you use
pulumi up
locally you have the option to cancel the update. The
apply
function executes regardless. Can you elaborate on checking for IsDryRun on the current deployment? Where is that value value be listened to?
b
I'm assuming you're using typescript? This is the
IsDryRun
function: https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/pulumi/runtime/#isDryRun It's in the
pulumi.runtime
module
w
ah gotcha, so I can just use
pulumi.runtime.isDryRun()
to decide
thanks a lot Joshua! I appreciate the help