Skip to content

Commit 48985f5

Browse files
authored
Merge pull request #43337 from hashicorp/b-aws_batch_compute_environment.inconsistent-final-plan
r/aws_batch_compute_environment: If the launch template ID is unknown, ForceNew
2 parents b806964 + 99dbc8f commit 48985f5

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

internal/service/batch/compute_environment.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func resourceComputeEnvironment() *schema.Resource {
8181
"compute_resources": {
8282
Type: schema.TypeList,
8383
Optional: true,
84-
ForceNew: true,
84+
Computed: true,
8585
MinItems: 0,
8686
MaxItems: 1,
8787
Elem: &schema.Resource{
@@ -576,7 +576,7 @@ func resourceComputeEnvironmentDelete(ctx context.Context, d *schema.ResourceDat
576576
return diags
577577
}
578578

579-
func resourceComputeEnvironmentCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, meta any) error {
579+
func resourceComputeEnvironmentCustomizeDiff(ctx context.Context, diff *schema.ResourceDiff, _ any) error {
580580
if computeEnvironmentType := strings.ToUpper(diff.Get(names.AttrType).(string)); computeEnvironmentType == string(awstypes.CETypeUnmanaged) {
581581
// UNMANAGED compute environments can have no compute_resources configured.
582582
if v, ok := diff.GetOk("compute_resources"); ok && len(v.([]any)) > 0 && v.([]any)[0] != nil {
@@ -668,6 +668,19 @@ func resourceComputeEnvironmentCustomizeDiff(_ context.Context, diff *schema.Res
668668
}
669669
}
670670

671+
// If the launch template version is unknown, set new value to ForceNew.
672+
if v := diff.GetRawPlan().GetAttr("compute_resources"); v.IsKnown() && v.LengthInt() == 1 {
673+
if v := v.AsValueSlice()[0].GetAttr(names.AttrLaunchTemplate); v.IsKnown() && v.LengthInt() == 1 {
674+
if v := v.AsValueSlice()[0].GetAttr(names.AttrVersion); !v.IsKnown() {
675+
out := expandComputeResource(ctx, diff.Get("compute_resources").([]any)[0].(map[string]any))
676+
out.LaunchTemplate.Version = aws.String(" ") // set version to a new empty value to trigger a replacement
677+
if err := diff.SetNew("compute_resources", []any{flattenComputeResource(ctx, out)}); err != nil {
678+
return err
679+
}
680+
}
681+
}
682+
}
683+
671684
if diff.HasChange("compute_resources.0.launch_template.0.launch_template_id") {
672685
if err := diff.ForceNew("compute_resources.0.launch_template.0.launch_template_id"); err != nil {
673686
return err

internal/service/batch/compute_environment_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,46 @@ func TestAccBatchComputeEnvironment_updateLaunchTemplate(t *testing.T) {
16091609
})
16101610
}
16111611

1612+
// https://github.com/hashicorp/terraform-provider-aws/issues/39470.
1613+
func TestAccBatchComputeEnvironment_updateLaunchTemplateID(t *testing.T) {
1614+
ctx := acctest.Context(t)
1615+
var ce awstypes.ComputeEnvironmentDetail
1616+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
1617+
resourceName := "aws_batch_compute_environment.test"
1618+
1619+
resource.ParallelTest(t, resource.TestCase{
1620+
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) },
1621+
ErrorCheck: acctest.ErrorCheck(t, names.BatchServiceID),
1622+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
1623+
CheckDestroy: testAccCheckComputeEnvironmentDestroy(ctx),
1624+
Steps: []resource.TestStep{
1625+
{
1626+
Config: testAccComputeEnvironmentConfig_launchTemplateWithVersion(rName, "foo"),
1627+
Check: resource.ComposeAggregateTestCheckFunc(
1628+
testAccCheckComputeEnvironmentExists(ctx, resourceName, &ce),
1629+
),
1630+
ConfigPlanChecks: resource.ConfigPlanChecks{
1631+
PreApply: []plancheck.PlanCheck{
1632+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionCreate),
1633+
},
1634+
},
1635+
},
1636+
// Swap to version 2 of the launch template
1637+
{
1638+
Config: testAccComputeEnvironmentConfig_launchTemplateWithVersion(rName, "bar"),
1639+
Check: resource.ComposeAggregateTestCheckFunc(
1640+
testAccCheckComputeEnvironmentExists(ctx, resourceName, &ce),
1641+
),
1642+
ConfigPlanChecks: resource.ConfigPlanChecks{
1643+
PreApply: []plancheck.PlanCheck{
1644+
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionReplace),
1645+
},
1646+
},
1647+
},
1648+
},
1649+
})
1650+
}
1651+
16121652
func TestAccBatchComputeEnvironment_UpdateSecurityGroupsAndSubnets_fargate(t *testing.T) {
16131653
ctx := acctest.Context(t)
16141654
var ce awstypes.ComputeEnvironmentDetail
@@ -2999,6 +3039,65 @@ resource "aws_batch_compute_environment" "test" {
29993039
`, rName, version))
30003040
}
30013041

3042+
func testAccComputeEnvironmentConfig_launchTemplateWithVersion(rName, userDataSeed string) string {
3043+
return acctest.ConfigCompose(testAccComputeEnvironmentConfig_base(rName), fmt.Sprintf(`
3044+
locals {
3045+
user_data = <<-EOF
3046+
Content-Type: multipart/mixed; boundary="//"
3047+
MIME-Version: 1.0
3048+
3049+
--//
3050+
Content-Type: text/x-shellscript; charset="us-ascii"
3051+
MIME-Version: 1.0
3052+
Content-Transfer-Encoding: 7bit
3053+
Content-Disposition: attachment; filename="userdata.txt"
3054+
3055+
#!/bin/bash
3056+
echo hello
3057+
echo %[2]q
3058+
--//--
3059+
EOF
3060+
}
3061+
3062+
resource "aws_launch_template" "test" {
3063+
name = %[1]q
3064+
user_data = base64encode(local.user_data)
3065+
}
3066+
3067+
resource "aws_batch_compute_environment" "test" {
3068+
name = %[1]q
3069+
3070+
compute_resources {
3071+
allocation_strategy = "SPOT_PRICE_CAPACITY_OPTIMIZED"
3072+
instance_role = aws_iam_instance_profile.ecs_instance.arn
3073+
instance_type = [
3074+
"c4.large",
3075+
]
3076+
3077+
launch_template {
3078+
launch_template_id = aws_launch_template.test.id
3079+
version = aws_launch_template.test.latest_version
3080+
}
3081+
3082+
max_vcpus = 16
3083+
min_vcpus = 0
3084+
security_group_ids = [
3085+
aws_security_group.test.id
3086+
]
3087+
spot_iam_fleet_role = aws_iam_role.ec2_spot_fleet.arn
3088+
subnets = [
3089+
aws_subnet.test.id
3090+
]
3091+
type = "SPOT"
3092+
}
3093+
3094+
service_role = aws_iam_role.batch_service.arn
3095+
type = "MANAGED"
3096+
depends_on = [aws_iam_role_policy_attachment.batch_service]
3097+
}
3098+
`, rName, userDataSeed))
3099+
}
3100+
30023101
func testAccComputeEnvironmentConfig_ec2Configuration(rName string) string {
30033102
return acctest.ConfigCompose(testAccComputeEnvironmentConfig_base(rName), acctest.ConfigLatestAmazonLinux2HVMEBSX8664AMI(), fmt.Sprintf(`
30043103
resource "aws_batch_compute_environment" "test" {

0 commit comments

Comments
 (0)