|
| 1 | + |
| 2 | +locals { |
| 3 | + ## A map of the subnets and their tagging |
| 4 | + subnet_map = merge({ |
| 5 | + for k, v in aws_subnet.subnets : v.id => v.tags |
| 6 | + }) |
| 7 | +} |
| 8 | + |
| 9 | +## Provision the SSM parameter to store the JSON data |
| 10 | +resource "aws_ssm_parameter" "current" { |
| 11 | + name = format("%s/%s/%s", var.parameter_store_prefix, var.vpc_id, var.name) |
| 12 | + description = "Used to share resource related tags with other accounts" |
| 13 | + type = "String" |
| 14 | + value = jsonencode(local.subnet_map) |
| 15 | + tags = local.tags |
| 16 | +} |
| 17 | + |
| 18 | +## Provision the RAM share to distribute the SSM parameter |
| 19 | +resource "aws_ram_resource_share" "ssm_parameter_share" { |
| 20 | + allow_external_principals = false |
| 21 | + name = format("ssm-parameter-share-%s", var.name) |
| 22 | + tags = local.tags |
| 23 | +} |
| 24 | + |
| 25 | +## Associate the Parameter Store value with the RAM resource share |
| 26 | +resource "aws_ram_resource_association" "ssm_parameter_association" { |
| 27 | + resource_share_arn = aws_ram_resource_share.ssm_parameter_share.arn |
| 28 | + resource_arn = aws_ssm_parameter.current.arn |
| 29 | +} |
| 30 | + |
| 31 | +## Associate the principals with the RAM share |
| 32 | +resource "aws_ram_principal_association" "ssm_parameter_accounts" { |
| 33 | + for_each = toset(var.share.accounts) |
| 34 | + |
| 35 | + principal = each.value |
| 36 | + resource_share_arn = aws_ram_resource_share.this.arn |
| 37 | +} |
| 38 | + |
| 39 | +## Associate the principals with the RAM share |
| 40 | +resource "aws_ram_principal_association" "ssm_parameter_organizational_units" { |
| 41 | + for_each = toset(var.share.organizational_units) |
| 42 | + |
| 43 | + principal = each.value |
| 44 | + resource_share_arn = aws_ram_resource_share.this.arn |
| 45 | +} |
0 commit comments