ENV specific stacks, or ignore

Hi there, like many others, I’d like to thank you for this amazing tool. I love how easy it is to use to create your boilerplate framework of terraform config as well being so easy to setup environments. Which leads me to my question.

I have been primarily working on my TS_ENV=dev stacks environment and its been working great!

I am now at a point where I want to initialize my TS_ENV=prod stack environment. Question here is that with the way I setup my stacks, its trying to provision some global resources again such as Route53 to be more specific.

Is there a method to instantiate these global resources so they are not done on a per environment level? Or an ignore env X,Y,Z for this stack?

I see another discussion about the terraspace up all command and to configure your app.rb to handle it. Wondering if we can say that only prod can call X stack? I tried to move my global resources into a global stack name to configure it this way, but its only handled now via the up all command but not calling the stack directly.

Maybe support:

config.all.ignore_stacks = nil
config.":ENV".ignore_stacks = nil. <-- allow choosing of envs?

Cheers, hope I explained myself correctly :stuck_out_tongue:

I just went through some of the other threads, noticed you could add ERB to your HCL files.

Ended up with this:

<% raise "Stack is not meant for anything other than prod" if Terraspace.env != "prod" %>

I should be ok with this for now. Please if anyone else comes up with another solution I would love to hear it!

Thank you so much for the kind words!

Yes, makes sense.

To answer this in short, you might want something like this:

config/app.rb

Terraspace.configure do |config|
  if Terraspace.env == "global"
    config.all.include_stacks = ["route53"]
  else
    config.all.exclude_stacks = ["route53"]
  end
end

Put together docs to explain it further: https://terraspace.cloud/docs/dependencies/exclude/

Also, made a BoltOps Learn video. Consider getting a subscription or asking your company. Trying to figure out ways to be able to work and improve Terraspace more. No sweat either way of course. :grinning_face_with_smiling_eyes: Thanks!

Note: In digging into this, released terraspace 1.0.4 and updated the option to config.all.exclude_stacks. The config.all.ignore_stacks still works but you’ll receive a deprecation warning.

Hey @tung . Thank you for the reply and explanation. My understanding is that this config will only work if a user uses the all stacks but not individual ones.

With your answer, I think this is the behaviour

terraspace up route53 (still invoked regardless of env)
terraspace all up (would ignore **route53** stack)

I see! Was wondering why the raise in ERB was ok to use, as it won’t work with terraspace all.

The aim here is to restrict certain stacks from being deployed with certain TS_ENV environments for terraspace up. The config.all related options were being conflated, at least in my mind. :rofl: Think get it now, though. Thanks for kindly pointing out that difference. :+1:

Okay, dugged into it. Believe tackled it in this PR and Docs:

Added the ability to control which stacks are allowed to be deployed. Believe this will work for what you’re trying to achieve:

config/app.rb

Terraspace.configure do |config|
  if Terraspace.env == "global"
    config.allow.stacks = ["route53"]
  else
    config.deny.stacks = ["route53"]
  end
end

The docs cover more examples. If your logic gets more complex, consider the multiple file approach in the docs.

Implemented config.allow.stacks and config.deny.stacks in a way so that it’s used by terraspace up and also by terraspace all. So folks only have to set it one time. Happy with how that turned out. :tada:

Note: The config.ENV.exclude_stacks can be achieved with config/envs/ENV.rb already. Got mixed feelings about the config.ENV.exclude_stacks notation. :man_shrugging:t2: Leaning on not adding it and sticking with only config/envs/ENV.rb right now.