main.tf 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. locals {
  2. # We want to share with:
  3. # * The other accounts in our partition and environment
  4. # * The common accounts
  5. # * But not ourselves
  6. remote_accounts = toset([
  7. for account in concat(local.account_map[var.environment], local.account_map["common"]) :
  8. account if account != var.aws_account_id
  9. ])
  10. }
  11. data "aws_availability_zones" "available" {
  12. state = "available"
  13. }
  14. resource "aws_ec2_transit_gateway" "tgw" {
  15. description = "Transit gateway for ${var.environment} in ${var.aws_partition}."
  16. amazon_side_asn = var.asn # may not need, but AWS recommends it for future proofing
  17. auto_accept_shared_attachments = "enable" # if we grant them access, they can attach.
  18. default_route_table_association = "enable"
  19. default_route_table_propagation = "enable"
  20. dns_support = "enable"
  21. tags = merge(
  22. { "Name" = var.name },
  23. var.tags,
  24. local.standard_tags)
  25. }
  26. # We require a RAM to share the resource
  27. resource "aws_ram_resource_share" "share_tgw" {
  28. name = var.name
  29. allow_external_principals = true # IMPORTANT
  30. tags = merge(
  31. { "Name" = var.name },
  32. var.tags,
  33. local.standard_tags
  34. )
  35. }
  36. # Share the tgw
  37. resource "aws_ram_resource_association" "share_tgw" {
  38. resource_arn = aws_ec2_transit_gateway.tgw.arn
  39. resource_share_arn = aws_ram_resource_share.share_tgw.id
  40. }
  41. # ... with each other account
  42. resource "aws_ram_principal_association" "share_with_accounts" {
  43. for_each = local.remote_accounts
  44. principal = each.value
  45. resource_share_arn = aws_ram_resource_share.share_tgw.id
  46. }