https://pulumi.com logo
Title
g

gentle-monitor-55086

05/19/2021, 8:20 PM
What's the proper / best way to prevent resources from being destroyed that i want to toggle their deployment based on a configuration boolean... Basically trying to say "only deploy to the -dev or -production api's if i've explicitly set a variable in the conf to do so". Another example was i had
"if servers < desired { // deploy ec2}"
With the issue being on the second run pulumi would see we didn't define the first set of ec2's and go "oh well i should destroy those". My issue was that i was generating unique values for the ec2, storing it in the userdata, and then also saving those off to a DB... So even using
protect
or
ignoreChanges
it was (is) a pain to try and avoid re-generating / saving new values into the DB and required all sorts of shims and "clever"(breaks if you breathe on it) code. I don't currently have an idea about the api and would like to do it the "right" way
w

witty-candle-66007

05/19/2021, 8:28 PM
You can use Pulumi stacks to drive the different environments via the stack-specific configuration file. The ideas is that you have a project (i.e. the code) and then you instantiate different stacks for the different environments. So, in your “dev” config file you would have
num_desired_servers
set to 1 or whatever and in the “prod” config file this would be set to 5 or whatever. Pulumi then manages the stacks separately.
g

gentle-monitor-55086

05/19/2021, 8:35 PM
somehow i missed the stack referencing ( i was avoiding doing multiple stacks since it seemed like a pain to get the names of my DB's etc. into my other stacks etc. 🤦 ) However i'm not sure this would help for the server example... For that, i wanted to be able to say start a stack with say
num_desired_servers = 1
and then scale it up on another "up" command later to maybe 3.
I also wanted to be able to scale down as well.
I would just use
protect
but it seemed tedious to me to have to unprotect the servers to be able to destroy the stack
in hindsight it probably wouldn't have been that big of a deal
w

witty-candle-66007

05/19/2021, 8:46 PM
This sort of code:
const instanceCount = config.getNumber("instanceCount");
for (let i = 0; i < instanceCount; i++) {
   // Create servers here
}
If the config file for, say the staging stack, has
instanceCount
set to, say, 2, this code will create 2 servers. Then later, if the config for staging is changed so that
instanceCount
is now set to, say, 3, Pulumi will create 1 more server and leave the original 2 servers as-is.
If the intent is to do the 2 servers and then add the 1 server as part of one “event” then the automation API may be helpful here since it allows you do this sort of orchestration. https://www.pulumi.com/docs/guides/automation-api/