Hi there,
I am trying to create an ECS Scheduled Task with Terraform.
terraform -v
Terraform v0.10.0
Please list the resources as a list, for example:
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)"
}
I would expect my scheduled task to be created.
* 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).
terraform apply
N/A
N/A
So I was able to create the task, the documentation was a bit unclear.
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).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!
Most helpful comment
So I was able to create the task, the documentation was a bit unclear.
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).role_arn
with a role suitable for the task.The Terraform Config should be like: