Skip to content
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
9 changes: 9 additions & 0 deletions .changelog/43937.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource/aws_batch_compute_environment: Add `userdata_type` support for launch templates

- Add `userdata_type` field to `LaunchTemplateSpecification` schema
- Support `EKS_BOOTSTRAP_SH` and `EKS_NODEADM` values
- Add validation for valid `userdata_type` values
- Update expand/flatten functions for proper AWS SDK mapping
- Add comprehensive unit and integration tests

Closes #43937
26 changes: 23 additions & 3 deletions internal/service/batch/compute_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ func resourceComputeEnvironment() *schema.Resource {
Optional: true,
Computed: true,
},
"userdata_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"EKS_BOOTSTRAP_SH",
"EKS_NODEADM",
}, false),
Description: "The EKS node initialization process to use. Only required if using a custom AMI. Valid values are EKS_BOOTSTRAP_SH or EKS_NODEADM.",
},
},
},
},
Expand Down Expand Up @@ -702,6 +711,11 @@ func resourceComputeEnvironmentCustomizeDiff(ctx context.Context, diff *schema.R
return err
}
}
if diff.HasChange("compute_resources.0.launch_template.0.userdata_type") {
if err := diff.ForceNew("compute_resources.0.launch_template.0.userdata_type"); err != nil {
return err
}
}

if diff.HasChange("compute_resources.0.tags") {
if err := diff.ForceNew("compute_resources.0.tags"); err != nil {
Expand Down Expand Up @@ -1065,7 +1079,9 @@ func expandLaunchTemplateSpecification(tfMap map[string]any) *awstypes.LaunchTem
if v, ok := tfMap["launch_template_name"].(string); ok && v != "" {
apiObject.LaunchTemplateName = aws.String(v)
}

if v, ok := tfMap["userdata_type"].(string); ok && v != "" {
apiObject.UserdataType = awstypes.UserdataType(v) // Map userdata_type to AWS SDK
}
if v, ok := tfMap[names.AttrVersion].(string); ok && v != "" {
apiObject.Version = aws.String(v)
}
Expand Down Expand Up @@ -1117,10 +1133,12 @@ func expandLaunchTemplateSpecificationUpdate(tfList []any) *awstypes.LaunchTempl
apiObject.LaunchTemplateId = aws.String(v)
}

if v, ok := tfMap["userdata_type"].(string); ok && v != "" {
apiObject.UserdataType = awstypes.UserdataType(v)
}
if v, ok := tfMap["launch_template_name"].(string); ok && v != "" {
apiObject.LaunchTemplateName = aws.String(v)
}

if v, ok := tfMap[names.AttrVersion].(string); ok {
apiObject.Version = aws.String(v)
} else {
Expand Down Expand Up @@ -1271,7 +1289,9 @@ func flattenLaunchTemplateSpecification(apiObject *awstypes.LaunchTemplateSpecif
if v := apiObject.LaunchTemplateName; v != nil {
tfMap["launch_template_name"] = aws.ToString(v)
}

if v := apiObject.UserdataType; v != "" {
tfMap["userdata_type"] = string(v)
}
if v := apiObject.Version; v != nil {
tfMap[names.AttrVersion] = aws.ToString(v)
}
Expand Down
141 changes: 71 additions & 70 deletions internal/service/batch/compute_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ func TestExpandLaunchTemplateSpecificationUpdate(t *testing.T) {
}
}
}

func TestAccBatchComputeEnvironment_basic(t *testing.T) {
ctx := acctest.Context(t)
var ce awstypes.ComputeEnvironmentDetail
Expand Down Expand Up @@ -320,7 +319,7 @@ func TestAccBatchComputeEnvironment_upgradeV0ToV1(t *testing.T) {
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_environment_name", ""),
),
},
{
Expand Down Expand Up @@ -2257,7 +2256,7 @@ resource "aws_batch_compute_environment" "test" {
func testAccComputeEnvironmentConfig_upgradeV0ToV1Legacy(rName string) string {
return acctest.ConfigCompose(testAccComputeEnvironmentConfig_base(rName), fmt.Sprintf(`
resource "aws_batch_compute_environment" "test" {
compute_environment_name = %[1]q
name = %[1]q

service_role = aws_iam_role.batch_service.arn
type = "UNMANAGED"
Expand Down Expand Up @@ -2559,9 +2558,6 @@ resource "aws_batch_compute_environment" "test" {

instance_type = ["m5.large"]

security_group_ids = [
aws_security_group.test.id
]
subnets = aws_subnet.test[*].id

instance_role = aws_iam_instance_profile.node.arn
Expand All @@ -2588,9 +2584,6 @@ resource "aws_batch_compute_environment" "test" {
"c4.large",
]
max_vcpus = 16
security_group_ids = [
aws_security_group.test.id
]
subnets = [
aws_subnet.test.id
]
Expand All @@ -2615,9 +2608,6 @@ resource "aws_batch_compute_environment" "test" {
instance_type = ["optimal"]
max_vcpus = 4
min_vcpus = 0
security_group_ids = [
aws_security_group.test.id
]
subnets = [
aws_subnet.test.id
]
Expand Down Expand Up @@ -2645,9 +2635,6 @@ resource "aws_batch_compute_environment" "test" {
instance_type = ["optimal"]
max_vcpus = 4
min_vcpus = 0
security_group_ids = [
aws_security_group.test.id
]
subnets = [
aws_subnet.test.id
]
Expand All @@ -2672,9 +2659,6 @@ resource "aws_batch_compute_environment" "test" {
max_vcpus = 16
min_vcpus = 4
desired_vcpus = 8
security_group_ids = [
aws_security_group.test.id
]
subnets = [
aws_subnet.test.id
]
Expand Down Expand Up @@ -2707,9 +2691,6 @@ resource "aws_batch_compute_environment" "test" {

compute_resources {
max_vcpus = 16
security_group_ids = [
aws_security_group.test.id
]
subnets = [
aws_subnet.test.id
]
Expand All @@ -2730,9 +2711,6 @@ resource "aws_batch_compute_environment" "test" {

compute_resources {
max_vcpus = 16
security_group_ids = [
aws_security_group.test.id
]
subnets = [
aws_subnet.test.id
]
Expand All @@ -2751,9 +2729,6 @@ resource "aws_batch_compute_environment" "test" {

compute_resources {
max_vcpus = 16
security_group_ids = [
aws_security_group.test.id
]
subnets = [
aws_subnet.test.id
]
Expand Down Expand Up @@ -2796,9 +2771,6 @@ resource "aws_batch_compute_environment" "test" {

compute_resources {
max_vcpus = 16
security_group_ids = [
aws_security_group.test.id
]
subnets = [
aws_subnet.test.id
]
Expand All @@ -2824,9 +2796,6 @@ resource "aws_batch_compute_environment" "test" {
]
max_vcpus = 16
min_vcpus = 2
security_group_ids = [
aws_security_group.test.id
]
spot_iam_fleet_role = aws_iam_role.ec2_spot_fleet.arn
subnets = [
aws_subnet.test.id
Expand Down Expand Up @@ -2855,9 +2824,6 @@ resource "aws_batch_compute_environment" "test" {
]
max_vcpus = 16
min_vcpus = 0
security_group_ids = [
aws_security_group.test.id
]
spot_iam_fleet_role = aws_iam_role.ec2_spot_fleet.arn
subnets = [
aws_subnet.test.id
Expand Down Expand Up @@ -2895,9 +2861,6 @@ resource "aws_batch_compute_environment" "test" {
instance_type = ["optimal"]
max_vcpus = %[2]d
min_vcpus = %[3]d
security_group_ids = [
aws_security_group.test.id
]
subnets = [
aws_subnet.test.id
]
Expand All @@ -2918,10 +2881,6 @@ resource "aws_batch_compute_environment" "test" {

compute_resources {
max_vcpus = 16
security_group_ids = [
aws_security_group.test_2.id,
aws_security_group.test_3.id,
]
subnets = [
aws_subnet.test_2.id
]
Expand Down Expand Up @@ -2986,9 +2945,6 @@ resource "aws_batch_compute_environment" "test" {
]
max_vcpus = 16
min_vcpus = 0
security_group_ids = [
aws_security_group.test.id
]
subnets = [
aws_subnet.test.id
]
Expand All @@ -3007,9 +2963,6 @@ func testAccComputeEnvironmentConfig_launchTemplate(rName string) string {
resource "aws_launch_template" "test" {
name = %[1]q

vpc_security_group_ids = [
aws_security_group.test.id
]
}

resource "aws_batch_compute_environment" "test" {
Expand Down Expand Up @@ -3063,9 +3016,6 @@ resource "aws_batch_compute_environment" "test" {

max_vcpus = 16
min_vcpus = 0
security_group_ids = [
aws_security_group.test.id
]
spot_iam_fleet_role = aws_iam_role.ec2_spot_fleet.arn
subnets = [
aws_subnet.test.id
Expand Down Expand Up @@ -3122,9 +3072,6 @@ resource "aws_batch_compute_environment" "test" {

max_vcpus = 16
min_vcpus = 0
security_group_ids = [
aws_security_group.test.id
]
spot_iam_fleet_role = aws_iam_role.ec2_spot_fleet.arn
subnets = [
aws_subnet.test.id
Expand Down Expand Up @@ -3162,9 +3109,6 @@ resource "aws_batch_compute_environment" "test" {
max_vcpus = 16
min_vcpus = 0

security_group_ids = [
aws_security_group.test.id
]
spot_iam_fleet_role = aws_iam_role.ec2_spot_fleet.arn
subnets = [
aws_subnet.test.id
Expand Down Expand Up @@ -3208,9 +3152,6 @@ resource "aws_batch_compute_environment" "test" {

placement_group = aws_placement_group.test.name

security_group_ids = [
aws_security_group.test.id
]
spot_iam_fleet_role = aws_iam_role.ec2_spot_fleet.arn
subnets = [
aws_subnet.test.id
Expand Down Expand Up @@ -3298,9 +3239,6 @@ resource "aws_batch_compute_environment" "test" {
"optimal",
]
max_vcpus = 16
security_group_ids = [
aws_security_group.test.id
]
spot_iam_fleet_role = aws_iam_role.ec2_spot_fleet.arn
subnets = [
aws_subnet.test.id
Expand Down Expand Up @@ -3340,9 +3278,6 @@ resource "aws_batch_compute_environment" "test" {
"c4.large",
]
max_vcpus = 16
security_group_ids = [
aws_security_group.test_2.id
]
spot_iam_fleet_role = aws_iam_role.ec2_spot_fleet.arn
subnets = [
aws_subnet.test_2.id
Expand Down Expand Up @@ -3384,9 +3319,6 @@ resource "aws_batch_compute_environment" "test" {
%[2]q,
]
max_vcpus = 16
security_group_ids = [
aws_security_group.test_2.id
]
spot_iam_fleet_role = aws_iam_role.ec2_spot_fleet.arn
subnets = [
aws_subnet.test_2.id
Expand All @@ -3398,3 +3330,72 @@ resource "aws_batch_compute_environment" "test" {
}
`, rName, instanceType))
}
func TestAccBatchComputeEnvironment_launchTemplateUserdataType(t *testing.T) {
ctx := acctest.Context(t)
var ce awstypes.ComputeEnvironmentDetail
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_batch_compute_environment.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.BatchServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckComputeEnvironmentDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccComputeEnvironmentConfig_launchTemplateUserdataType(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckComputeEnvironmentExists(ctx, resourceName, &ce),
resource.TestCheckResourceAttr(resourceName, "compute_resources.0.launch_template.0.userdata_type", "EKS_NODEADM"),
resource.TestCheckResourceAttr(resourceName, "compute_resources.0.ec2_configuration.0.image_type", "EKS_AL2023"),
),
},
},
})
}
func testAccComputeEnvironmentConfig_launchTemplateUserdataType(rName string) string {
return fmt.Sprintf(`
resource "aws_launch_template" "test" {
name_prefix = %[1]q

image_id = "ami-0c02fb55956c7d316"
key_name = "test-batch"

network_interfaces {
associate_public_ip_address = false
security_groups = ["sg-037850016f118f906", "sg-0a724e7ab6b472a2a"]
}
}
resource "aws_batch_compute_environment" "test" {
name = %[1]q

eks_configuration {
eks_cluster_arn = "arn:aws:eks:eu-north-1:009887377961:cluster/batch-eks-today"
kubernetes_namespace = "my-aws-batch-namespace"
}

compute_resources {
type = "EC2"
allocation_strategy = "BEST_FIT_PROGRESSIVE"
min_vcpus = 0
max_vcpus = 128
instance_type = ["m5"]
subnets = ["subnet-0610290e9f05a429f", "subnet-0af87ddfe49bfad7c", "subnet-07ffc3bf816a73899"]
instance_role = "arn:aws:iam::009887377961:instance-profile/eks-a8ccb345-6bda-6dcd-1f98-8759d9f38494"

ec2_configuration {
image_type = "EKS_AL2023"
image_id_override = "ami-0c02fb55956c7d316"
}
launch_template {
launch_template_name = aws_launch_template.test.name
version = aws_launch_template.test.latest_version
userdata_type = "EKS_NODEADM"
}
}

service_role = "arn:aws:iam::009887377961:role/aws-service-role/batch.amazonaws.com/AWSServiceRoleForBatch"
type = "MANAGED"
}
`, rName)
}
Loading