Terrafile Multi Environment

Hi,

What it’s the best design pattern to use Terrafile in Multi Environment?

For exemple, for dev environment (dev branch) i want my terrafile pointing to modules develop branch, but in production branch i want terrafile pointing to master branch in modules.

Thanks for the great Work.

Regards,
Fábio Santos

Think this is what you’re looking for https://terraspace.cloud/docs/patterns/different-versions/ Thanks!

Hi @tung ,

Tks for reply.

That approach (using custom helper to versioning) don’t use the Terrafile approach. Is possible doing this using Terrafile?

Regards,
Fábio Santos

Interesting. That’s a cleaner approach when using the Terrafile.

So the Terrafile is a DSL, but at the end of the day it’s actually just ruby. Which means you have access to the full power of ruby in it. For example:

Terrafile

if Terraspace.env == "dev"
  mod "sg", source: "terraform-aws-modules/security-group/aws", version: "4.4.0"
else # IE: prod
  mod "sg", source: "terraform-aws-modules/security-group/aws", version: "4.3.0"
end

Shorter single line:

mod "sg", source: "terraform-aws-modules/security-group/aws", version: Terraspace.env == "dev" ? "4.4.0" : "4.3.0"

It would be also cool if there was a group syntax similar to bundler like so though:

group :dev do
  mod "sg", source: "terraform-aws-modules/security-group/aws", version: "4.4.0"
end

group :prod do
  mod "sg", source: "terraform-aws-modules/security-group/aws", version: "4.3.0"
end

Will consider PRs. No sweat either way, of course.

Hope that helps.

Hi @tung,

Many Tks. this is what i was looking for.

Regards,
Fábio Santos

1 Like