Skip to content
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

Allow empty string as valid setting for launch_type in CloudWatchEventTarget #19555

Merged
merged 7 commits into from
Jun 10, 2021
Merged
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/19555.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_cloudwatch_event_target: Fix `ecs_target.launch_type` not allowing empty string values.
```
11 changes: 7 additions & 4 deletions aws/resource_aws_cloudwatch_event_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
160 changes: 160 additions & 0 deletions aws/resource_aws_cloudwatch_event_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "events.${data.aws_partition.current.dns_suffix}"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

resource "aws_iam_role_policy" "test" {
name = %[1]q
role = aws_iam_role.test.id

policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecs:RunTask"
],
"Resource": [
"*"
]
}
]
}
EOF
}

resource "aws_ecs_cluster" "test" {
name = %[1]q
}

resource "aws_ecs_task_definition" "task" {
family = %[1]q
cpu = 256
memory = 512
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"

container_definitions = <<EOF
[
{
"name": "first",
"image": "service-first",
"cpu": 10,
"memory": 512,
"essential": true
}
]
EOF
}

data "aws_partition" "current" {}
`, rName)
}

func testAccAWSCloudWatchEventTargetConfigEcsWithBlankTaskCount(rName string) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_event_rule" "test" {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/cloudwatch_event_target.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ The following arguments are supported:
`ecs_target` support the following:

* `group` - (Optional) Specifies an ECS task group for the task. The maximum length is 255 characters.
* `launch_type` - (Optional) Specifies the launch type on which your task is running. The launch type that you specify here must match one of the launch type (compatibilities) of the target task. Valid values are `EC2` or `FARGATE`.
* `launch_type` - (Optional) Specifies the launch type on which your task is running. The launch type that you specify here must match one of the launch type (compatibilities) of the target task. Valid values include: an empty string `""` (to specify no launch type), `EC2`, or `FARGATE`.
* `network_configuration` - (Optional) Use this if the ECS task uses the awsvpc network mode. This specifies the VPC subnets and security groups associated with the task, and whether a public IP address is to be used. Required if launch_type is FARGATE because the awsvpc mode is required for Fargate tasks.
* `platform_version` - (Optional) Specifies the platform version for the task. Specify only the numeric portion of the platform version, such as 1.1.0. This is used only if LaunchType is FARGATE. For more information about valid platform versions, see [AWS Fargate Platform Versions](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/platform_versions.html).
* `task_count` - (Optional) The number of tasks to create based on the TaskDefinition. The default is 1.
Expand Down