LoginSignup
6
5

More than 3 years have passed since last update.

ECS Serviceを自動起動・停止するAutomation

Last updated at Posted at 2021-03-28

(2021/4/5追記) APIリファレンスのリンクを追記
(2021/4/4追記) IAMRoleの作成方法とCLIでの作成手順を追記しました。

AWSではEC2やRDSの自動起動・停止を行うためにAutomationという機能がデフォルトで用意されています。CloudwatchEventと組み合わせるだけで簡単に夜間や休日の停止が実現でき、コスト削減が実現できます。

ただ、残念ながら自分がよく使うFargate(ECS)ではそのようなAutomationがなかったため、Runbookを自分で作りました。
(起動はタスク数を1以上に、停止はタスク数を0にする形で実現しています)

どのように記載するかのリファレンスが少なかったですが、 Boto3のSDK AWSのAPI を内部で呼び出しているだけのようだったので特にハマることなく実現できました。

(2021/4/5追記)変数名などはAPIリファレンスを参照してください。
https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_UpdateService.html

Automation作成方法

SystemsManager > 共有リソース > ドキュメント にて 「オートメーションを作成する」で エディタにYamlを貼り付けるだけ。

EcsServiceStart Runbook

EcsServiceStart.yaml
description: ecs service start automation runbook
schemaVersion: '0.3'
parameters:
  EcsClusterName:
    type: String
  EcsServiceName:
    type: String
  DesiredCount:
    type: Integer
    default: 1
mainSteps:
  - name: ECS
    action: 'aws:executeAwsApi'
    inputs:
      Service: ecs
      Api: UpdateService
      cluster: '{{ EcsClusterName }}'
      service: '{{ EcsServiceName }}'
      desiredCount: '{{ DesiredCount }}'

EcsServiceStop Runbook

EcsServiceStop.yaml
description: ecs service stop automation runbook
schemaVersion: '0.3'
parameters:
  EcsClusterName:
    type: String
  EcsServiceName:
    type: String
mainSteps:
  - name: ECS
    action: 'aws:executeAwsApi'
    inputs:
      Service: ecs
      Api: UpdateService
      cluster: '{{ EcsClusterName }}'
      service: '{{ EcsServiceName }}'
      desiredCount: 0

スケジュール実行設定

作成したAutomationをCloudwatchEventsで自動化します。

Cloudwatch > イベント > ルール で イベントソースを cron式 に、ターゲットを 作成したSSM Automation にするだけです。ここでCluster名やService名も指定しましょう。

できあがり。

実行用のIAMロールの作成 (2021/4/4追記)

SsmAutomationRoleという名前で作成。

  • 信頼関係 : ID プロバイダー events.amazonaws.com
  • ロール
    • AmazonSSMAutomationRole
    • AmazonECS_FullAccess (より限定的にすることも可能)

CLIでの設定例 (2021/4/4追記)

Automationの作成

aws ssm create-document \
  --name EcsServiceStart \
  --content file://EcsServiceStart.yaml \
  --document-format YAML \
  --document-type Automation

CloudwatchEventsのルールおよびTarget作成

AM9:00に起動する例

aws events put-rule --schedule-expression "cron(0 0 * * ? *)" --name "RetoolDailyStartAutomationRule"

aws events put-targets --rule RemoteAppDailyStartAutomationRule --targets file://EcsServiceStartTarget.json

タスク数はデフォルトの1となる。Inputの値に [] を付与する必要がある点がわかりにくかったです。複数のTargetが必要な場合はJson内にもう一つ設定を追記してください。

EcsServiceStartTarget.json
[
  {
    "Id": "id1",
    "Arn": "arn:aws:ssm:ap-northeast-1:{アカウント名}:automation-definition/EcsServiceStart",
    "RoleArn": "arn:aws:iam::{アカウント名}:role/SsmAutomationRole",
    "Input": "{\"EcsClusterName\":[\"{クラスター名}\"], \"EcsServiceName\":[\"{サービス名}\"]}"
  }
]
6
5
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
6
5