|
| 1 | +locals { |
| 2 | + |
| 3 | + environment = var.environment != null ? var.environment : "default" |
| 4 | + aws_region = var.aws_region |
| 5 | + |
| 6 | + # Flatten host_groups into a map of individual host definitions keyed by |
| 7 | + # "groupKey-hostName" so we can create one aws_ec2_host per host. |
| 8 | + mac_dedicated_hosts = merge([ |
| 9 | + for group_key, group in var.host_groups : { |
| 10 | + for host in group.hosts : |
| 11 | + "${group_key}-${host.name}" => { |
| 12 | + instance_type = group.host_instance_type |
| 13 | + availability_zone = host.availability_zone |
| 14 | + group_name = group.name |
| 15 | + host_name = host.name |
| 16 | + } |
| 17 | + } |
| 18 | + ]...) |
| 19 | +} |
| 20 | + |
| 21 | +resource "aws_ec2_host" "mac_dedicated_host" { |
| 22 | + for_each = local.mac_dedicated_hosts |
| 23 | + |
| 24 | + instance_type = each.value.instance_type |
| 25 | + availability_zone = each.value.availability_zone |
| 26 | + auto_placement = "on" |
| 27 | + |
| 28 | + tags = { |
| 29 | + "Name" = each.value.host_name |
| 30 | + "HostGroup" = each.value.group_name |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +resource "aws_resourcegroups_group" "mac_host_group" { |
| 35 | + for_each = { for _, group in var.host_groups : group.name => group } |
| 36 | + |
| 37 | + name = each.value.name |
| 38 | + |
| 39 | + configuration { |
| 40 | + type = "AWS::EC2::HostManagement" |
| 41 | + |
| 42 | + parameters { |
| 43 | + name = "any-host-based-license-configuration" |
| 44 | + values = ["true"] |
| 45 | + } |
| 46 | + |
| 47 | + parameters { |
| 48 | + name = "auto-allocate-host" |
| 49 | + values = [ |
| 50 | + "false", |
| 51 | + ] |
| 52 | + } |
| 53 | + parameters { |
| 54 | + name = "auto-host-recovery" |
| 55 | + values = [ |
| 56 | + "false", |
| 57 | + ] |
| 58 | + } |
| 59 | + parameters { |
| 60 | + name = "auto-release-host" |
| 61 | + values = [ |
| 62 | + "false", |
| 63 | + ] |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + configuration { |
| 68 | + type = "AWS::ResourceGroups::Generic" |
| 69 | + parameters { |
| 70 | + name = "allowed-resource-types" |
| 71 | + values = [ |
| 72 | + "AWS::EC2::Host", |
| 73 | + ] |
| 74 | + } |
| 75 | + |
| 76 | + parameters { |
| 77 | + name = "deletion-protection" |
| 78 | + values = [ |
| 79 | + "UNLESS_EMPTY", |
| 80 | + ] |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + tags = { |
| 85 | + "Name" = each.value.name |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +resource "aws_resourcegroups_resource" "mac_host_membership" { |
| 90 | + for_each = local.mac_dedicated_hosts |
| 91 | + |
| 92 | + group_arn = aws_resourcegroups_group.mac_host_group[each.value.group_name].arn |
| 93 | + resource_arn = aws_ec2_host.mac_dedicated_host[each.key].arn |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +resource "aws_licensemanager_license_configuration" "mac_dedicated_host_license_configuration" { |
| 98 | + name = "mac-dedicated-host-license-configuration" |
| 99 | + description = "Mac dedicated host license configuration" |
| 100 | + license_counting_type = "Socket" |
| 101 | + |
| 102 | + tags = { |
| 103 | + "Name" = each.value.name |
| 104 | + } |
| 105 | +} |
0 commit comments