diff --git a/.changelog/19555.txt b/.changelog/19555.txt new file mode 100644 index 00000000000..161bc3f9034 --- /dev/null +++ b/.changelog/19555.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_cloudwatch_event_target: Fix `ecs_target.launch_type` not allowing empty string values. +``` diff --git a/aws/resource_aws_cloudwatch_event_target.go b/aws/resource_aws_cloudwatch_event_target.go index d4c60d91cb8..f4dbe285114 100644 --- a/aws/resource_aws_cloudwatch_event_target.go +++ b/aws/resource_aws_cloudwatch_event_target.go @@ -143,10 +143,13 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource { ValidateFunc: validation.StringLenBetween(1, 255), }, "launch_type": { - Type: schema.TypeString, - Optional: true, - Default: events.LaunchTypeEc2, - ValidateFunc: validation.StringInSlice(events.LaunchType_Values(), false), + Type: schema.TypeString, + Optional: true, + Default: events.LaunchTypeEc2, + ValidateFunc: validation.Any( + validation.StringIsEmpty, + validation.StringInSlice(events.LaunchType_Values(), false), + ), }, "network_configuration": { Type: schema.TypeList, diff --git a/aws/resource_aws_cloudwatch_event_target_test.go b/aws/resource_aws_cloudwatch_event_target_test.go index 987a4b7eaf3..e738b3964b7 100644 --- a/aws/resource_aws_cloudwatch_event_target_test.go +++ b/aws/resource_aws_cloudwatch_event_target_test.go @@ -451,6 +451,62 @@ func TestAccAWSCloudWatchEventTarget_ecs(t *testing.T) { }) } +func TestAccAWSCloudWatchEventTarget_ecsWithBlankLaunchType(t *testing.T) { + resourceName := "aws_cloudwatch_event_target.test" + iamRoleResourceName := "aws_iam_role.test" + ecsTaskDefinitionResourceName := "aws_ecs_task_definition.task" + var v events.Target + rName := acctest.RandomWithPrefix("tf_ecs_target") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, events.EndpointsID), + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudWatchEventTargetConfigEcsWithBlankLaunchType(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudWatchEventTargetExists(resourceName, &v), + resource.TestCheckResourceAttrPair(resourceName, "role_arn", iamRoleResourceName, "arn"), + resource.TestCheckResourceAttr(resourceName, "ecs_target.#", "1"), + resource.TestCheckResourceAttr(resourceName, "ecs_target.0.task_count", "1"), + resource.TestCheckResourceAttrPair(resourceName, "ecs_target.0.task_definition_arn", ecsTaskDefinitionResourceName, "arn"), + resource.TestCheckResourceAttr(resourceName, "ecs_target.0.launch_type", ""), + resource.TestCheckResourceAttr(resourceName, "ecs_target.0.network_configuration.#", "1"), + resource.TestCheckResourceAttr(resourceName, "ecs_target.0.network_configuration.0.subnets.#", "1"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc(resourceName), + ImportStateVerify: true, + }, + { + Config: testAccAWSCloudWatchEventTargetConfigEcs(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudWatchEventTargetExists(resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "ecs_target.0.launch_type", "FARGATE"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc(resourceName), + ImportStateVerify: true, + }, + { + Config: testAccAWSCloudWatchEventTargetConfigEcsWithBlankLaunchType(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudWatchEventTargetExists(resourceName, &v), + resource.TestCheckResourceAttr(resourceName, "ecs_target.0.launch_type", ""), + ), + }, + }, + }) +} + func TestAccAWSCloudWatchEventTarget_ecsWithBlankTaskCount(t *testing.T) { resourceName := "aws_cloudwatch_event_target.test" var v events.Target @@ -1275,6 +1331,110 @@ data "aws_partition" "current" {} `, rName) } +func testAccAWSCloudWatchEventTargetConfigEcsWithBlankLaunchType(rName string) string { + return fmt.Sprintf(` +resource "aws_cloudwatch_event_rule" "test" { + name = %[1]q + description = "schedule_ecs_test" + + schedule_expression = "rate(5 minutes)" +} + +resource "aws_vpc" "vpc" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_subnet" "subnet" { + vpc_id = aws_vpc.vpc.id + cidr_block = "10.1.1.0/24" +} + +resource "aws_cloudwatch_event_target" "test" { + arn = aws_ecs_cluster.test.id + rule = aws_cloudwatch_event_rule.test.id + role_arn = aws_iam_role.test.arn + + ecs_target { + task_count = 1 + task_definition_arn = aws_ecs_task_definition.task.arn + launch_type = "" + + network_configuration { + subnets = [aws_subnet.subnet.id] + } + } +} + +resource "aws_iam_role" "test" { + name = %[1]q + + assume_role_policy = <