https://pulumi.com logo
Title
h

hallowed-camera-52062

03/30/2021, 12:41 PM
Hi! 👋🏼 I'd like to know if there is a way to gracefully exit a Pulumi Typescript program under a specific condition? For example:
const environmentName: string = pulumi.getStack().toLowerCase()

// Filter the resources that belong to the current environment
const resources: ResourceDefinition[] = resourcesDefinitions.filter(
  (r: ResourceDefinition) => r.environment === environmentName
)

if (resources.length === 0) {
  console.log(
    `There are no resources to be deployed onto the current environment ('${environmentName}').`
  )
  process.exit(0)
}

resources.map((resource) => {
  const getConventionName: ConventionNameGetter = useNamingConvention(
    resource.name,
    resource.environment
  )
  provisionResource(resource, getConventionName)
})
I tried implementing this with
process.exit(0)
but it seems like it's not a good idea:
The Pulumi runtime detected that 712 promises were still active

at the time that the process exited. There are a few ways that this can occur:

  * Not using `await` or `.then` on a Promise returned from a Pulumi API

  * Introducing a cyclic dependency between two Pulumi Resources

  * A bug in the Pulumi Runtime

Leaving promises active is probably not what you want. If you are unsure about

why you are seeing this message, re-run your program with the `PULUMI_DEBUG_PROMISE_LEAKS`

environment variable. The Pulumi runtime will then print out additional

debug information about the leaked promises.

error: an unhandled error occurred: Program exited with non-zero exit code: 1
Resources:
    + 1 to create
b

brave-planet-10645

03/30/2021, 12:42 PM
Not with a "standard" Pulumi program, but you can using our automation SDK
👍🏼 1
h

hallowed-camera-52062

03/30/2021, 12:43 PM
Thanks for the quick response, @brave-planet-10645. Do you have any link pointing to an example maybe?
b

better-shampoo-48884

03/30/2021, 12:43 PM
And if you still hit on the 712 promises still active after you go for automation - perhaps you can have a look for similarities with this: https://github.com/pulumi/pulumi/issues/6633
👍🏼 1
b

brave-planet-10645

03/30/2021, 12:44 PM
@hallowed-camera-52062 we have a whole load of examples here: https://github.com/pulumi/automation-api-examples
😍 1
h

hallowed-camera-52062

03/30/2021, 12:44 PM
Thanks a lot, people! 🙏🏼
d

dry-football-2639

03/30/2021, 1:41 PM
Instead of exitting a Pulumi program on a condition, you can lazily register your Pulumi resources by wrapping them into a JS function that evaluates on a condition, and optionally returns Outputs:
const lazyRunner = (resources: ResourceDefinition[]): ResourceOutputs => {
    return resources.map((resource) => {
        const getConventionName: ConventionNameGetter = useNamingConvention(
            resource.name,
            resource.environment
        )
        return provisionResource(resource, getConventionName)
    })
}

const environmentName: string = pulumi.getStack().toLowerCase()
// Filter the resources that belong to the current environment
const resources: ResourceDefinition[] = resourcesDefinitions.filter(
  (r: ResourceDefinition) => r.environment === environmentName
)

if (resources.length === 0) {
  console.log(
    `There are no resources to be deployed onto the current environment ('${environmentName}'). Skipping...`
  )
}

export const resourceOutputs: ResourceOutputs = (resources.length === 0) ? null : lazyRunner(resources)