terragrunt.hcl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. # Extract the variables we need for easy access
  29. account_name = local.account_vars.locals.account_name
  30. account_id = local.account_vars.locals.aws_account_id
  31. aws_region = local.region_vars.locals.aws_region
  32. aws_partition = local.partition_vars.locals.aws_partition
  33. common_services_account = local.partition_vars.locals.common_services_account
  34. common_profile = local.partition_vars.locals.common_profile
  35. # variables created here are available to *.hcl files in this hierarchy, but are not
  36. # automatically sent via inputs to the modules. Put global variables in global.hcl
  37. #
  38. # (Conversely, inputs are not automatically available to the hcl files)
  39. }
  40. # ---------------------------------------------------------------------------------------------------------------------
  41. # Generate an AWS provider block
  42. # ---------------------------------------------------------------------------------------------------------------------
  43. generate "provider" {
  44. path = "provider.tf"
  45. if_exists = "overwrite_terragrunt"
  46. contents = <<EOF
  47. provider "aws" {
  48. version = "~> 2.66"
  49. region = "${local.aws_region}"
  50. assume_role {
  51. role_arn = "arn:${local.aws_partition}:iam::${local.account_id}:role/user/mdr_terraformer"
  52. session_name = "terraform"
  53. }
  54. profile = "${local.common_profile}"
  55. # Only these AWS Account IDs may be operated on by this template
  56. allowed_account_ids = ["${local.account_id}"]
  57. }
  58. # The "common services" provider in the respective partition is always available
  59. provider "aws" {
  60. region = "${local.aws_region}"
  61. allowed_account_ids = [ "471284459109", "701290387780" ]
  62. profile = "${local.common_profile}"
  63. alias = "common"
  64. }
  65. EOF
  66. }
  67. # Configure Terragrunt to automatically store tfstate files in an S3 bucket
  68. # We'll want to reenable this when we have valid settings
  69. remote_state {
  70. backend = "s3"
  71. generate = {
  72. path = "backend.tf"
  73. if_exists = "overwrite_terragrunt"
  74. }
  75. config = {
  76. bucket = local.global_vars.locals.remote_state_bucket
  77. # This key includes the terraform-0.12 directory name, which i don't like
  78. #key = "aws/${basename(get_parent_terragrunt_dir())}/${path_relative_to_include()}/terraform.tfstate"
  79. key = "aws/${path_relative_to_include()}/terraform.tfstate"
  80. region = "${local.aws_region}"
  81. encrypt = true
  82. dynamodb_table = "afsxdr-terraform-state"
  83. profile = "${local.common_profile}"
  84. role_arn = "arn:${local.aws_partition}:iam::${local.common_services_account}:role/user/mdr_terraformer"
  85. }
  86. }
  87. # ---------------------------------------------------------------------------------------------------------------------
  88. # GLOBAL PARAMETERS
  89. # These variables apply to all configurations in this subfolder. These are automatically merged into the child
  90. # `terragrunt.hcl` config via the include block.
  91. # ---------------------------------------------------------------------------------------------------------------------
  92. # Configure root level variables that all resources can inherit. This is especially helpful with multi-account configs
  93. # where terraform_remote_state data sources are placed directly into the modules.
  94. inputs = merge(
  95. local.account_vars.locals,
  96. local.region_vars.locals,
  97. local.partition_vars.locals,
  98. local.environment_vars.locals,
  99. local.global_vars.locals,
  100. )