Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

RFC: Improve environment configuration #89

Open
JakeGinnivan opened this issue Dec 5, 2022 · 2 comments
Open

RFC: Improve environment configuration #89

JakeGinnivan opened this issue Dec 5, 2022 · 2 comments

Comments

@JakeGinnivan
Copy link
Member

The NX plugin currently passes a bunch of -tfvar arguments, we could instead flip it so our terraform projects read the info from the project.

By default the NX plugin puts config into docs/environments/.md as frontmatter, with a little bit of a hack, we could drop in a file which makes the config available. I'm not sure how to type it though inside terraform.

This would make it easier to 'exit' NX later as well, because it makes the terragrunt commands much simpler.

locals {
  # Create a map { "kind--name" => yaml_doc } from the yaml frontmatter text.
  # Must use \n---\n to avoid splitting on strings and comments containing "---".
  # YAML allows "---" to be the first and last line of a file, so make sure
  # raw yaml begins and ends with a newline.
  # The "---" can be followed by spaces, so need to remove those too.
  # Skip blocks that are empty or comments-only in case yaml began with a comment before "---".
  yaml_file = [
    for yaml in split(
      "\n---\n",
      "\n${replace(file("${path.root}/../../../docs/environments/${var.environment_tag}.md"), "/(?m)^---[[:blank:]]*(#.*)?$/", "---")}\n"
    ) :
    yaml
  ]
  frontmatter        = local.yaml_file[1]
  environment_config = yamldecode(local.frontmatter)
  repo_config = jsondecode(file("${path.root}/../../../package.json"))
}

output "environment_config" {
  value = local.environment_config
}
output "repo_config" {
  value = local.repo_config
}

This would remove the need for nx-vars.tf, but to allow backwards compatibility we can switch from command line args to TF_VAR_ environmental variables because it would work with either approach.

Then instead of var.environment_tag or var.resource_group_id you would local.environment_config.resource_group_id

@emlyn-arkahna
Copy link
Collaborator

I like the idea of not passing the vars through anymore, this would allow us to exit the nx process more easily and increase readability.

Would it maybe be better to have nx generate a nx-locals.tf file during the run that isn't checked in, a bit like how backend.tf is generated.

@emlyn-arkahna
Copy link
Collaborator

This would mean that the parser can be in nx so you can cope with gotyas in the .md files

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants