LoginSignup
1
1

More than 1 year has passed since last update.

ECSで定期実行するタスクをTerraformで作成する際のTips

Last updated at Posted at 2023-03-22

AmazonECSにおいて、タスクを定期的に実行することができます。
その際に考慮すべきポイントが2点あります。

Terraform v1.4.2
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v4.48.0
+ provider registry.terraform.io/hashicorp/random v3.4.3

①cloudwatch_event_targetが依存関係を参照できないのでやむを得ず明示的にdepends_onを使用する

cloudwatch_event_rule.tf
resource "aws_cloudwatch_event_rule" "example" {
  # ...
}

resource "aws_cloudwatch_event_target" "example" {
  # ...
  depends_on = [
    aws_cloudwatch_event_rule.example,
  ]
}

②最新のタスクが実行できるよう同期を取るため、リビジョン番号は記述しないようにする

cloudwatch_event_rule.tf
// リビジョン番号を削除
locals {
  task_definition_arn_without_revision = replace(
    aws_ecs_task_definition.example_latest.arn, "/:[0-9]+$/", ""
  )
}

  ecs_target {
    task_count          = 1
    task_definition_arn = data.aws_ecs_task_definition.example_latest.arn
    launch_type         = "FARGATE"
  # ...
data.tf
data "aws_ecs_task_definition" "example_latest" {
  task_definition = aws_ecs_task_definition.example.family
}

参考
Terraformを使って常に最新のタスク定義でECS Scheduled Taskを実行する

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1