Skip to content

Commit aea7f65

Browse files
docs: add example to create dedicate hosts and host group
1 parent 571ee7c commit aea7f65

File tree

6 files changed

+203
-0
lines changed

6 files changed

+203
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
## Requirements
3+
4+
| Name | Version |
5+
|------|---------|
6+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.11 |
7+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 6.0 |
8+
9+
## Providers
10+
11+
| Name | Version |
12+
|------|---------|
13+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 6.27.0 |
14+
15+
## Modules
16+
17+
No modules.
18+
19+
## Resources
20+
21+
| Name | Type |
22+
|------|------|
23+
| [aws_ec2_host.mac_dedicated_host](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ec2_host) | resource |
24+
| [aws_licensemanager_license_configuration.mac_dedicated_host_license_configuration](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/licensemanager_license_configuration) | resource |
25+
| [aws_resourcegroups_group.mac_host_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/resourcegroups_group) | resource |
26+
| [aws_resourcegroups_resource.mac_host_membership](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/resourcegroups_resource) | resource |
27+
28+
## Inputs
29+
30+
| Name | Description | Type | Default | Required |
31+
|------|-------------|------|---------|:--------:|
32+
| <a name="input_aws_profile"></a> [aws\_profile](#input\_aws\_profile) | AWS profile (i.e., generated via 'sl aws session generate') to use. | `string` | n/a | yes |
33+
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | Default AWS region. | `string` | n/a | yes |
34+
| <a name="input_default_tags"></a> [default\_tags](#input\_default\_tags) | A map of tags to apply to resources. | `map(string)` | n/a | yes |
35+
| <a name="input_host_groups"></a> [host\_groups](#input\_host\_groups) | Map of host groups, each with a name, host instance type, and a list of hosts (name + AZ). | <pre>map(object({<br/> name = string<br/> host_instance_type = string<br/> hosts = list(object({<br/> name = string<br/> availability_zone = string<br/> }))<br/> }))</pre> | n/a | yes |
36+
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to apply to resources. | `map(string)` | n/a | yes |
37+
38+
## Outputs
39+
40+
| Name | Description |
41+
|------|-------------|
42+
| <a name="output_license_specification_arn"></a> [license\_specification\_arn](#output\_license\_specification\_arn) | ARN of the License Manager configuration used for Mac dedicated hosts. |
43+
| <a name="output_resource_group_arns"></a> [resource\_group\_arns](#output\_resource\_group\_arns) | Map of resource group names to their ARNs. |
44+
<!-- END_TF_DOCS -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
output "resource_group_arns" {
2+
description = "Map of resource group names to their ARNs."
3+
value = {
4+
for k, rg in aws_resourcegroups_group.mac_host_group :
5+
rg.name => rg.arn
6+
}
7+
}
8+
9+
output "license_specification_arn" {
10+
description = "ARN of the License Manager configuration used for Mac dedicated hosts."
11+
value = aws_licensemanager_license_configuration.mac_dedicated_host_license_configuration.arn
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
provider "aws" {
2+
region = local.aws_region
3+
4+
default_tags {
5+
tags = {
6+
Example = local.environment
7+
}
8+
}
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
variable "aws_region" {
2+
description = "AWS region."
3+
type = string
4+
}
5+
6+
variable "environment" {
7+
description = "Environment name, used as prefix."
8+
9+
type = string
10+
default = null
11+
}
12+
13+
variable "host_groups" {
14+
description = "Map of host groups, each with a name, host instance type, and a list of hosts (name + AZ)."
15+
type = map(object({
16+
name = string
17+
host_instance_type = string
18+
hosts = list(object({
19+
name = string
20+
availability_zone = string
21+
}))
22+
}))
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = ">= 6.21"
6+
}
7+
}
8+
9+
required_version = ">= 1.3.0"
10+
}

0 commit comments

Comments
 (0)