terragrunt.hcl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. # Automatically load account-level variables
  19. account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
  20. # Automatically load region-level variables
  21. region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))
  22. # Automatically load partitiot-level variables
  23. partition_vars = read_terragrunt_config(find_in_parent_folders("partition.hcl"))
  24. # Automatically load environment-level variables
  25. environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
  26. # Automatically load global-level variables
  27. global_vars = read_terragrunt_config(find_in_parent_folders("globals.hcl"))
  28. # Generate our standard tags
  29. standard_tags = merge(
  30. local.global_vars.locals.global_tags,
  31. local.environment_vars.locals.environment_tags,
  32. local.partition_vars.locals.partition_tags,
  33. local.region_vars.locals.region_tags,
  34. local.account_vars.locals.account_tags
  35. )
  36. # Extract the variables we need for easy access
  37. account_name = local.account_vars.locals.account_name
  38. account_id = local.account_vars.locals.aws_account_id
  39. aws_region = local.region_vars.locals.aws_region
  40. aws_partition = local.partition_vars.locals.aws_partition
  41. common_services_account = local.partition_vars.locals.common_services_account
  42. legacy_account = local.environment_vars.locals.legacy_account
  43. tfstate_region = local.partition_vars.locals.tfstate_region
  44. common_profile = local.partition_vars.locals.common_profile
  45. # variables created here are available to *.hcl files in this hierarchy, but are not
  46. # automatically sent via inputs to the modules. Put global variables in global.hcl
  47. #
  48. # (Conversely, inputs are not automatically available to the hcl files)
  49. }
  50. # ---------------------------------------------------------------------------------------------------------------------
  51. # Generate a required providers block
  52. # -- Allows override on a per-module basis
  53. # ---------------------------------------------------------------------------------------------------------------------
  54. generate "required_providers" {
  55. path = "required_provider.tf"
  56. if_exists = "overwrite_terragrunt"
  57. contents = <<EOF
  58. terraform {
  59. required_providers {
  60. aws = {
  61. source = "hashicorp/aws"
  62. version = "= 3.37.0" # 2021-04-29: upgrade from 2.66
  63. }
  64. template = {
  65. source = "hashicorp/template"
  66. version = "= 2.2.0" # 2021-04-29: ugprade from 2.1.0
  67. }
  68. vault = {
  69. source = "hashicorp/vault"
  70. version = "= 2.19.1" # 2021-04-29: upgrade from 2.18.0
  71. }
  72. sensu = {
  73. source = "jtopjian/sensu"
  74. version = "= 0.10.5"
  75. }
  76. }
  77. }
  78. EOF
  79. }
  80. # ---------------------------------------------------------------------------------------------------------------------
  81. # Generate an AWS provider block
  82. # ---------------------------------------------------------------------------------------------------------------------
  83. generate "provider" {
  84. path = "provider.tf"
  85. if_exists = "overwrite_terragrunt"
  86. contents = <<EOF
  87. provider "template" {
  88. }
  89. provider "aws" {
  90. region = "${local.aws_region}"
  91. assume_role {
  92. role_arn = "arn:${local.aws_partition}:iam::${local.account_id}:role/user/mdr_terraformer"
  93. session_name = "terraform-default"
  94. }
  95. profile = "${local.common_profile}"
  96. # Only these AWS Account IDs may be operated on by this template
  97. allowed_account_ids = ["${local.account_id}"]
  98. }
  99. # The "common" provider in the respective partition is always available
  100. provider "aws" {
  101. region = "${local.aws_region}"
  102. allowed_account_ids = [ "471284459109", "701290387780" ]
  103. profile = "${local.common_profile}"
  104. alias = "common"
  105. assume_role {
  106. role_arn = "arn:${local.aws_partition}:iam::${local.common_services_account}:role/user/mdr_terraformer"
  107. session_name = "terraform-common"
  108. }
  109. }
  110. # The "mdr-common-services-commercial" provider is used for public DNS entries
  111. provider "aws" {
  112. region = "us-east-1"
  113. allowed_account_ids = [ "471284459109" ]
  114. profile = "commercial"
  115. alias = "mdr-common-services-commercial"
  116. assume_role {
  117. role_arn = "arn:aws:iam::471284459109:role/user/mdr_terraformer"
  118. session_name = "terraform-mdr-common-services-commercial"
  119. }
  120. }
  121. # The "C2" provider, used for private DNS
  122. provider "aws" {
  123. region = "us-gov-east-1"
  124. allowed_account_ids = [ "721817724804", "738800754746" ]
  125. profile = "govcloud"
  126. alias = "c2"
  127. assume_role {
  128. role_arn = "arn:aws-us-gov:iam::${ local.environment_vars.locals.c2_accounts["aws-us-gov"] }:role/user/mdr_terraformer"
  129. session_name = "terraform-c2"
  130. }
  131. }
  132. EOF
  133. }
  134. # Configure Terragrunt to automatically store tfstate files in an S3 bucket
  135. remote_state {
  136. backend = "s3"
  137. generate = {
  138. path = "backend.tf"
  139. if_exists = "overwrite_terragrunt"
  140. }
  141. config = {
  142. bucket = local.global_vars.locals.remote_state_bucket
  143. # This key includes the terraform-0.12 directory name, which i don't like
  144. #key = "aws/${basename(get_parent_terragrunt_dir())}/${path_relative_to_include()}/terraform.tfstate"
  145. key = "aws/${path_relative_to_include()}/terraform.tfstate"
  146. region = "${local.tfstate_region}"
  147. encrypt = true
  148. dynamodb_table = "afsxdr-terraform-state"
  149. profile = "${local.common_profile}"
  150. role_arn = "arn:${local.aws_partition}:iam::${local.common_services_account}:role/user/mdr_terraformer"
  151. }
  152. }
  153. # ---------------------------------------------------------------------------------------------------------------------
  154. # GLOBAL PARAMETERS
  155. # These variables apply to all configurations in this subfolder. These are automatically merged into the child
  156. # `terragrunt.hcl` config via the include block.
  157. # ---------------------------------------------------------------------------------------------------------------------
  158. # Configure root level variables that all resources can inherit. This is especially helpful with multi-account configs
  159. # where terraform_remote_state data sources are placed directly into the modules.
  160. inputs = merge(
  161. local.global_vars.locals,
  162. local.environment_vars.locals,
  163. local.partition_vars.locals,
  164. local.region_vars.locals,
  165. local.account_vars.locals,
  166. { # Variables created in this file that we want to pass through
  167. standard_tags = local.standard_tags
  168. }
  169. )