diff --git a/aws/resource_aws_launch_template.go b/aws/resource_aws_launch_template.go index c4b7248328b0..d4db44c91790 100644 --- a/aws/resource_aws_launch_template.go +++ b/aws/resource_aws_launch_template.go @@ -140,6 +140,24 @@ func resourceAwsLaunchTemplate() *schema.Resource { }, }, + "cpu_options": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "core_count": { + Type: schema.TypeInt, + Optional: true, + }, + "threads_per_core": { + Type: schema.TypeInt, + Optional: true, + }, + }, + }, + }, + "capacity_reservation_specification": { Type: schema.TypeList, Optional: true, @@ -649,6 +667,10 @@ func resourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) err return fmt.Errorf("error setting capacity_reservation_specification: %s", err) } + if err := d.Set("cpu_options", getCpuOptions(ltData.CpuOptions)); err != nil { + return err + } + if strings.HasPrefix(aws.StringValue(ltData.InstanceType), "t2") || strings.HasPrefix(aws.StringValue(ltData.InstanceType), "t3") { if err := d.Set("credit_specification", getCreditSpecification(ltData.CreditSpecification)); err != nil { return fmt.Errorf("error setting credit_specification: %s", err) @@ -810,6 +832,17 @@ func getCapacityReservationTarget(crt *ec2.CapacityReservationTargetResponse) [] return s } +func getCpuOptions(cs *ec2.LaunchTemplateCpuOptions) []interface{} { + s := []interface{}{} + if cs != nil { + s = append(s, map[string]interface{}{ + "core_count": aws.Int64Value(cs.CoreCount), + "threads_per_core": aws.Int64Value(cs.ThreadsPerCore), + }) + } + return s +} + func getCreditSpecification(cs *ec2.CreditSpecification) []interface{} { s := []interface{}{} if cs != nil { @@ -1078,6 +1111,14 @@ func buildLaunchTemplateData(d *schema.ResourceData) (*ec2.RequestLaunchTemplate } } + if v, ok := d.GetOk("cpu_options"); ok { + co := v.([]interface{}) + + if len(co) > 0 { + opts.CpuOptions = readCpuOptionsFromConfig(co[0].(map[string]interface{})) + } + } + if v, ok := d.GetOk("credit_specification"); ok && (strings.HasPrefix(instanceType, "t2") || strings.HasPrefix(instanceType, "t3")) { cs := v.([]interface{}) @@ -1378,6 +1419,20 @@ func readCapacityReservationTargetFromConfig(crt map[string]interface{}) *ec2.Ca return capacityReservationTarget } +func readCpuOptionsFromConfig(co map[string]interface{}) *ec2.LaunchTemplateCpuOptionsRequest { + cpuOptions := &ec2.LaunchTemplateCpuOptionsRequest{} + + if v, ok := co["core_count"].(int); ok && v != 0 { + cpuOptions.CoreCount = aws.Int64(int64(v)) + } + + if v, ok := co["threads_per_core"].(int); ok && v != 0 { + cpuOptions.ThreadsPerCore = aws.Int64(int64(v)) + } + + return cpuOptions +} + func readCreditSpecificationFromConfig(cs map[string]interface{}) *ec2.CreditSpecificationRequest { creditSpecification := &ec2.CreditSpecificationRequest{} diff --git a/aws/resource_aws_launch_template_test.go b/aws/resource_aws_launch_template_test.go index c54bee67d351..f73c7f8c4e33 100644 --- a/aws/resource_aws_launch_template_test.go +++ b/aws/resource_aws_launch_template_test.go @@ -424,6 +424,26 @@ func TestAccAWSLaunchTemplate_capacityReservation_target(t *testing.T) { }) } +func TestAccAWSLaunchTemplate_cpuOptions(t *testing.T) { + var template ec2.LaunchTemplate + resName := "aws_launch_template.foo" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLaunchTemplateDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLaunchTemplateConfig_cpuOptions(rName, 4, 2), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSLaunchTemplateExists(resName, &template), + ), + }, + }, + }) +} + func TestAccAWSLaunchTemplate_creditSpecification_nonBurstable(t *testing.T) { var template ec2.LaunchTemplate rName := acctest.RandomWithPrefix("tf-acc-test") @@ -1053,6 +1073,19 @@ resource "aws_launch_template" "test" { `, rInt) } +func testAccAWSLaunchTemplateConfig_cpuOptions(rName string, coreCount, threadsPerCore int) string { + return fmt.Sprintf(` +resource "aws_launch_template" "foo" { + name = %q + + cpu_options { + core_count = %d + threads_per_core = %d + } +} +`, rName, coreCount, threadsPerCore) +} + func testAccAWSLaunchTemplateConfig_creditSpecification(rName, instanceType, cpuCredits string) string { return fmt.Sprintf(` resource "aws_launch_template" "test" { diff --git a/website/docs/r/launch_template.html.markdown b/website/docs/r/launch_template.html.markdown index 20cff8c22233..a3a1451cb368 100644 --- a/website/docs/r/launch_template.html.markdown +++ b/website/docs/r/launch_template.html.markdown @@ -28,6 +28,11 @@ resource "aws_launch_template" "foo" { capacity_reservation_preference = "open" } + cpu_options { + core_count = 4 + threads_per_core = 2 + } + credit_specification { cpu_credits = "standard" } @@ -104,6 +109,7 @@ The following arguments are supported: * `block_device_mappings` - Specify volumes to attach to the instance besides the volumes specified by the AMI. See [Block Devices](#block-devices) below for details. * `capacity_reservation_specification` - Targeting for EC2 capacity reservations. See [Capacity Reservation Specification](#capacity-reservation-specification) below for more details. +* `cpu_options` - The CPU options for the instance. See [CPU Options](#cpu-options) below for more details. * `credit_specification` - Customize the credit specification of the instance. See [Credit Specification](#credit-specification) below for more details. * `disable_api_termination` - If `true`, enables [EC2 Instance @@ -177,6 +183,16 @@ The `capacity_reservation_target` block supports the following: * `capacity_reservation_id` - The ID of the Capacity Reservation to target. +### CPU Options + +The `cpu_options` block supports the following: + +* `core_count` - The number of CPU cores for the instance. +* `threads_per_core` - The number of threads per CPU core. To disable Intel Hyper-Threading Technology for the instance, specify a value of 1. +Otherwise, specify the default value of 2. + +Both number of CPU cores and threads per core must be specified. Valid number of CPU cores and threads per core for the instance type can be found in the [CPU Options Documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-optimize-cpu.html?shortFooter=true#cpu-options-supported-instances-values) + ### Credit Specification Credit specification can be applied/modified to the EC2 Instance at any time.