Skip to content

Commit

Permalink
resource/aws_launch_template: Add cpu_options configuration block (…
Browse files Browse the repository at this point in the history
…support disabling multithreading) (#6552)

Output from acceptance testing:

```
--- PASS: TestAccAWSLaunchTemplate_disappears (19.67s)
--- PASS: TestAccAWSLaunchTemplate_IamInstanceProfile_EmptyConfigurationBlock (20.43s)
--- PASS: TestAccAWSLaunchTemplate_cpuOptions (21.78s)
--- PASS: TestAccAWSLaunchTemplate_capacityReservation_preference (22.78s)
--- PASS: TestAccAWSLaunchTemplate_creditSpecification_t2 (23.44s)
--- PASS: TestAccAWSLaunchTemplate_data (24.46s)
--- PASS: TestAccAWSLaunchTemplate_basic (25.09s)
--- PASS: TestAccAWSLaunchTemplateDataSource_basic (25.55s)
--- PASS: TestAccAWSLaunchTemplate_creditSpecification_nonBurstable (25.75s)
--- PASS: TestAccAWSLaunchTemplate_creditSpecification_t3 (26.53s)
--- PASS: TestAccAWSLaunchTemplate_capacityReservation_target (27.86s)
--- PASS: TestAccAWSLaunchTemplate_networkInterface (28.58s)
--- PASS: TestAccAWSLaunchTemplate_description (30.73s)
--- PASS: TestAccAWSLaunchTemplate_ElasticInferenceAccelerator (31.05s)
--- PASS: TestAccAWSLaunchTemplate_tags (32.30s)
--- PASS: TestAccAWSLaunchTemplate_networkInterface_ipv6AddressCount (15.61s)
--- PASS: TestAccAWSLaunchTemplate_networkInterface_ipv6Addresses (16.28s)
--- PASS: TestAccAWSLaunchTemplate_licenseSpecification (13.24s)
--- PASS: TestAccAWSLaunchTemplate_associatePublicIPAddress (43.89s)
--- PASS: TestAccAWSLaunchTemplate_update (48.68s)
--- PASS: TestAccAWSLaunchTemplate_EbsOptimized (49.54s)
--- PASS: TestAccAWSLaunchTemplate_BlockDeviceMappings_EBS_DeleteOnTermination (52.88s)
--- PASS: TestAccAWSLaunchTemplate_BlockDeviceMappings_EBS (55.49s)
--- PASS: TestAccAWSLaunchTemplate_instanceMarketOptions (42.68s)
```
  • Loading branch information
praveensastry authored Feb 12, 2020
1 parent 9c4b6a0 commit fbd9336
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
55 changes: 55 additions & 0 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{})

Expand Down Expand Up @@ -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{}

Expand Down
33 changes: 33 additions & 0 deletions aws/resource_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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" {
Expand Down
16 changes: 16 additions & 0 deletions website/docs/r/launch_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit fbd9336

Please sign in to comment.