Terraform-provider-aws: What arn should one use in aws_cloudwatch_event_target if using ecs_target?

Created on 22 Aug 2017  ยท  4Comments  ยท  Source: hashicorp/terraform-provider-aws

Hi there,

I am trying to create an ECS Scheduled Task with Terraform.

Terraform Version

terraform -v
Terraform v0.10.0

Affected Resource(s)

Please list the resources as a list, for example:

  • aws_cloudwatch_event_target

Terraform Configuration Files

resource "aws_cloudwatch_log_group" "scheduled-job" {
  name = "scheduled-job"
}

data "template_file" "scheduled-job_task_definition" {
  template = "${file("${path.module}/scheduled-job.json")}"
  vars {
    log_group_name = "${aws_cloudwatch_log_group.scheduled-job.name}"
    log_group_region = "${var.region}"
    log_stream_prefix = "scheduled-job_"
  }
}

resource "aws_ecs_task_definition" "scheduled-job" {
  family = "scheduled-job"
  container_definitions = "${data.template_file.scheduled-job_task_definition.rendered}"
}

resource "aws_cloudwatch_event_target" "scheduled-job" {
  target_id = "scheduled-job"
  rule      = "${aws_cloudwatch_event_rule.scheduled-job.name}"
  arn = "${aws_ecs_task_definition.scheduled-job.arn}"
  ecs_target {
    task_count = 1
    task_definition_arn = "${aws_ecs_task_definition.scheduled-job.arn}"
  }
}

resource "aws_cloudwatch_event_rule" "scheduled-job" {
  name        =  "scheduled-job"
  description = "Run a scheduled job every 1 hours"
  schedule_expression = "rate(1 hour)"
}

Expected Behavior

I would expect my scheduled task to be created.

Actual Behavior

* aws_cloudwatch_event_target.scheduled_job: Creating CloudWatch Event Target failed: ValidationException: Parameter arn:aws:ecs:[region]:[accountid]:task-definition/scheduled-job:1 is not valid. Reason: Provided Arn is not in correct format.

I think the error is correct, it is not a correct format (note the last colon between task definition name and version).
I am also not sure what ARN should be specified here, all examples I found used Lambda or Kinesis, where the ARN to be used is obvious.
The ARN also cannot be omitted, as it is required.
The desired Scheduled Task can be manually configured under the ECS Service (on the Console).

Steps to Reproduce

  1. Create a scheduled task like in the snipped above.
  2. terraform apply

Important Factoids

N/A

References

N/A

question

Most helpful comment

So I was able to create the task, the documentation was a bit unclear.

  • The arn is the ID ( .id, as it does not have .arn) of the ECS Cluster the Task will be ran in (and not the ARN of the Task Defninition).
  • Must provide role_arn with a role suitable for the task.

The Terraform Config should be like:

resource "aws_cloudwatch_log_group" "scheduled-job" {
  name = "scheduled-job"
}

data "template_file" "scheduled-job_task_definition" {
  template = "${file("${path.module}/scheduled-job.json")}"
  vars {
    log_group_name = "${aws_cloudwatch_log_group.scheduled-job.name}"
    log_group_region = "${var.region}"
    log_stream_prefix = "scheduled-job_"
  }
}

resource "aws_ecs_task_definition" "scheduled-job" {
  family = "scheduled-job"
  container_definitions = "${data.template_file.scheduled-job_task_definition.rendered}"
}

resource "aws_cloudwatch_event_target" "scheduled-job" {
  target_id = "scheduled-job"
  rule      = "${aws_cloudwatch_event_rule.scheduled-job.name}"
  arn = "${aws_ecs_cluster.my_luvly_cluster.id}"
  role_arn = ""${aws_iam_role.my_role_for_this.arn}""
  ecs_target {
    task_count = 1
    task_definition_arn = "${aws_ecs_task_definition.scheduled-job.arn}"
  }
}

resource "aws_cloudwatch_event_rule" "scheduled-job" {
  name        =  "scheduled-job"
  description = "Run a scheduled job every 1 hours"
  schedule_expression = "rate(1 hour)"
}

All 4 comments

So I was able to create the task, the documentation was a bit unclear.

  • The arn is the ID ( .id, as it does not have .arn) of the ECS Cluster the Task will be ran in (and not the ARN of the Task Defninition).
  • Must provide role_arn with a role suitable for the task.

The Terraform Config should be like:

resource "aws_cloudwatch_log_group" "scheduled-job" {
  name = "scheduled-job"
}

data "template_file" "scheduled-job_task_definition" {
  template = "${file("${path.module}/scheduled-job.json")}"
  vars {
    log_group_name = "${aws_cloudwatch_log_group.scheduled-job.name}"
    log_group_region = "${var.region}"
    log_stream_prefix = "scheduled-job_"
  }
}

resource "aws_ecs_task_definition" "scheduled-job" {
  family = "scheduled-job"
  container_definitions = "${data.template_file.scheduled-job_task_definition.rendered}"
}

resource "aws_cloudwatch_event_target" "scheduled-job" {
  target_id = "scheduled-job"
  rule      = "${aws_cloudwatch_event_rule.scheduled-job.name}"
  arn = "${aws_ecs_cluster.my_luvly_cluster.id}"
  role_arn = ""${aws_iam_role.my_role_for_this.arn}""
  ecs_target {
    task_count = 1
    task_definition_arn = "${aws_ecs_task_definition.scheduled-job.arn}"
  }
}

resource "aws_cloudwatch_event_rule" "scheduled-job" {
  name        =  "scheduled-job"
  description = "Run a scheduled job every 1 hours"
  schedule_expression = "rate(1 hour)"
}

I close it, as the answer was found.

hey this came in handy for me, especially the fact that the arn in the aws_cloudwatch_event_target turned out to be the cluster id, rather than the task arn. thanks!

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings