Skip to content

Commit ddb37ac

Browse files
authored
feat: improve how to use resource_group in modules (#49)
* feat: improve how to use resource_group in modules * feat(instance): replace `cpu_core_count` and `cpu_threads_per_core` to `cpu_options`
1 parent cea1f20 commit ddb37ac

File tree

11 files changed

+123
-78
lines changed

11 files changed

+123
-78
lines changed

modules/instance/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ This module creates following resources.
2424

2525
| Name | Version |
2626
|------|---------|
27-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.91.0 |
28-
| <a name="provider_cloudinit"></a> [cloudinit](#provider\_cloudinit) | 2.3.6 |
27+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 6.12.0 |
28+
| <a name="provider_cloudinit"></a> [cloudinit](#provider\_cloudinit) | 2.3.7 |
2929

3030
## Modules
3131

3232
| Name | Source | Version |
3333
|------|--------|---------|
3434
| <a name="module_instance_profile"></a> [instance\_profile](#module\_instance\_profile) | tedilabs/account/aws//modules/iam-role | ~> 0.29.0 |
35-
| <a name="module_resource_group"></a> [resource\_group](#module\_resource\_group) | tedilabs/misc/aws//modules/resource-group | ~> 0.10.0 |
35+
| <a name="module_resource_group"></a> [resource\_group](#module\_resource\_group) | tedilabs/misc/aws//modules/resource-group | ~> 0.12.0 |
3636
| <a name="module_security_group"></a> [security\_group](#module\_security\_group) | tedilabs/network/aws//modules/security-group | ~> 0.32.0 |
3737

3838
## Resources
@@ -80,9 +80,7 @@ This module creates following resources.
8080
| <a name="input_placement_group"></a> [placement\_group](#input\_placement\_group) | (Optional) The name of the placement group to start the instance in, if applicable. Choose an instance type that supports enhanced networking to use `CLUSTER` placement group. You cannot launch Dedicated Hosts in placement groups. | `string` | `null` | no |
8181
| <a name="input_placement_group_partition"></a> [placement\_group\_partition](#input\_placement\_group\_partition) | (Optional) The index of the partition the instance is in. Valid only if the `placement_group` resource's strategy is set to `PARTITION`. | `number` | `null` | no |
8282
| <a name="input_private_ip"></a> [private\_ip](#input\_private\_ip) | (Optional) The primary private IPv4 address to associate with the instance. | `string` | `null` | no |
83-
| <a name="input_resource_group_description"></a> [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no |
84-
| <a name="input_resource_group_enabled"></a> [resource\_group\_enabled](#input\_resource\_group\_enabled) | (Optional) Whether to create Resource Group to find and group AWS resources which are created by this module. | `bool` | `true` | no |
85-
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | (Optional) The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. | `string` | `""` | no |
83+
| <a name="input_resource_group"></a> [resource\_group](#input\_resource\_group) | (Optional) A configurations of Resource Group for this module. `resource_group` as defined below.<br/> (Optional) `enabled` - Whether to create Resource Group to find and group AWS resources which are created by this module. Defaults to `true`.<br/> (Optional) `name` - The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. If not provided, a name will be generated using the module name and instance name.<br/> (Optional) `description` - The description of Resource Group. Defaults to `Managed by Terraform.`. | <pre>object({<br/> enabled = optional(bool, true)<br/> name = optional(string, "")<br/> description = optional(string, "Managed by Terraform.")<br/> })</pre> | `{}` | no |
8684
| <a name="input_root_volume"></a> [root\_volume](#input\_root\_volume) | (Optional) The configuration for root volume (root block device) of the instance. `root_volume` block as defined below.<br/> (Optional) `type` - The type of volume to attach.<br/> (Optional) `size` - The size of the volume, in GiB. If you are creating the volume from a snapshot, then the size of the volume can’t be smaller than the size of the snapshot.<br/> (Optional) `provisioned_iops` - The amount of provisioned IOPS. Only valid for type of `io1`, `io2` or `gp3`.<br/> (Optional) `provisioned_throughput` - Throughput to provision for a volume in mebibytes per second (MiB/s). This is only valid for type of `gp3`.<br/> (Optional) `encryption_enabled` - Whether to enable volume encryption. Defaults to `false`.<br/> (Optional) `encryption_kms_key` - The ARN(Amazon Resource Name) of the KMS Key to use when encrypting the volume.<br/> (Optional) `delete_on_termination` - Whether the volume should be destroyed on instance termination. Defaults to `true`.<br/> (Optional) `tags` - A map of tags to assign to the device. | `any` | `{}` | no |
8785
| <a name="input_secondary_private_ips"></a> [secondary\_private\_ips](#input\_secondary\_private\_ips) | (Optional) A list of secondary private IPv4 addresses to assign to the instance's primary network interface (eth0). Can only be assigned to the primary network interface (eth0) attached at instance creation, not a pre-existing network interface i.e., referenced in a network\_interface block. | `set(string)` | `null` | no |
8886
| <a name="input_security_groups"></a> [security\_groups](#input\_security\_groups) | (Optional) A list of security group IDs to assign to the instance. | `list(string)` | `[]` | no |
@@ -117,6 +115,7 @@ This module creates following resources.
117115
| <a name="output_metadata"></a> [metadata](#output\_metadata) | The configuration for metadata of the instance. |
118116
| <a name="output_name"></a> [name](#output\_name) | The name of the instance. |
119117
| <a name="output_network"></a> [network](#output\_network) | The network configuration for the instance.<br/> `availability_zone` - The Availability Zone of the instance.<br/> `subnet_id` - The ID of subnet of the launched instance.<br/> `source_dest_check_enabled` - Whether the traffic is routed to the instance when the destination address does not match the instance.<br/><br/> `public_ip` - The public IP address assigned to the instance, if applicable. NOTE: If you are using an aws\_eip with your instance, you should refer to the EIP's address directly and not use `public_ip` as this field will change after the EIP is attached.<br/> `private_ip` - The private IP address assigned to the instance.<br/> `secondary_private_ips` - A list of secondary private IPv4 addresses assigned to the instance's primary network interface.<br/><br/> `public_domain` - The public DNS name assigned to the instance. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC.<br/> `private_domain` - The private DNS name assigned to the instance. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC.<br/> `hostname_type` - The type of hostname for the EC2 instances.<br/> `dns_resource_name_ipv4_enabled` - Whether to resolve the IPv4 address of the EC2 instance for requests to your resource-name based domain.<br/> `dns_resource_name_ipv6_enabled` - Whether to resolve the IPv6 address of the EC2 instance for requests to your resource-name based domain. |
118+
| <a name="output_resource_group"></a> [resource\_group](#output\_resource\_group) | The resource group created to manage resources in this module. |
120119
| <a name="output_ssh_key"></a> [ssh\_key](#output\_ssh\_key) | The name of the SSH Key to access the instance. |
121120
| <a name="output_state"></a> [state](#output\_state) | The state of the instance. One of: `PENDING`, `RUNNING`, `STOPPING`, `STOPPED`, `SHUTTING_DOWN`, `TERMINATED`. |
122121
| <a name="output_storage"></a> [storage](#output\_storage) | The configuration of storage for the instance. |

modules/instance/main.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ resource "aws_instance" "this" {
116116

117117

118118
## CPU
119-
cpu_core_count = try(var.cpu_options.core_count, null)
120-
cpu_threads_per_core = try(var.cpu_options.threads_per_core, null)
119+
cpu_options {
120+
core_count = try(var.cpu_options.core_count, null)
121+
threads_per_core = try(var.cpu_options.threads_per_core, null)
122+
}
121123

122124
credit_specification {
123125
cpu_credits = (local.is_t_type && (var.cpu_credit_specification != null)
@@ -277,8 +279,10 @@ resource "aws_spot_instance_request" "this" {
277279

278280

279281
## CPU
280-
cpu_core_count = try(var.cpu_options.core_count, null)
281-
cpu_threads_per_core = try(var.cpu_options.threads_per_core, null)
282+
cpu_options {
283+
core_count = try(var.cpu_options.core_count, null)
284+
threads_per_core = try(var.cpu_options.threads_per_core, null)
285+
}
282286

283287
credit_specification {
284288
cpu_credits = (local.is_t_type && (var.cpu_credit_specification != null)

modules/instance/outputs.tf

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ output "cpu" {
146146
EOF
147147
value = {
148148
options = {
149-
core_count = local.instance.cpu_core_count
150-
threads_per_core = local.instance.cpu_threads_per_core
149+
core_count = one(local.instance.cpu_options[*].core_count)
150+
threads_per_core = one(local.instance.cpu_options[*].threads_per_core)
151151
}
152152
credit_specification = try(upper(local.instance.credit_specification[0].cpu_credits), null)
153153
}
@@ -236,3 +236,19 @@ output "zzz" {
236236
if !contains(["arn", "id", "availability_zone", "disable_api_stop", "disable_api_termination", "instance_state", "private_ip", "private_dns", "public_ip", "public_dns", "tags", "tags_all", "security_grouops", "cpu_core_count", "cpu_threads_per_core", "subnet_id", "timeouts", "credit_specification", "monitoring", "instance_initiated_shutdown_behavior", "maintenance_options", "placement_group", "placement_partition_number", "host_id", "tenancy", "key_name", "instance_type", "ami", "source_dest_check", "iam_instance_profile", "associate_public_ip_address", "ebs_optimized", "secondary_private_ips", "security_groups", "vpc_security_group_ids", "hibernation", "volume_tags", "enclave_options", "metadata_options", "launch_template", "private_dns_name_options", "root_block_device", "primary_network_interface_id", "ephemeral_block_device", "user_data_replace_on_change", "user_data", "user_data_base64"], k)
237237
}
238238
}
239+
240+
output "resource_group" {
241+
description = "The resource group created to manage resources in this module."
242+
value = merge(
243+
{
244+
enabled = var.resource_group.enabled && var.module_tags_enabled
245+
},
246+
(var.resource_group.enabled && var.module_tags_enabled
247+
? {
248+
arn = module.resource_group[0].arn
249+
name = module.resource_group[0].name
250+
}
251+
: {}
252+
)
253+
)
254+
}

modules/instance/resource-group.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
locals {
2-
resource_group_name = (var.resource_group_name != ""
3-
? var.resource_group_name
2+
resource_group_name = (var.resource_group.name != ""
3+
? var.resource_group.name
44
: join(".", [
55
local.metadata.package,
66
local.metadata.module,
@@ -12,12 +12,12 @@ locals {
1212

1313
module "resource_group" {
1414
source = "tedilabs/misc/aws//modules/resource-group"
15-
version = "~> 0.10.0"
15+
version = "~> 0.12.0"
1616

17-
count = (var.resource_group_enabled && var.module_tags_enabled) ? 1 : 0
17+
count = (var.resource_group.enabled && var.module_tags_enabled) ? 1 : 0
1818

1919
name = local.resource_group_name
20-
description = var.resource_group_description
20+
description = var.resource_group.description
2121

2222
query = {
2323
resource_tags = local.module_tags

modules/instance/variables.tf

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -454,23 +454,21 @@ variable "module_tags_enabled" {
454454
# Resource Group
455455
###################################################
456456

457-
variable "resource_group_enabled" {
458-
description = "(Optional) Whether to create Resource Group to find and group AWS resources which are created by this module."
459-
type = bool
460-
default = true
461-
nullable = false
462-
}
463457

464-
variable "resource_group_name" {
465-
description = "(Optional) The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`."
466-
type = string
467-
default = ""
468-
nullable = false
469-
}
470458

471-
variable "resource_group_description" {
472-
description = "(Optional) The description of Resource Group."
473-
type = string
474-
default = "Managed by Terraform."
475-
nullable = false
459+
460+
variable "resource_group" {
461+
description = <<EOF
462+
(Optional) A configurations of Resource Group for this module. `resource_group` as defined below.
463+
(Optional) `enabled` - Whether to create Resource Group to find and group AWS resources which are created by this module. Defaults to `true`.
464+
(Optional) `name` - The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. If not provided, a name will be generated using the module name and instance name.
465+
(Optional) `description` - The description of Resource Group. Defaults to `Managed by Terraform.`.
466+
EOF
467+
type = object({
468+
enabled = optional(bool, true)
469+
name = optional(string, "")
470+
description = optional(string, "Managed by Terraform.")
471+
})
472+
default = {}
473+
nullable = false
476474
}

modules/placement-group/outputs.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,19 @@ output "spread_level" {
2727
description = "The spread level to determine how the placement group spread instance. Only configured when the `strategy` is `SPREAD`."
2828
value = try(upper(aws_placement_group.this.spread_level), null)
2929
}
30+
31+
output "resource_group" {
32+
description = "The resource group created to manage resources in this module."
33+
value = merge(
34+
{
35+
enabled = var.resource_group.enabled && var.module_tags_enabled
36+
},
37+
(var.resource_group.enabled && var.module_tags_enabled
38+
? {
39+
arn = module.resource_group[0].arn
40+
name = module.resource_group[0].name
41+
}
42+
: {}
43+
)
44+
)
45+
}

modules/placement-group/resource-group.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
locals {
2-
resource_group_name = (var.resource_group_name != ""
3-
? var.resource_group_name
2+
resource_group_name = (var.resource_group.name != ""
3+
? var.resource_group.name
44
: join(".", [
55
local.metadata.package,
66
local.metadata.module,
@@ -12,12 +12,12 @@ locals {
1212

1313
module "resource_group" {
1414
source = "tedilabs/misc/aws//modules/resource-group"
15-
version = "~> 0.10.0"
15+
version = "~> 0.12.0"
1616

17-
count = (var.resource_group_enabled && var.module_tags_enabled) ? 1 : 0
17+
count = (var.resource_group.enabled && var.module_tags_enabled) ? 1 : 0
1818

1919
name = local.resource_group_name
20-
description = var.resource_group_description
20+
description = var.resource_group.description
2121

2222
query = {
2323
resource_tags = local.module_tags

modules/placement-group/variables.tf

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,21 @@ variable "module_tags_enabled" {
5757
# Resource Group
5858
###################################################
5959

60-
variable "resource_group_enabled" {
61-
description = "(Optional) Whether to create Resource Group to find and group AWS resources which are created by this module."
62-
type = bool
63-
default = true
64-
nullable = false
65-
}
6660

67-
variable "resource_group_name" {
68-
description = "(Optional) The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`."
69-
type = string
70-
default = ""
71-
nullable = false
72-
}
7361

74-
variable "resource_group_description" {
75-
description = "(Optional) The description of Resource Group."
76-
type = string
77-
default = "Managed by Terraform."
78-
nullable = false
62+
63+
variable "resource_group" {
64+
description = <<EOF
65+
(Optional) A configurations of Resource Group for this module. `resource_group` as defined below.
66+
(Optional) `enabled` - Whether to create Resource Group to find and group AWS resources which are created by this module. Defaults to `true`.
67+
(Optional) `name` - The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. If not provided, a name will be generated using the module name and instance name.
68+
(Optional) `description` - The description of Resource Group. Defaults to `Managed by Terraform.`.
69+
EOF
70+
type = object({
71+
enabled = optional(bool, true)
72+
name = optional(string, "")
73+
description = optional(string, "Managed by Terraform.")
74+
})
75+
default = {}
76+
nullable = false
7977
}

modules/ssh-key/outputs.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,19 @@ output "type" {
3535
# if !contains(["id", "key_name", "key_name_prefix", "arn", "key_pair_id", "fingerprint", "key_type", "public_key", "tags", "tags_all"], k)
3636
# }
3737
# }
38+
39+
output "resource_group" {
40+
description = "The resource group created to manage resources in this module."
41+
value = merge(
42+
{
43+
enabled = var.resource_group.enabled && var.module_tags_enabled
44+
},
45+
(var.resource_group.enabled && var.module_tags_enabled
46+
? {
47+
arn = module.resource_group[0].arn
48+
name = module.resource_group[0].name
49+
}
50+
: {}
51+
)
52+
)
53+
}

modules/ssh-key/resource-group.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
locals {
2-
resource_group_name = (var.resource_group_name != ""
3-
? var.resource_group_name
2+
resource_group_name = (var.resource_group.name != ""
3+
? var.resource_group.name
44
: join(".", [
55
local.metadata.package,
66
local.metadata.module,
@@ -12,12 +12,12 @@ locals {
1212

1313
module "resource_group" {
1414
source = "tedilabs/misc/aws//modules/resource-group"
15-
version = "~> 0.10.0"
15+
version = "~> 0.12.0"
1616

17-
count = (var.resource_group_enabled && var.module_tags_enabled) ? 1 : 0
17+
count = (var.resource_group.enabled && var.module_tags_enabled) ? 1 : 0
1818

1919
name = local.resource_group_name
20-
description = var.resource_group_description
20+
description = var.resource_group.description
2121

2222
query = {
2323
resource_tags = local.module_tags

0 commit comments

Comments
 (0)