terragrunt.hcl 8.7 KB

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