Skip to content

Commit 0ee961c

Browse files
authored
add cpu_options to launch template (#159)
* add cpu_options to launch template * calculate cpu_count * minor improvements * terraform fmt
1 parent fe88a94 commit 0ee961c

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ All those keys are required to be there so if the alarm you are setting does not
199199
| <a name="input_block_device_mappings"></a> [block\_device\_mappings](#input\_block\_device\_mappings) | Specify volumes to attach to the instance besides the volumes specified by the AMI | <pre>list(object({<br/> device_name = optional(string)<br/> no_device = optional(bool)<br/> virtual_name = optional(string)<br/> ebs = object({<br/> delete_on_termination = optional(bool)<br/> encrypted = optional(bool)<br/> iops = optional(number)<br/> throughput = optional(number)<br/> kms_key_id = optional(string)<br/> snapshot_id = optional(string)<br/> volume_size = optional(number)<br/> volume_type = optional(string)<br/> })<br/> }))</pre> | `[]` | no |
200200
| <a name="input_capacity_rebalance"></a> [capacity\_rebalance](#input\_capacity\_rebalance) | Indicates whether capacity rebalance is enabled. Otherwise, capacity rebalance is disabled. | `bool` | `false` | no |
201201
| <a name="input_context"></a> [context](#input\_context) | Single object for setting entire context at once.<br/>See description of individual variables for details.<br/>Leave string and numeric variables as `null` to use default value.<br/>Individual variable settings (non-null) override settings in context object,<br/>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br/> "additional_tag_map": {},<br/> "attributes": [],<br/> "delimiter": null,<br/> "descriptor_formats": {},<br/> "enabled": true,<br/> "environment": null,<br/> "id_length_limit": null,<br/> "label_key_case": null,<br/> "label_order": [],<br/> "label_value_case": null,<br/> "labels_as_tags": [<br/> "unset"<br/> ],<br/> "name": null,<br/> "namespace": null,<br/> "regex_replace_chars": null,<br/> "stage": null,<br/> "tags": {},<br/> "tenant": null<br/>}</pre> | no |
202+
| <a name="input_cpu_options"></a> [cpu\_options](#input\_cpu\_options) | The CPU options for the instance | <pre>object({<br/> amd_sev_snp_enabled = optional(bool, null)<br/> core_count = optional(number, null)<br/> threads_per_core = optional(number, null)<br/><br/> })</pre> | `null` | no |
202203
| <a name="input_cpu_utilization_high_evaluation_periods"></a> [cpu\_utilization\_high\_evaluation\_periods](#input\_cpu\_utilization\_high\_evaluation\_periods) | The number of periods over which data is compared to the specified threshold | `number` | `2` | no |
203204
| <a name="input_cpu_utilization_high_period_seconds"></a> [cpu\_utilization\_high\_period\_seconds](#input\_cpu\_utilization\_high\_period\_seconds) | The period in seconds over which the specified statistic is applied | `number` | `300` | no |
204205
| <a name="input_cpu_utilization_high_statistic"></a> [cpu\_utilization\_high\_statistic](#input\_cpu\_utilization\_high\_statistic) | The statistic to apply to the alarm's associated metric. Either of the following is supported: `SampleCount`, `Average`, `Sum`, `Minimum`, `Maximum` | `string` | `"Average"` | no |

main.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ data "aws_subnet" "this" {
33
id = each.value
44
}
55

6+
data "aws_ec2_instance_type" "default" {
7+
count = (
8+
var.cpu_options != null && var.cpu_options.threads_per_core != null && var.cpu_options.core_count == null ? 1 : 0
9+
)
10+
11+
instance_type = var.instance_type
12+
}
13+
614
resource "aws_launch_template" "default" {
715
count = module.this.enabled ? 1 : 0
816

@@ -87,6 +95,22 @@ resource "aws_launch_template" "default" {
8795
}
8896
}
8997

98+
dynamic "cpu_options" {
99+
for_each = var.cpu_options != null ? [var.cpu_options] : []
100+
content {
101+
amd_sev_snp = cpu_options.value.amd_sev_snp_enabled != null ? (cpu_options.value.amd_sev_snp_enabled ? "enabled" : "disabled") : null
102+
103+
# if threads_per_core is set and core_count is not set, use the default cores from the instance type
104+
core_count = (
105+
cpu_options.value.core_count != null ? cpu_options.value.core_count : (
106+
cpu_options.value.threads_per_core == null ? null : one(data.aws_ec2_instance_type.default[*].default_cores)
107+
)
108+
)
109+
110+
threads_per_core = lookup(cpu_options.value, "threads_per_core", null)
111+
}
112+
}
113+
90114
monitoring {
91115
enabled = var.enable_monitoring
92116
}

variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,14 @@ variable "instance_reuse_policy" {
515515
description = "If warm pool and this block are configured, instances in the Auto Scaling group can be returned to the warm pool on scale in. The default is to terminate instances in the Auto Scaling group when the group scales in."
516516
default = null
517517
}
518+
519+
variable "cpu_options" {
520+
type = object({
521+
amd_sev_snp_enabled = optional(bool, null)
522+
core_count = optional(number, null)
523+
threads_per_core = optional(number, null)
524+
525+
})
526+
description = "The CPU options for the instance"
527+
default = null
528+
}

0 commit comments

Comments
 (0)