Skip to content

Commit 559c9b6

Browse files
authored
Merge pull request #45132 from tabito-hara/f-aws_emr_managed_scaling_policy-add_scaling_strategy
[Enhancement] aws_emr_managed_scaling_policy: Add `scaling_strategy` and `utilization_performance_index` arguments
2 parents 015f785 + 8ba2fd8 commit 559c9b6

File tree

4 files changed

+97
-18
lines changed

4 files changed

+97
-18
lines changed

.changelog/45132.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/aws_emr_managed_scaling_policy: Add `scaling_strategy` and `utilization_performance_index` arguments
3+
```

internal/service/emr/managed_scaling_policy.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1718
"github.com/hashicorp/terraform-provider-aws/internal/conns"
1819
"github.com/hashicorp/terraform-provider-aws/internal/enum"
1920
"github.com/hashicorp/terraform-provider-aws/internal/errs"
@@ -73,6 +74,18 @@ func resourceManagedScalingPolicy() *schema.Resource {
7374
},
7475
},
7576
},
77+
"scaling_strategy": {
78+
Type: schema.TypeString,
79+
Optional: true,
80+
ForceNew: true,
81+
ValidateDiagFunc: enum.Validate[awstypes.ScalingStrategy](),
82+
},
83+
"utilization_performance_index": {
84+
Type: schema.TypeInt,
85+
Optional: true,
86+
ForceNew: true,
87+
ValidateFunc: validation.IntInSlice([]int{1, 25, 50, 75, 100}),
88+
},
7689
},
7790
}
7891
}
@@ -82,6 +95,9 @@ func resourceManagedScalingPolicyCreate(ctx context.Context, d *schema.ResourceD
8295
conn := meta.(*conns.AWSClient).EMRClient(ctx)
8396

8497
clusterID := d.Get("cluster_id").(string)
98+
input := &emr.PutManagedScalingPolicyInput{
99+
ClusterId: aws.String(clusterID),
100+
}
85101
if v := d.Get("compute_limits").(*schema.Set).List(); len(v) > 0 && v[0] != nil {
86102
tfMap := v[0].(map[string]any)
87103
computeLimits := &awstypes.ComputeLimits{
@@ -98,18 +114,22 @@ func resourceManagedScalingPolicyCreate(ctx context.Context, d *schema.ResourceD
98114
} else if v, ok := tfMap["maximum_ondemand_capacity_units"].(int); ok && v >= 0 {
99115
computeLimits.MaximumOnDemandCapacityUnits = aws.Int32(int32(v))
100116
}
101-
input := &emr.PutManagedScalingPolicyInput{
102-
ClusterId: aws.String(clusterID),
103-
ManagedScalingPolicy: &awstypes.ManagedScalingPolicy{
104-
ComputeLimits: computeLimits,
105-
},
117+
input.ManagedScalingPolicy = &awstypes.ManagedScalingPolicy{
118+
ComputeLimits: computeLimits,
106119
}
120+
}
121+
if v, ok := d.GetOk("scaling_strategy"); ok {
122+
input.ManagedScalingPolicy.ScalingStrategy = awstypes.ScalingStrategy(v.(string))
123+
}
107124

108-
_, err := conn.PutManagedScalingPolicy(ctx, input)
125+
if v, ok := d.GetOk("utilization_performance_index"); ok {
126+
input.ManagedScalingPolicy.UtilizationPerformanceIndex = aws.Int32(int32(v.(int)))
127+
}
109128

110-
if err != nil {
111-
return sdkdiag.AppendErrorf(diags, "putting EMR Managed Scaling Policy: %s", err)
112-
}
129+
_, err := conn.PutManagedScalingPolicy(ctx, input)
130+
131+
if err != nil {
132+
return sdkdiag.AppendErrorf(diags, "putting EMR Managed Scaling Policy: %s", err)
113133
}
114134

115135
d.SetId(clusterID)
@@ -138,6 +158,10 @@ func resourceManagedScalingPolicyRead(ctx context.Context, d *schema.ResourceDat
138158
return sdkdiag.AppendErrorf(diags, "setting compute_limits: %s", err)
139159
}
140160

161+
if managedScalingPolicy.ScalingStrategy != "" {
162+
d.Set("scaling_strategy", managedScalingPolicy.ScalingStrategy)
163+
d.Set("utilization_performance_index", managedScalingPolicy.UtilizationPerformanceIndex)
164+
}
141165
return diags
142166
}
143167

internal/service/emr/managed_scaling_policy_test.go

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"testing"
1010

11+
awstypes "github.com/aws/aws-sdk-go-v2/service/emr/types"
1112
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1213
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1314
"github.com/hashicorp/terraform-plugin-testing/terraform"
@@ -174,6 +175,38 @@ func TestAccEMRManagedScalingPolicy_ComputeLimits_maximumOnDemandCapacityUnitsSp
174175
})
175176
}
176177

178+
func TestAccEMRManagedScalingPolicy_advancedScaling(t *testing.T) {
179+
ctx := acctest.Context(t)
180+
resourceName := "aws_emr_managed_scaling_policy.test"
181+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
182+
183+
resource.ParallelTest(t, resource.TestCase{
184+
PreCheck: func() { acctest.PreCheck(ctx, t) },
185+
ErrorCheck: acctest.ErrorCheck(t, names.EMRServiceID),
186+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
187+
CheckDestroy: testAccCheckManagedScalingPolicyDestroy(ctx),
188+
Steps: []resource.TestStep{
189+
{
190+
Config: testAccManagedScalingPolicyConfig_advancedScaling(rName),
191+
Check: resource.ComposeTestCheckFunc(
192+
testAccCheckManagedScalingPolicyExists(ctx, resourceName),
193+
resource.TestCheckResourceAttr(resourceName, "scaling_strategy", string(awstypes.ScalingStrategyAdvanced)),
194+
resource.TestCheckResourceAttr(resourceName, "utilization_performance_index", "1"),
195+
),
196+
},
197+
{
198+
ResourceName: resourceName,
199+
ImportState: true,
200+
ImportStateVerify: true,
201+
ImportStateVerifyIgnore: []string{
202+
"compute_limits.0.maximum_core_capacity_units",
203+
"compute_limits.0.maximum_ondemand_capacity_units",
204+
},
205+
},
206+
},
207+
})
208+
}
209+
177210
func testAccCheckManagedScalingPolicyExists(ctx context.Context, n string) resource.TestCheckFunc {
178211
return func(s *terraform.State) error {
179212
rs, ok := s.RootModule().Resources[n]
@@ -215,7 +248,7 @@ func testAccCheckManagedScalingPolicyDestroy(ctx context.Context) resource.TestC
215248
}
216249
}
217250

218-
func testAccManagedScalingPolicyConfig_base(rName string) string {
251+
func testAccManagedScalingPolicyConfig_base(rName, releaseLabel, instanceType string) string {
219252
return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptInDefaultExclude(), fmt.Sprintf(`
220253
resource "aws_vpc" "test" {
221254
cidr_block = "10.0.0.0/16"
@@ -455,16 +488,16 @@ resource "aws_emr_cluster" "test" {
455488
keep_job_flow_alive_when_no_steps = true
456489
log_uri = "s3n://terraform/testlog/"
457490
name = %[1]q
458-
release_label = "emr-5.30.1"
491+
release_label = %[2]q
459492
service_role = aws_iam_role.emr_service.arn
460493
461494
master_instance_group {
462-
instance_type = "c4.large"
495+
instance_type = %[3]q
463496
}
464497
465498
core_instance_group {
466499
instance_count = 1
467-
instance_type = "c4.large"
500+
instance_type = %[3]q
468501
}
469502
470503
depends_on = [
@@ -479,13 +512,15 @@ resource "aws_emr_cluster" "test" {
479512
emr_managed_slave_security_group = aws_security_group.test.id
480513
instance_profile = aws_iam_instance_profile.emr_instance_profile.arn
481514
}
482-
515+
lifecycle {
516+
ignore_changes = ["os_release_label"]
517+
}
483518
}
484-
`, rName))
519+
`, rName, releaseLabel, instanceType))
485520
}
486521

487522
func testAccManagedScalingPolicyConfig_basic(rName string) string {
488-
return acctest.ConfigCompose(testAccManagedScalingPolicyConfig_base(rName), `
523+
return acctest.ConfigCompose(testAccManagedScalingPolicyConfig_base(rName, "emr-5.30.1", "c4.large"), `
489524
resource "aws_emr_managed_scaling_policy" "test" {
490525
cluster_id = aws_emr_cluster.test.id
491526
compute_limits {
@@ -498,7 +533,7 @@ resource "aws_emr_managed_scaling_policy" "test" {
498533
}
499534

500535
func testAccManagedScalingPolicyConfig_computeLimitsMaximumCoreCapacityUnits(rName string, maximumCoreCapacityUnits int) string {
501-
return acctest.ConfigCompose(testAccManagedScalingPolicyConfig_base(rName), fmt.Sprintf(`
536+
return acctest.ConfigCompose(testAccManagedScalingPolicyConfig_base(rName, "emr-5.30.1", "c4.large"), fmt.Sprintf(`
502537
resource "aws_emr_managed_scaling_policy" "test" {
503538
cluster_id = aws_emr_cluster.test.id
504539
compute_limits {
@@ -512,7 +547,7 @@ resource "aws_emr_managed_scaling_policy" "test" {
512547
}
513548

514549
func testAccManagedScalingPolicyConfig_computeLimitsMaximumOnDemandCapacityUnits(rName string, maximumOnDemandCapacityUnits int) string {
515-
return acctest.ConfigCompose(testAccManagedScalingPolicyConfig_base(rName), fmt.Sprintf(`
550+
return acctest.ConfigCompose(testAccManagedScalingPolicyConfig_base(rName, "emr-5.30.1", "c4.large"), fmt.Sprintf(`
516551
resource "aws_emr_managed_scaling_policy" "test" {
517552
cluster_id = aws_emr_cluster.test.id
518553
compute_limits {
@@ -524,3 +559,18 @@ resource "aws_emr_managed_scaling_policy" "test" {
524559
}
525560
`, maximumOnDemandCapacityUnits))
526561
}
562+
563+
func testAccManagedScalingPolicyConfig_advancedScaling(rName string) string {
564+
return acctest.ConfigCompose(testAccManagedScalingPolicyConfig_base(rName, "emr-7.12.0", "r8g.xlarge"), `
565+
resource "aws_emr_managed_scaling_policy" "test" {
566+
cluster_id = aws_emr_cluster.test.id
567+
compute_limits {
568+
unit_type = "Instances"
569+
minimum_capacity_units = 1
570+
maximum_capacity_units = 2
571+
}
572+
scaling_strategy = "ADVANCED"
573+
utilization_performance_index = 1
574+
}
575+
`)
576+
}

website/docs/r/emr_managed_scaling_policy.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ This resource supports the following arguments:
4646
* `region` - (Optional) Region where this resource will be [managed](https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints). Defaults to the Region set in the [provider configuration](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#aws-configuration-reference).
4747
* `cluster_id` - (Required) ID of the EMR cluster
4848
* `compute_limits` - (Required) Configuration block with compute limit settings. Described below.
49+
* `scaling_strategy` – (Optional) Specifies the scaling strategy. When set to `ADVANCED`, the `utilization_performance_index` argument can be used to configure an advanced scaling strategy. An advanced scaling strategy requires Amazon EMR on EC2 version 7.0 or later. Valid values: `ADVANCED`, `DEFAULT`.
50+
* `utilization_performance_index` – (Optional) Integer value that represents the advanced scaling strategy. Higher values optimize for performance, while lower values optimize for resource conservation. A value of `50` provides a balance between performance and resource conservation. See [the AWS documentation](https://docs.aws.amazon.com/emr/latest/ManagementGuide/managed-scaling-allocation-strategy-optimized.html#managed-scaling-allocation-strategy-optimized-getting-started) for more details. Required when `scaling_strategy` is set to `ADVANCED`. Valid values: `1`, `25`, `50`, `75`, `100`.
4951

5052
### compute_limits
5153

0 commit comments

Comments
 (0)