3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Fargateタスクを時間帯によって自動起動/停止させる方法

Posted at

はじめに

これまでは、毎朝始業前にFargateタスクをAWSコンソールから起動し、就業後に停止していました。しかし、うっかり停止を忘れてしまい、無駄にAWSの料金が発生することも……。
自動起動/停止機能の仕組みを作るにはLambdaなどの別サービスを使う必要があると思っていたのですが、実は サービスの自動スケーリング機能 を使うことで、Fargateタスクの自動起動/停止機能を時間帯ベースで簡単に構築できます。

CFnテンプレートについて

AWSコンソール上でポチポチ作るのも面倒だったので、CloudFormationを使って自動化の仕組みを構築しました。そのときに実際に利用したCFnテンプレートを載せておきます。

前提条件

以下のリソースが既に作成済みであることを前提としています

  • ECSクラスター
  • ECSサービス

テンプレートの中身

AWSTemplateFormatVersion: '2010-09-09'

Parameters:
  ECSClusterName:
    Type: String
    Description: A ecs cluster name.
  ECSServiceName:
    Type: String
    Description: A ecs service name.
  CreateECSServiceScalableTarget:
    Type: String
    AllowedValues:
      - true
      - false
    Default: true
    Description: Set to 'true' to create ECSServiceScalableTarget, 'false' to skip.
  CronForStartUp:
    Type: String
    Default: "cron(0 8 ? * MON-FRI *)" # 平日8時
    Description: A cron for startup ecs task.
  CronForShutDown:
    Type: String
    Default: "cron(0 19 ? * MON-FRI *)" # 平日19時
    Description: A cron for shutdown ecs task.

Conditions:
  ShouldCreateECSServiceScalableTarget: !Equals [!Ref CreateECSServiceScalableTarget, 'true']

Resources:
  ECSServiceScalableTarget:
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Condition: ShouldCreateECSServiceScalableTarget
    Properties:
      MaxCapacity: 1
      MinCapacity: 0
      ResourceId: !Sub
        - "service/${ECSCluster}/${ECSService}"
        - ECSCluster: !Ref ECSClusterName
          ECSService: !Ref ECSServiceName
      ScalableDimension: ecs:service:DesiredCount
      ServiceNamespace: ecs
      ScheduledActions:
        - ScheduledActionName: StartUp
          Schedule: !Ref CronForStartUp
          ScalableTargetAction:
            MinCapacity: 1
            MaxCapacity: 1
          Timezone: Asia/Tokyo
        - ScheduledActionName: ShutDown
          Schedule: !Ref CronForShutDown
          ScalableTargetAction:
            MinCapacity: 0
            MaxCapacity: 0
          Timezone: Asia/Tokyo

仕組みの概要

本構成では、以下のようにFargateタスクの起動と停止を自動化します。

  • 平日 8:00(JST):タスク数を 1 にスケールアウト(=起動)
  • 平日19:00(JST):タスク数を 0 にスケールイン(=停止)

これにより、業務時間中のみFargateタスクが稼働するようになり、不要な稼働時間を削減してコスト最適化を実現できます。
起動・停止の時刻は、CloudFormationのパラメータで cron式 によって自由に変更可能です。業務時間に合わせて調整してください。
また、テンプレートには CreateECSServiceScalableTarget というパラメータがあります。これを false に設定することで、自動起動・停止の仕組み自体を無効化することができます。

👇こんな使い方がおすすめ

  • 長期休暇前CreateECSServiceScalableTarget = false にして、自動起動/停止の仕組みを停止しておく(タスク数が0になっているかを事前に確認しておくこと)
  • 休暇明けCreateECSServiceScalableTarget = true に戻せば、自動起動/停止の機能が再び有効になります

さいごに

今回紹介した方法を活用することで、Fargateタスクを時間帯に応じて自動起動・停止する仕組みを、Lambdaなどを使わずに簡潔に構築できます。
日々の運用コスト削減や手間の軽減にもつながるので、ぜひ試してみてください!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?