# modules This directory contains modules that are included from the environment directories. Modules called from other modules should be placed under the `submodules` subdirectory. Modules called from other modules should be placed under the `thirdparty` subdirectory. ## Variables Added 2022-06-10 Variables that affect configuration and are subject to change exist with the modules. There are a few important things to know: 1. If the variable only affects the module itself, just put it in the module directory. 2. If the variable is used by more than one module, put it in the `variables/` directory and create symbolic links into the modules that need it. 3. If a variable is added to `xdr-terraform-live`, it should be one that is set once and never changes. These variables can be found in the `constants.tf` file under `variables/`, and is linked into all modules. 4. When creating a new module, be sure to use `cp -a` instead of `cp -r`. `cp -a` will preserve symbolic links. ## Variable Differentiation Since we still want different settings base on things like environment or account, there are a few "tropes" used to provide this differentiation. From easiest to most complex, the 3 methods most often employed are as follows. Note that in these examples, all 3 produce the same result: ### 1. Directly during assignment This is straightforward: e.g.: ``` instance_type = var.environment == "prod" ? "m5a.xlarge" : "t3a.micro" ``` ### 2. Map with differentiator This method creates a map of values, and selects the appropriate one immediately. This is good for things that will always have a value. e.g.: ``` locals { instance_type = { prod = "m5a.xlarge", test = "t3a.micro", }[var.environment] } ``` ### 3. Default with Exceptions This method sets a default value, creates a map of exceptions, and then does a `lookup` to determine the final value. This is useful when the same value will be used most of the time, but it can be overridden in certain cases. e.g. ``` locals { instance_type_default = "m5a.xlarge" instance_type_exceptions = { afs-mdr-test-c2-gov = "t3a.micro" afs-mdr-test-malware-gov = "t3a.micro" } instance_type = lookup(local.instance_type_exceptions, var.account_name, local.instance_type_default } ```