2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ecscheduleでスケジュールタスクを管理

Last updated at Posted at 2024-06-22

はじめに

ECSのスケジュールタスクを管理するためのOSS「ecschedule」が非常に便利だったので紹介します。ecschedule単体でもスケジュールタスクの管理が可能ですが、ecspressoとGithub Actionsを組み合わせることで、さらに効率的にスケジュールタスクを管理できるようになります。
今回はecscheduleの概要と基本的な使い方を紹介し、別の記事でecspressoとGithub Actionsを組み合わせてスケジュールタスクを自動更新する方法を書きたいと思います。

書くこと

  • ecscheduleの基本的な使い方

前提

  • AWS上に既にECS環境が立ち上がっていて、スケジュールタスクも設定されている前提で書いていきます

ecscheduleとは

ecscheduleは、ECSのスケジュールタスクをコードで管理するためのツールです。ecscheduleはECSのデプロイツールであるecspressoにインスパイアされており、ecspressoライクに設定ファイルを書くことができます。
一般的には、ecspressoでタスク定義やサービス定義を更新し、ecscheduleでスケジュールタスクを更新するという使い方がよくされています。

ecscheduleのインストール方法

まずはローカル環境にecscheduleをインストールしていきます。
インストール方法はbrewを使用した方法とgoを使った方法の2種類が用意されています。
お好きな方でローカルにインストールしてください。

% brew install Songmu/tap/ecschedule
# or
% go install github.com/Songmu/ecschedule/cmd/ecschedule@latest

ecscheduleの基本操作

ecscheduleの基本的な使い方について説明します。現行の設定に変更を加えるケースを想定して、基本的な手順を以下に示します。具体的な流れは以下の通りです。

  1. 現行の設定を取り込む
  2. 設定を修正する
  3. 修正内容の差分を確認する
  4. 設定を更新する

設定ファイルの取り込み

ecschedule dumpコマンドで管理したいスケジュールタスクの設定を取り込みます。

ecschedule dump --cluster クラスター名 --region ap-northeast-1 > ecschedule.yaml

上記コマンドを実行するとecschedule.yamlに以下のようにスケジュールタスクの設定が取り込まれます。

ecschedule.yaml
region: ap-northeast-1
cluster: ecshedule-test
rules:
- name: ecschedule-example
  scheduleExpression: rate(10 minutes)
  disabled: true
  targetId: test
  taskDefinition: ecschedule-test:5
  launch_type: FARGATE
  platform_version: LATEST
  network_configuration:
    aws_vpc_configuration:
      subnets:
      - subnet-040d07d987712387f
      - subnet-07863719a58ffd006
      - subnet-05b36af1097805832
      security_groups:
      - sg-066fdfb7c5ff72931
      assign_public_ip: ENABLED

変更差分の確認

設定ファイルを見るとスケジュールタスクecschedule-exampleが10分毎に稼働していますが、こちらのルールを無効化したいとします。
その場合、以下のように設定ファイルを修正します。

ecschedule.yaml
region: ap-northeast-1
cluster: ecshedule-test
rules:
- name: ecschedule-example
  scheduleExpression: rate(10 minutes)
  disabled: true #ルールを無効化
  targetId: test
  taskDefinition: ecschedule-test:5
  launch_type: FARGATE
  platform_version: LATEST
  network_configuration:
    aws_vpc_configuration:
      subnets:
      - subnet-040d07d987712387f
      - subnet-07863719a58ffd006
      - subnet-05b36af1097805832
      security_groups:
      - sg-066fdfb7c5ff72931
      assign_public_ip: ENABLED

diffコマンドで変更内容の差分を確認します

ecschedule diff -conf ecschedule.yaml --all

diffコマンドを実行すると変更箇所が色付きで表示されます。
出力結果
スクリーンショット 2024-06-22 17.02.48.png

設定の更新

変更内容に間違いがないことを確認したら、applyコマンドを実行して設定を更新します

ecschedule apply -conf ecschedule.yaml --all

出力結果
スクリーンショット 2024-06-22 17.39.10.png

following rule applied以下に変更したルールが表示されていれば反映完了です。

まとめ

今回はecsheduleの基本的な使い方について紹介しました。
ecscheduleは他にもルールを実行するrunコマンドが用意されていたり、tfstateからサブネットやセキュリティグループのARNを取得することができたり、いろんな機能が存在しています。より詳細な情報は公式リポジトリで確認してみてください。

別記事でecscheduleとecspresso、Github Actionsを使用したスケジュールタスクの自動更新方法について書きたいと思います。

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?