Skip to content

Commit 19b44d2

Browse files
authored
feat: Add cpu optimization options (#181)
1 parent ae09a00 commit 19b44d2

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ No modules.
109109
|------|-------------|------|---------|:--------:|
110110
| <a name="input_ami"></a> [ami](#input\_ami) | ID of AMI to use for the instance | `string` | n/a | yes |
111111
| <a name="input_associate_public_ip_address"></a> [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | If true, the EC2 instance will have associated public IP address | `bool` | `null` | no |
112+
| <a name="input_cpu_core_count"></a> [cpu\_core\_count](#input\_cpu\_core\_count) | Sets the number of CPU cores for an instance. | `number` | `null` | no |
112113
| <a name="input_cpu_credits"></a> [cpu\_credits](#input\_cpu\_credits) | The credit option for CPU usage (unlimited or standard) | `string` | `"standard"` | no |
114+
| <a name="input_cpu_threads_per_core"></a> [cpu\_threads\_per\_core](#input\_cpu\_threads\_per\_core) | Sets the number of CPU threads per core for an instance (has no effect unless cpu\_core\_count is also set). | `number` | `null` | no |
113115
| <a name="input_disable_api_termination"></a> [disable\_api\_termination](#input\_disable\_api\_termination) | If true, enables EC2 Instance Termination Protection | `bool` | `false` | no |
114116
| <a name="input_ebs_block_device"></a> [ebs\_block\_device](#input\_ebs\_block\_device) | Additional EBS block devices to attach to the instance | `list(map(string))` | `[]` | no |
115117
| <a name="input_ebs_optimized"></a> [ebs\_optimized](#input\_ebs\_optimized) | If true, the launched EC2 instance will be EBS-optimized | `bool` | `false` | no |

examples/basic/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Note that this example may create resources which can cost money. Run `terraform
3333
| Name | Source | Version |
3434
|------|--------|---------|
3535
| <a name="module_ec2"></a> [ec2](#module\_ec2) | ../../ | |
36+
| <a name="module_ec2_optimize_cpu"></a> [ec2\_optimize\_cpu](#module\_ec2\_optimize\_cpu) | ../../ | |
3637
| <a name="module_ec2_with_metadata_options"></a> [ec2\_with\_metadata\_options](#module\_ec2\_with\_metadata\_options) | ../../ | |
3738
| <a name="module_ec2_with_network_interface"></a> [ec2\_with\_network\_interface](#module\_ec2\_with\_network\_interface) | ../../ | |
3839
| <a name="module_ec2_with_t2_unlimited"></a> [ec2\_with\_t2\_unlimited](#module\_ec2\_with\_t2\_unlimited) | ../../ | |

examples/basic/main.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,34 @@ module "ec2_zero" {
196196
subnet_id = tolist(data.aws_subnet_ids.all.ids)[0]
197197
vpc_security_group_ids = [module.security_group.security_group_id]
198198
}
199+
200+
module "ec2_optimize_cpu" {
201+
source = "../../"
202+
203+
instance_count = 1
204+
205+
name = "example-optimize-cpu"
206+
ami = data.aws_ami.amazon_linux.id
207+
instance_type = "c4.2xlarge"
208+
subnet_id = tolist(data.aws_subnet_ids.all.ids)[0]
209+
210+
vpc_security_group_ids = [module.security_group.security_group_id]
211+
associate_public_ip_address = true
212+
placement_group = aws_placement_group.web.id
213+
214+
user_data_base64 = base64encode(local.user_data)
215+
216+
root_block_device = [
217+
{
218+
volume_type = "gp2"
219+
volume_size = 10
220+
},
221+
]
222+
cpu_core_count = 2 # default 4
223+
cpu_threads_per_core = 1 # default 2
224+
225+
tags = {
226+
"Env" = "Private"
227+
"Location" = "Secret"
228+
}
229+
}

main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ resource "aws_instance" "this" {
8585
instance_initiated_shutdown_behavior = var.instance_initiated_shutdown_behavior
8686
placement_group = var.placement_group
8787
tenancy = var.tenancy
88+
cpu_core_count = var.cpu_core_count
89+
cpu_threads_per_core = var.cpu_threads_per_core
8890

8991
tags = merge(
9092
{

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,15 @@ variable "num_suffix_format" {
204204
type = string
205205
default = "-%d"
206206
}
207+
208+
variable "cpu_core_count" {
209+
description = "Sets the number of CPU cores for an instance." # This option is only supported on creation of instance type that support CPU Options https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-optimize-cpu.html#cpu-options-supported-instances-values
210+
type = number
211+
default = null
212+
}
213+
214+
variable "cpu_threads_per_core" {
215+
description = "Sets the number of CPU threads per core for an instance (has no effect unless cpu_core_count is also set)."
216+
type = number
217+
default = null
218+
}

0 commit comments

Comments
 (0)