From 4e7bb081a4eec0f90f810b7bb2e201127736c084 Mon Sep 17 00:00:00 2001 From: karancode Date: Tue, 29 Sep 2020 01:52:09 +0900 Subject: [PATCH 1/5] add cpu_core options --- main.tf | 2 ++ variables.tf | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/main.tf b/main.tf index cb8f5896..95e04ae5 100644 --- a/main.tf +++ b/main.tf @@ -75,6 +75,8 @@ resource "aws_instance" "this" { instance_initiated_shutdown_behavior = var.instance_initiated_shutdown_behavior placement_group = var.placement_group tenancy = var.tenancy + cpu_core_count = var.cpu_core_count + cpu_threads_per_core = var.cpu_threads_per_core tags = merge( { diff --git a/variables.tf b/variables.tf index fe5b0d3a..5d8fc998 100644 --- a/variables.tf +++ b/variables.tf @@ -193,4 +193,14 @@ variable "num_suffix_format" { default = "-%d" } +variable "cpu_core_count" { + 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 + type = number + default = null +} +variable "cpu_threads_per_core" { + description = "Sets the number of CPU threads per core for an instance (has no effect unless cpu_core_count is also set)." + type = number + default = 2 +} From d6500ff70c8161e05db61b987c2cf28995c5ff3a Mon Sep 17 00:00:00 2001 From: karancode Date: Thu, 1 Oct 2020 15:55:37 +0900 Subject: [PATCH 2/5] update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 729e6d01..4065b1dd 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,9 @@ No requirements. |------|-------------|------|---------|:--------:| | ami | ID of AMI to use for the instance | `string` | n/a | yes | | associate\_public\_ip\_address | If true, the EC2 instance will have associated public IP address | `bool` | `null` | no | +| cpu\_core\_count | Sets the number of CPU cores for an instance. | `number` | `null` | no | | cpu\_credits | The credit option for CPU usage (unlimited or standard) | `string` | `"standard"` | no | +| 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` | `2` | no | | disable\_api\_termination | If true, enables EC2 Instance Termination Protection | `bool` | `false` | no | | ebs\_block\_device | Additional EBS block devices to attach to the instance | `list(map(string))` | `[]` | no | | ebs\_optimized | If true, the launched EC2 instance will be EBS-optimized | `bool` | `false` | no | From b8040bc19a884a9d19e4c4ff799d793c9e3bf4f2 Mon Sep 17 00:00:00 2001 From: karancode Date: Thu, 1 Oct 2020 16:05:40 +0900 Subject: [PATCH 3/5] add example --- examples/basic/main.tf | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/examples/basic/main.tf b/examples/basic/main.tf index 2ae888d6..c4acee18 100644 --- a/examples/basic/main.tf +++ b/examples/basic/main.tf @@ -173,3 +173,35 @@ module "ec2_zero" { subnet_id = tolist(data.aws_subnet_ids.all.ids)[0] vpc_security_group_ids = [module.security_group.this_security_group_id] } + +module "ec2_optimize_cpu" { + source = "../../" + + instance_count = 1 + + name = "example-optimize-cpu" + ami = data.aws_ami.amazon_linux.id + instance_type = "c4.2xlarge" + subnet_id = tolist(data.aws_subnet_ids.all.ids)[0] + + vpc_security_group_ids = [module.security_group.this_security_group_id] + associate_public_ip_address = true + placement_group = aws_placement_group.web.id + + user_data_base64 = base64encode(local.user_data) + + root_block_device = [ + { + volume_type = "gp2" + volume_size = 10 + }, + ] + cpu_core_count = 2 # default 4 + cpu_threads_per_core = 1 # default 2 + + + tags = { + "Env" = "Private" + "Location" = "Secret" + } +} From 0cfa166576082e4a0815c5eb5de610fbd73a111d Mon Sep 17 00:00:00 2001 From: karancode Date: Thu, 26 Aug 2021 00:37:06 +0900 Subject: [PATCH 4/5] default threads null --- variables.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/variables.tf b/variables.tf index 3bd8b4f2..72be9e4d 100644 --- a/variables.tf +++ b/variables.tf @@ -214,5 +214,5 @@ variable "cpu_core_count" { variable "cpu_threads_per_core" { description = "Sets the number of CPU threads per core for an instance (has no effect unless cpu_core_count is also set)." type = number - default = 2 + default = null } From 627edbaf7893aec14f78b3f57c6471cbe4195b1f Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Wed, 25 Aug 2021 17:46:29 +0200 Subject: [PATCH 5/5] Fixed docs --- README.md | 2 ++ examples/basic/README.md | 1 + examples/basic/main.tf | 3 +-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c565995e..8e508239 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,9 @@ No modules. |------|-------------|------|---------|:--------:| | [ami](#input\_ami) | ID of AMI to use for the instance | `string` | n/a | yes | | [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | If true, the EC2 instance will have associated public IP address | `bool` | `null` | no | +| [cpu\_core\_count](#input\_cpu\_core\_count) | Sets the number of CPU cores for an instance. | `number` | `null` | no | | [cpu\_credits](#input\_cpu\_credits) | The credit option for CPU usage (unlimited or standard) | `string` | `"standard"` | no | +| [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 | | [disable\_api\_termination](#input\_disable\_api\_termination) | If true, enables EC2 Instance Termination Protection | `bool` | `false` | no | | [ebs\_block\_device](#input\_ebs\_block\_device) | Additional EBS block devices to attach to the instance | `list(map(string))` | `[]` | no | | [ebs\_optimized](#input\_ebs\_optimized) | If true, the launched EC2 instance will be EBS-optimized | `bool` | `false` | no | diff --git a/examples/basic/README.md b/examples/basic/README.md index 9d615233..7c8cf737 100644 --- a/examples/basic/README.md +++ b/examples/basic/README.md @@ -33,6 +33,7 @@ Note that this example may create resources which can cost money. Run `terraform | Name | Source | Version | |------|--------|---------| | [ec2](#module\_ec2) | ../../ | | +| [ec2\_optimize\_cpu](#module\_ec2\_optimize\_cpu) | ../../ | | | [ec2\_with\_metadata\_options](#module\_ec2\_with\_metadata\_options) | ../../ | | | [ec2\_with\_network\_interface](#module\_ec2\_with\_network\_interface) | ../../ | | | [ec2\_with\_t2\_unlimited](#module\_ec2\_with\_t2\_unlimited) | ../../ | | diff --git a/examples/basic/main.tf b/examples/basic/main.tf index 4df2a743..e0b13a46 100644 --- a/examples/basic/main.tf +++ b/examples/basic/main.tf @@ -207,7 +207,7 @@ module "ec2_optimize_cpu" { instance_type = "c4.2xlarge" subnet_id = tolist(data.aws_subnet_ids.all.ids)[0] - vpc_security_group_ids = [module.security_group.this_security_group_id] + vpc_security_group_ids = [module.security_group.security_group_id] associate_public_ip_address = true placement_group = aws_placement_group.web.id @@ -222,7 +222,6 @@ module "ec2_optimize_cpu" { cpu_core_count = 2 # default 4 cpu_threads_per_core = 1 # default 2 - tags = { "Env" = "Private" "Location" = "Secret"