terragrunt.hcl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # ---------------------------------------------------------------------------------------------------------------------
  2. # Global Variables and Terragrunt Configuration
  3. # ---------------------------------------------------------------------------------------------------------------------
  4. # This file takes care of the global variables. These are settings that should apply to ALL environments: prod, test,
  5. # *AND* common, across both partitions (govcloud and commercial)
  6. #
  7. # It also takes care of setting up:
  8. # The provider file
  9. # * A default provider for the account you're in
  10. # * A 'commercial' provider alias for the common services account in commercial
  11. # * A 'govcloud' provider alias for the common services account in govcloud
  12. # The backend file
  13. # *
  14. # ---------------------------------------------------------------------------------------------------------------------
  15. # Variables
  16. # ---------------------------------------------------------------------------------------------------------------------
  17. locals {
  18. # Globally ignore the checks for tfsec
  19. ignored_tfsec = [
  20. "aws-iam-no-policy-wildcards", # We use wildcards in policies
  21. "aws-lambda-enable-tracing", # We do not enable X-Ray Tracing for Lambda
  22. "aws-s3-enable-bucket-logging", # TODO: We do not currently log s3 access. We should. MSOCI-
  23. ]
  24. # Automatically load account-level variables
  25. account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
  26. # Automatically load region-level variables
  27. region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))
  28. # Automatically load partitiot-level variables
  29. partition_vars = read_terragrunt_config(find_in_parent_folders("partition.hcl"))
  30. # Automatically load environment-level variables
  31. environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
  32. # Automatically load global-level variables
  33. global_vars = read_terragrunt_config(find_in_parent_folders("globals.hcl"))
  34. # Generate our standard tags
  35. standard_tags = merge(
  36. local.global_vars.locals.global_tags,
  37. local.environment_vars.locals.environment_tags,
  38. local.partition_vars.locals.partition_tags,
  39. local.region_vars.locals.region_tags,
  40. local.account_vars.locals.account_tags
  41. )
  42. # Extract the variables we need for easy access
  43. account_name = local.account_vars.locals.account_name
  44. account_id = local.account_vars.locals.aws_account_id
  45. aws_region = local.region_vars.locals.aws_region
  46. aws_partition = local.partition_vars.locals.aws_partition
  47. common_services_account = local.partition_vars.locals.common_services_account
  48. legacy_account = local.environment_vars.locals.legacy_account
  49. tfstate_region = local.partition_vars.locals.tfstate_region
  50. common_profile = local.partition_vars.locals.common_profile
  51. # variables created here are available to *.hcl files in this hierarchy, but are not
  52. # automatically sent via inputs to the modules. Put global variables in global.hcl
  53. #
  54. # (Conversely, inputs are not automatically available to the hcl files)
  55. }
  56. # ---------------------------------------------------------------------------------------------------------------------
  57. # Apply Static Code Analysis
  58. # - 2022-04-23 - MSOCI-2143 - Comment out until ready
  59. # ---------------------------------------------------------------------------------------------------------------------
  60. # OS X Users, run:
  61. # brew install lint
  62. # brew install tfsec
  63. # brew install checkov
  64. # This can remain uncommented. The file won't hurt anything.
  65. generate "tflint_configuration" {
  66. path = "tflint.hcl"
  67. if_exists = "overwrite_terragrunt"
  68. contents = <<EOF
  69. plugin "aws" {
  70. enabled = true
  71. version = "0.13.3"
  72. source = "github.com/terraform-linters/tflint-ruleset-aws"
  73. deep_check = true
  74. }
  75. EOF
  76. }
  77. # uncomment the following to enable static code analysis
  78. #terraform {
  79. # before_hook "tflintinit" {
  80. # commands = ["plan", "apply"]
  81. # execute = [
  82. # "tflint",
  83. # "--config=tflint.hcl",
  84. # "--init"
  85. # ]
  86. # }
  87. #
  88. # before_hook "tflint" {
  89. # commands = ["plan", "apply"]
  90. # execute = [
  91. # "tflint",
  92. # "--config=tflint.hcl",
  93. # ".",
  94. # ]
  95. # }
  96. #
  97. # before_hook "tfsec" {
  98. # commands = ["plan", "apply"]
  99. # execute = [
  100. # "tfsec",
  101. # "--concise-output",
  102. # "--exclude", join(",", local.ignored_tfsec),
  103. # ]
  104. # }
  105. #
  106. ## before_hook "checkov" {
  107. ## commands = ["plan", "apply"]
  108. ## execute = [
  109. ## "checkov",
  110. ## "-d", ".",
  111. ## "--quiet",
  112. ## "--framework", "terraform",
  113. ## "--skip-check", "CKV_AWS_150", # We do not enable deletion protection for LBs
  114. ## ]
  115. ## }
  116. #}
  117. # ---------------------------------------------------------------------------------------------------------------------
  118. # Generate a required providers block
  119. # -- Allows override on a per-module basis
  120. # ---------------------------------------------------------------------------------------------------------------------
  121. generate "required_providers" {
  122. path = "required_provider.tf"
  123. if_exists = "overwrite_terragrunt"
  124. contents = <<EOF
  125. terraform {
  126. required_providers {
  127. aws = {
  128. source = "hashicorp/aws"
  129. version = "4.4.0" # 2022-03-08: upgrade from 3.63.0; 2021-09-21: upgrade from 3.37.0
  130. }
  131. vault = {
  132. source = "hashicorp/vault"
  133. version = "3.4.1" # 2022-04-08: upgrade from 2.19.1; 2021-04-29: upgrade from 2.18.0
  134. }
  135. sensu = {
  136. source = "jtopjian/sensu"
  137. version = "0.12.1" # 2022-04-06: upgrade from 0.10.5
  138. }
  139. }
  140. }
  141. EOF
  142. }
  143. # ---------------------------------------------------------------------------------------------------------------------
  144. # Generate an AWS provider block
  145. # ---------------------------------------------------------------------------------------------------------------------
  146. generate "provider" {
  147. path = "provider.tf"
  148. if_exists = "overwrite_terragrunt"
  149. contents = <<EOF
  150. provider "template" {
  151. }
  152. provider "aws" {
  153. region = "${local.aws_region}"
  154. assume_role {
  155. role_arn = "arn:${local.aws_partition}:iam::${local.account_id}:role/user/mdr_terraformer"
  156. session_name = "terraform-default"
  157. }
  158. profile = "${local.common_profile}"
  159. # Only these AWS Account IDs may be operated on by this template
  160. allowed_account_ids = ["${local.account_id}"]
  161. }
  162. # The "common" provider in the respective partition is always available
  163. provider "aws" {
  164. region = "${local.aws_region}"
  165. allowed_account_ids = [ "471284459109", "701290387780" ]
  166. profile = "${local.common_profile}"
  167. alias = "common"
  168. assume_role {
  169. role_arn = "arn:${local.aws_partition}:iam::${local.common_services_account}:role/user/mdr_terraformer"
  170. session_name = "terraform-common"
  171. }
  172. }
  173. # The "mdr-common-services-commercial" provider is used for public DNS entries
  174. provider "aws" {
  175. region = "us-east-1"
  176. allowed_account_ids = [ "471284459109" ]
  177. profile = "commercial"
  178. alias = "mdr-common-services-commercial"
  179. assume_role {
  180. role_arn = "arn:aws:iam::471284459109:role/user/mdr_terraformer"
  181. session_name = "terraform-mdr-common-services-commercial"
  182. }
  183. }
  184. # The "C2" provider, used for private DNS
  185. provider "aws" {
  186. region = "us-gov-east-1"
  187. allowed_account_ids = [ "721817724804", "738800754746" ]
  188. profile = "govcloud"
  189. alias = "c2"
  190. use_fips_endpoint = true
  191. assume_role {
  192. role_arn = "arn:aws-us-gov:iam::${local.environment_vars.locals.c2_accounts["aws-us-gov"]}:role/user/mdr_terraformer"
  193. session_name = "terraform-c2"
  194. }
  195. }
  196. EOF
  197. }
  198. # Configure Terragrunt to automatically store tfstate files in an S3 bucket
  199. remote_state {
  200. backend = "s3"
  201. generate = {
  202. path = "backend.tf"
  203. if_exists = "overwrite_terragrunt"
  204. }
  205. config = {
  206. bucket = local.global_vars.locals.remote_state_bucket
  207. # This key includes the terraform-0.12 directory name, which i don't like
  208. #key = "aws/${basename(get_parent_terragrunt_dir())}/${path_relative_to_include()}/terraform.tfstate"
  209. key = "aws/${path_relative_to_include()}/terraform.tfstate"
  210. region = "${local.tfstate_region}"
  211. encrypt = true
  212. dynamodb_table = "afsxdr-terraform-state"
  213. profile = "${local.common_profile}"
  214. role_arn = "arn:${local.aws_partition}:iam::${local.common_services_account}:role/user/mdr_terraformer"
  215. }
  216. }
  217. # ---------------------------------------------------------------------------------------------------------------------
  218. # GLOBAL PARAMETERS
  219. # These variables apply to all configurations in this subfolder. These are automatically merged into the child
  220. # `terragrunt.hcl` config via the include block.
  221. # ---------------------------------------------------------------------------------------------------------------------
  222. # Configure root level variables that all resources can inherit. This is especially helpful with multi-account configs
  223. # where terraform_remote_state data sources are placed directly into the modules.
  224. inputs = merge(
  225. local.global_vars.locals,
  226. local.environment_vars.locals,
  227. local.partition_vars.locals,
  228. local.region_vars.locals,
  229. local.account_vars.locals,
  230. { # Variables created in this file that we want to pass through
  231. standard_tags = local.standard_tags
  232. }
  233. )