Skip to content

Commit 6a123ad

Browse files
authored
feat: Implementation of the cpu_options block and addition of support for AMD SEV-SNP (#334)
Co-authored-by: Samuel CHNIBER <[email protected]>
1 parent d6207bf commit 6a123ad

File tree

8 files changed

+130
-11
lines changed

8 files changed

+130
-11
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ The following combinations are supported to conditionally create resources:
162162
| Name | Version |
163163
|------|---------|
164164
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
165-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.20 |
165+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.66 |
166166

167167
## Providers
168168

169169
| Name | Version |
170170
|------|---------|
171-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.20 |
171+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.66 |
172172

173173
## Modules
174174

@@ -199,6 +199,7 @@ No modules.
199199
| <a name="input_capacity_reservation_specification"></a> [capacity\_reservation\_specification](#input\_capacity\_reservation\_specification) | Describes an instance's Capacity Reservation targeting option | `any` | `{}` | no |
200200
| <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 |
201201
| <a name="input_cpu_credits"></a> [cpu\_credits](#input\_cpu\_credits) | The credit option for CPU usage (unlimited or standard) | `string` | `null` | no |
202+
| <a name="input_cpu_options"></a> [cpu\_options](#input\_cpu\_options) | Defines CPU options to apply to the instance at launch time. | `any` | `{}` | no |
202203
| <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 |
203204
| <a name="input_create"></a> [create](#input\_create) | Whether to create an instance | `bool` | `true` | no |
204205
| <a name="input_create_iam_instance_profile"></a> [create\_iam\_instance\_profile](#input\_create\_iam\_instance\_profile) | Determines whether an IAM instance profile is created or to use an existing IAM instance profile | `bool` | `false` | no |

examples/complete/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ Note that this example may create resources which can cost money. Run `terraform
2020
| Name | Version |
2121
|------|---------|
2222
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
23-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.20 |
23+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.66 |
2424

2525
## Providers
2626

2727
| Name | Version |
2828
|------|---------|
29-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.20 |
29+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.66 |
3030

3131
## Modules
3232

3333
| Name | Source | Version |
3434
|------|--------|---------|
3535
| <a name="module_ec2_complete"></a> [ec2\_complete](#module\_ec2\_complete) | ../../ | n/a |
36+
| <a name="module_ec2_cpu_options"></a> [ec2\_cpu\_options](#module\_ec2\_cpu\_options) | ../../ | n/a |
3637
| <a name="module_ec2_disabled"></a> [ec2\_disabled](#module\_ec2\_disabled) | ../../ | n/a |
3738
| <a name="module_ec2_metadata_options"></a> [ec2\_metadata\_options](#module\_ec2\_metadata\_options) | ../../ | n/a |
3839
| <a name="module_ec2_multiple"></a> [ec2\_multiple](#module\_ec2\_multiple) | ../../ | n/a |
@@ -55,6 +56,7 @@ Note that this example may create resources which can cost money. Run `terraform
5556
| [aws_network_interface.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_interface) | resource |
5657
| [aws_placement_group.web](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/placement_group) | resource |
5758
| [aws_ami.amazon_linux](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
59+
| [aws_ami.amazon_linux_23](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
5860
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
5961

6062
## Inputs

examples/complete/main.tf

+84-5
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ module "ec2_complete" {
5454
user_data_base64 = base64encode(local.user_data)
5555
user_data_replace_on_change = true
5656

57-
cpu_core_count = 2 # default 4
58-
cpu_threads_per_core = 1 # default 2
59-
57+
cpu_options = {
58+
core_count = 2
59+
threads_per_core = 1
60+
}
6061
enable_volume_tags = false
6162
root_block_device = [
6263
{
@@ -244,8 +245,10 @@ module "ec2_spot_instance" {
244245

245246
user_data_base64 = base64encode(local.user_data)
246247

247-
cpu_core_count = 2 # default 4
248-
cpu_threads_per_core = 1 # default 2
248+
cpu_options = {
249+
core_count = 2
250+
threads_per_core = 1
251+
}
249252

250253
enable_volume_tags = false
251254
root_block_device = [
@@ -334,6 +337,72 @@ resource "aws_ec2_capacity_reservation" "targeted" {
334337
instance_match_criteria = "targeted"
335338
}
336339

340+
################################################################################
341+
# EC2 Module - CPU Options
342+
################################################################################
343+
module "ec2_cpu_options" {
344+
source = "../../"
345+
346+
name = "${local.name}-cpu-options"
347+
348+
ami = data.aws_ami.amazon_linux_23.id
349+
instance_type = "c6a.xlarge" # used to set core count below and test amd_sev_snp attribute
350+
availability_zone = element(module.vpc.azs, 0)
351+
subnet_id = element(module.vpc.private_subnets, 0)
352+
vpc_security_group_ids = [module.security_group.security_group_id]
353+
placement_group = aws_placement_group.web.id
354+
associate_public_ip_address = true
355+
disable_api_stop = false
356+
357+
create_iam_instance_profile = true
358+
iam_role_description = "IAM role for EC2 instance"
359+
iam_role_policies = {
360+
AdministratorAccess = "arn:aws:iam::aws:policy/AdministratorAccess"
361+
}
362+
363+
user_data_base64 = base64encode(local.user_data)
364+
user_data_replace_on_change = true
365+
366+
cpu_options = {
367+
core_count = 2
368+
threads_per_core = 1
369+
amd_sev_snp = "enabled"
370+
}
371+
enable_volume_tags = false
372+
root_block_device = [
373+
{
374+
encrypted = true
375+
volume_type = "gp3"
376+
throughput = 200
377+
volume_size = 50
378+
tags = {
379+
Name = "my-root-block"
380+
}
381+
},
382+
]
383+
384+
ebs_block_device = [
385+
{
386+
device_name = "/dev/sdf"
387+
volume_type = "gp3"
388+
volume_size = 5
389+
throughput = 200
390+
encrypted = true
391+
kms_key_id = aws_kms_key.this.arn
392+
tags = {
393+
MountPoint = "/mnt/data"
394+
}
395+
}
396+
]
397+
398+
tags = merge(
399+
local.tags,
400+
{
401+
Name = "${local.name}-cpu-options"
402+
}
403+
)
404+
}
405+
337406
################################################################################
338407
# Supporting Resources
339408
################################################################################
@@ -362,6 +431,16 @@ data "aws_ami" "amazon_linux" {
362431
}
363432
}
364433

434+
data "aws_ami" "amazon_linux_23" {
435+
most_recent = true
436+
owners = ["amazon"]
437+
438+
filter {
439+
name = "name"
440+
values = ["al2023-ami-2023*-x86_64"]
441+
}
442+
}
443+
365444
module "security_group" {
366445
source = "terraform-aws-modules/security-group/aws"
367446
version = "~> 4.0"

examples/complete/versions.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.20"
7+
version = ">= 4.66"
88
}
99
}
1010
}

main.tf

+30
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ resource "aws_instance" "this" {
4646

4747
ebs_optimized = var.ebs_optimized
4848

49+
dynamic "cpu_options" {
50+
for_each = length(var.cpu_options) > 0 ? [var.cpu_options] : []
51+
52+
content {
53+
core_count = try(cpu_options.value.core_count, null)
54+
threads_per_core = try(cpu_options.value.threads_per_core, null)
55+
amd_sev_snp = try(cpu_options.value.amd_sev_snp, null)
56+
}
57+
}
58+
4959
dynamic "capacity_reservation_specification" {
5060
for_each = length(var.capacity_reservation_specification) > 0 ? [var.capacity_reservation_specification] : []
5161

@@ -204,6 +214,16 @@ resource "aws_instance" "ignore_ami" {
204214

205215
ebs_optimized = var.ebs_optimized
206216

217+
dynamic "cpu_options" {
218+
for_each = length(var.cpu_options) > 0 ? [var.cpu_options] : []
219+
220+
content {
221+
core_count = try(cpu_options.value.core_count, null)
222+
threads_per_core = try(cpu_options.value.threads_per_core, null)
223+
amd_sev_snp = try(cpu_options.value.amd_sev_snp, null)
224+
}
225+
}
226+
207227
dynamic "capacity_reservation_specification" {
208228
for_each = length(var.capacity_reservation_specification) > 0 ? [var.capacity_reservation_specification] : []
209229

@@ -379,6 +399,16 @@ resource "aws_spot_instance_request" "this" {
379399
valid_from = var.spot_valid_from
380400
# End spot request specific attributes
381401

402+
dynamic "cpu_options" {
403+
for_each = length(var.cpu_options) > 0 ? [var.cpu_options] : []
404+
405+
content {
406+
core_count = try(cpu_options.value.core_count, null)
407+
threads_per_core = try(cpu_options.value.threads_per_core, null)
408+
amd_sev_snp = try(cpu_options.value.amd_sev_snp, null)
409+
}
410+
}
411+
382412
dynamic "capacity_reservation_specification" {
383413
for_each = length(var.capacity_reservation_specification) > 0 ? [var.capacity_reservation_specification] : []
384414

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ variable "timeouts" {
260260
default = {}
261261
}
262262

263+
variable "cpu_options" {
264+
description = "Defines CPU options to apply to the instance at launch time."
265+
type = any
266+
default = {}
267+
}
268+
263269
variable "cpu_core_count" {
264270
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
265271
type = number

versions.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.20"
7+
version = ">= 4.66"
88
}
99
}
1010
}

wrappers/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module "wrapper" {
5050
enable_volume_tags = try(each.value.enable_volume_tags, var.defaults.enable_volume_tags, true)
5151
vpc_security_group_ids = try(each.value.vpc_security_group_ids, var.defaults.vpc_security_group_ids, null)
5252
timeouts = try(each.value.timeouts, var.defaults.timeouts, {})
53+
cpu_options = try(each.value.cpu_options, var.defaults.cpu_options, {})
5354
cpu_core_count = try(each.value.cpu_core_count, var.defaults.cpu_core_count, null)
5455
cpu_threads_per_core = try(each.value.cpu_threads_per_core, var.defaults.cpu_threads_per_core, null)
5556
create_spot_instance = try(each.value.create_spot_instance, var.defaults.create_spot_instance, false)

0 commit comments

Comments
 (0)