Skip to content

r/aws_batch_compute_environment: add unmanaged vCPUs argument #42618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/42618.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_batch_compute_environment: Add `unmanaged_v_cpus` argument
```
22 changes: 21 additions & 1 deletion internal/service/batch/compute_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func resourceComputeEnvironment() *schema.Resource {
StateFunc: sdkv2.ToUpperSchemaStateFunc,
ValidateDiagFunc: enum.ValidateIgnoreCase[awstypes.CEType](),
},
"unmanaged_v_cpus": {
Type: schema.TypeInt,
Optional: true,
},
"update_policy": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -299,6 +303,10 @@ func resourceComputeEnvironmentCreate(ctx context.Context, d *schema.ResourceDat
input.State = awstypes.CEState(v.(string))
}

if v, ok := d.GetOk("unmanaged_v_cpus"); ok {
input.UnmanagedvCpus = aws.Int32(int32(v.(int)))
}

output, err := conn.CreateComputeEnvironment(ctx, input)

if err != nil {
Expand Down Expand Up @@ -371,6 +379,7 @@ func resourceComputeEnvironmentRead(ctx context.Context, d *schema.ResourceData,
d.Set(names.AttrStatus, computeEnvironment.Status)
d.Set(names.AttrStatusReason, computeEnvironment.StatusReason)
d.Set(names.AttrType, computeEnvironment.Type)
d.Set("unmanaged_v_cpus", computeEnvironment.UnmanagedvCpus)
if err := d.Set("update_policy", flattenComputeEnvironmentUpdatePolicy(computeEnvironment.UpdatePolicy)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting update_policy: %s", err)
}
Expand All @@ -397,6 +406,12 @@ func resourceComputeEnvironmentUpdate(ctx context.Context, d *schema.ResourceDat
input.State = awstypes.CEState(d.Get(names.AttrState).(string))
}

if d.HasChange("unmanaged_v_cpus") {
if v, ok := d.GetOk("unmanaged_v_cpus"); ok {
input.UnmanagedvCpus = aws.Int32(int32(v.(int)))
}
}

if d.HasChange("update_policy") {
input.UpdatePolicy = expandComputeEnvironmentUpdatePolicy(d.Get("update_policy").([]any))
}
Expand Down Expand Up @@ -568,6 +583,11 @@ func resourceComputeEnvironmentCustomizeDiff(_ context.Context, diff *schema.Res
if v, ok := diff.GetOk("compute_resources"); ok && len(v.([]any)) > 0 && v.([]any)[0] != nil {
return fmt.Errorf("no `compute_resources` can be specified when `type` is %q", computeEnvironmentType)
}
} else {
// Only UNMANAGED compute environments can use UnmanagedvCpus.
if _, ok := diff.GetOk("unmanaged_v_cpus"); ok {
return fmt.Errorf("`unmanaged_v_cpus` can only be specified when `type` is %q", string(awstypes.CETypeUnmanaged))
}
}

if diff.Id() != "" {
Expand Down Expand Up @@ -820,7 +840,7 @@ func isServiceLinkedRoleDiff(diff *schema.ResourceDiff) bool {
if diff.HasChange(names.AttrServiceRole) {
beforeRaw, afterRaw := diff.GetChange(names.AttrServiceRole)
before, _ = beforeRaw.(string)
after, _ := afterRaw.(string)
after, _ = afterRaw.(string)
return isServiceLinkedRole(before) && isServiceLinkedRole(after)
}
afterRaw, _ := diff.GetOk(names.AttrServiceRole)
Expand Down
115 changes: 115 additions & 0 deletions internal/service/batch/compute_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,104 @@ func TestAccBatchComputeEnvironment_createEC2WithoutComputeResources(t *testing.
})
}

// TestAccBatchComputeEnvironment_unmanagedVCPUs tests the unmanaged_v_cpus parameter
func TestAccBatchComputeEnvironment_unmanagedVCPUs(t *testing.T) {
ctx := acctest.Context(t)
var ce awstypes.ComputeEnvironmentDetail
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_batch_compute_environment.test"
serviceRoleResourceName := "aws_iam_role.batch_service"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.BatchServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckComputeEnvironmentDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccComputeEnvironmentConfig_unmanagedVCPUs(rName, 4),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeEnvironmentExists(ctx, resourceName, &ce),
acctest.CheckResourceAttrRegionalARN(ctx, resourceName, names.AttrARN, "batch", fmt.Sprintf("compute-environment/%s", rName)),
resource.TestCheckResourceAttr(resourceName, "compute_environment_name", rName),
resource.TestCheckResourceAttr(resourceName, "compute_environment_name_prefix", ""),
resource.TestCheckResourceAttr(resourceName, "compute_resources.#", "0"),
resource.TestCheckResourceAttrSet(resourceName, "ecs_cluster_arn"),
resource.TestCheckResourceAttrPair(resourceName, names.AttrServiceRole, serviceRoleResourceName, names.AttrARN),
resource.TestCheckResourceAttr(resourceName, names.AttrState, "ENABLED"),
resource.TestCheckResourceAttrSet(resourceName, names.AttrStatus),
resource.TestCheckResourceAttrSet(resourceName, names.AttrStatusReason),
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, "0"),
resource.TestCheckResourceAttr(resourceName, names.AttrType, "UNMANAGED"),
resource.TestCheckResourceAttr(resourceName, "unmanaged_v_cpus", "4"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeEnvironmentConfig_unmanagedVCPUs(rName, 8),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeEnvironmentExists(ctx, resourceName, &ce),
resource.TestCheckResourceAttr(resourceName, "unmanaged_v_cpus", "8"),
),
},
},
})
}

// TestAccBatchComputeEnvironment_unmanagedVCPUsWithManaged tests that unmanaged_v_cpus parameter cannot be used with MANAGED type
func TestAccBatchComputeEnvironment_unmanagedVCPUsWithManaged(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.BatchServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckComputeEnvironmentDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccComputeEnvironmentConfig_managedWithUnmanagedVCPUs(rName, 4),
ExpectError: regexache.MustCompile("`unmanaged_v_cpus` can only be specified when `type` is \"UNMANAGED\""),
},
},
})
}

// testAccComputeEnvironmentConfig_managedWithUnmanagedVCPUs tests that unmanaged_v_cpus parameter cannot be used with MANAGED type
func testAccComputeEnvironmentConfig_managedWithUnmanagedVCPUs(rName string, unmanagedVCPUs int) string {
return fmt.Sprintf(`
%s

resource "aws_batch_compute_environment" "test" {
compute_environment_name = %[2]q

compute_resources {
instance_role = aws_iam_instance_profile.ecs_instance.arn
instance_type = ["optimal"]
max_vcpus = 16
min_vcpus = 0
security_group_ids = [
aws_security_group.test.id
]
subnets = [
aws_subnet.test.id
]
type = "EC2"
}

service_role = aws_iam_role.batch_service.arn
type = "MANAGED"
unmanaged_v_cpus = %[3]d

depends_on = [aws_iam_role_policy_attachment.batch_service]
}
`, testAccComputeEnvironmentConfig_base(rName), rName, unmanagedVCPUs)
}

func testAccCheckComputeEnvironmentDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).BatchClient(ctx)
Expand Down Expand Up @@ -3136,3 +3234,20 @@ resource "aws_batch_compute_environment" "test" {
}
`, rName))
}

// testAccComputeEnvironmentConfig_unmanagedVCPUs is a test fixture for unmanaged_v_cpus parameter
func testAccComputeEnvironmentConfig_unmanagedVCPUs(rName string, unmanagedVCPUs int) string {
return fmt.Sprintf(`
%s

resource "aws_batch_compute_environment" "test" {
compute_environment_name = %[2]q

service_role = aws_iam_role.batch_service.arn
type = "UNMANAGED"
unmanaged_v_cpus = %[3]d

depends_on = [aws_iam_role_policy_attachment.batch_service]
}
`, testAccComputeEnvironmentConfig_base(rName), rName, unmanagedVCPUs)
}
1 change: 1 addition & 0 deletions website/docs/r/batch_compute_environment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ This resource supports the following arguments:
* `state` - (Optional) The state of the compute environment. If the state is `ENABLED`, then the compute environment accepts jobs from a queue and can scale out automatically based on queues. Valid items are `ENABLED` or `DISABLED`. Defaults to `ENABLED`.
* `tags` - (Optional) Key-value map of resource tags. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
* `type` - (Required) The type of the compute environment. Valid items are `MANAGED` or `UNMANAGED`.
* `unmanaged_v_cpus` - (Optional) The maximum number of allowed vCPUs for the Batch compute environment. This parameter can only be specified when `type` is `UNMANAGED`.
* `update_policy` - (Optional) Specifies the infrastructure update policy for the compute environment. See details below.

### compute_resources
Expand Down
Loading