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.15.0"
  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. before_hook "tflint" {
  80. commands = ["plan", "apply"]
  81. execute = [
  82. "tflint",
  83. "--config=tflint.hcl",
  84. ".",
  85. ]
  86. }
  87. before_hook "tfsec" {
  88. commands = ["plan", "apply"]
  89. execute = [
  90. "tfsec",
  91. "--concise-output",
  92. "--exclude", join(",", local.ignored_tfsec),
  93. ]
  94. }
  95. # before_hook "checkov" {
  96. # commands = ["plan", "apply"]
  97. # execute = [
  98. # "checkov",
  99. # "-d", ".",
  100. # "--quiet",
  101. # "--framework", "terraform",
  102. # "--skip-check", "CKV_AWS_150", # We do not enable deletion protection for LBs
  103. # ]
  104. # }
  105. }
  106. # ---------------------------------------------------------------------------------------------------------------------
  107. # Generate a required providers block
  108. # -- Allows override on a per-module basis
  109. # ---------------------------------------------------------------------------------------------------------------------
  110. generate "required_providers" {
  111. path = "required_provider.tf"
  112. if_exists = "overwrite_terragrunt"
  113. contents = <<EOF
  114. terraform {
  115. required_providers {
  116. aws = {
  117. source = "hashicorp/aws"
  118. version = "4.22.0" # 2022-07-21: Upgrade from 4.4.0; 2022-03-08: upgrade from 3.63.0
  119. }
  120. vault = {
  121. source = "hashicorp/vault"
  122. version = "3.8.0" # 2022-07-28: upgrade from 3.4.1; 2022-04-08: upgrade from 2.19.1
  123. }
  124. sensu = {
  125. source = "jtopjian/sensu"
  126. version = "0.12.1" # 2022-04-06: upgrade from 0.10.5
  127. }
  128. }
  129. }
  130. EOF
  131. }
  132. # ---------------------------------------------------------------------------------------------------------------------
  133. # Generate an AWS provider block
  134. # ---------------------------------------------------------------------------------------------------------------------
  135. generate "provider" {
  136. path = "provider.tf"
  137. if_exists = "overwrite_terragrunt"
  138. contents = <<EOF
  139. provider "template" {
  140. }
  141. provider "aws" {
  142. region = "${local.aws_region}"
  143. assume_role {
  144. role_arn = "arn:${local.aws_partition}:iam::${local.account_id}:role/user/mdr_terraformer"
  145. session_name = "terraform-default"
  146. }
  147. profile = "${local.common_profile}"
  148. # Only these AWS Account IDs may be operated on by this template
  149. allowed_account_ids = ["${local.account_id}"]
  150. }
  151. # The "common" provider in the respective partition is always available
  152. provider "aws" {
  153. region = "${local.aws_region}"
  154. allowed_account_ids = [ "471284459109", "701290387780" ]
  155. profile = "${local.common_profile}"
  156. alias = "common"
  157. assume_role {
  158. role_arn = "arn:${local.aws_partition}:iam::${local.common_services_account}:role/user/mdr_terraformer"
  159. session_name = "terraform-common"
  160. }
  161. }
  162. # The "mdr-common-services-commercial" provider is used for public DNS entries
  163. provider "aws" {
  164. region = "us-east-1"
  165. allowed_account_ids = [ "471284459109" ]
  166. profile = "commercial"
  167. alias = "mdr-common-services-commercial"
  168. assume_role {
  169. role_arn = "arn:aws:iam::471284459109:role/user/mdr_terraformer"
  170. session_name = "terraform-mdr-common-services-commercial"
  171. }
  172. }
  173. # The "C2" provider, used for private DNS
  174. provider "aws" {
  175. region = "us-gov-east-1"
  176. allowed_account_ids = [ "721817724804", "738800754746" ]
  177. profile = "govcloud"
  178. alias = "c2"
  179. use_fips_endpoint = true
  180. assume_role {
  181. role_arn = "arn:aws-us-gov:iam::${local.environment_vars.locals.c2_accounts["aws-us-gov"]}:role/user/mdr_terraformer"
  182. session_name = "terraform-c2"
  183. }
  184. }
  185. EOF
  186. }
  187. # Configure Terragrunt to automatically store tfstate files in an S3 bucket
  188. remote_state {
  189. backend = "s3"
  190. generate = {
  191. path = "backend.tf"
  192. if_exists = "overwrite_terragrunt"
  193. }
  194. config = {
  195. bucket = local.global_vars.locals.remote_state_bucket
  196. # This key includes the terraform-0.12 directory name, which i don't like
  197. #key = "aws/${basename(get_parent_terragrunt_dir())}/${path_relative_to_include()}/terraform.tfstate"
  198. key = "aws/${path_relative_to_include()}/terraform.tfstate"
  199. region = "${local.tfstate_region}"
  200. encrypt = true
  201. dynamodb_table = "afsxdr-terraform-state"
  202. profile = "${local.common_profile}"
  203. role_arn = "arn:${local.aws_partition}:iam::${local.common_services_account}:role/user/mdr_terraformer"
  204. }
  205. }
  206. # ---------------------------------------------------------------------------------------------------------------------
  207. # GLOBAL PARAMETERS
  208. # These variables apply to all configurations in this subfolder. These are automatically merged into the child
  209. # `terragrunt.hcl` config via the include block.
  210. # ---------------------------------------------------------------------------------------------------------------------
  211. # Configure root level variables that all resources can inherit. This is especially helpful with multi-account configs
  212. # where terraform_remote_state data sources are placed directly into the modules.
  213. inputs = merge(
  214. local.global_vars.locals,
  215. local.environment_vars.locals,
  216. local.partition_vars.locals,
  217. local.region_vars.locals,
  218. local.account_vars.locals,
  219. )