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

備忘録: ECSのタスク定義がどのクラスターと紐づいているか確認するためのスクリプト

Posted at

はじめに

ECS のクラスターが増えてきて、「このタスク定義はどのクラスターで動いてたっけ?」となることがよくあったので、AWS SDK for Ruby (v3) を用いて、ECS の全クラスターからサービス名と紐づくタスク定義を取得し、その結果を Markdown 表形式で出力するスクリプトを作成しました。

前提

  • Ruby 2.5 以上
  • aws-sdk-ecs gem(v3 系)をインストール済み
  • AWS CLI などで認証情報が設定済み(~/.aws/credentials や環境変数)
  • 実行環境に対して ECS の ListClusters / ListServices / DescribeServices 権限が付与されている IAM ロール/ユーザー
  • aws-sdk-ecs をインストール
$ gem install aws-sdk-ecs

スクリプト全体

#!/usr/bin/env ruby
# encoding: utf-8

require 'aws-sdk-ecs'  # v3
require 'optparse'

# オプションの解析
options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: list_ecs_services.rb [options]"
  opts.on("-r", "--region REGION", "AWS リージョン (例: ap-northeast-1)") do |r|
    options[:region] = r
  end
  opts.on("-h", "--help", "このヘルプを表示") do
    puts opts
    exit
  end
end.parse!

# ECS クライアントの初期化
ecs = Aws::ECS::Client.new(region: options[:region])

# 1) 全クラスター ARN を取得
cluster_arns = []
next_token = nil
begin
  loop do
    resp = ecs.list_clusters(max_results: 100, next_token: next_token)
    cluster_arns.concat(resp.cluster_arns)
    next_token = resp.next_token
    break unless next_token
  end
rescue => e
  warn "Error listing clusters: #{e.message}"
  exit 1
end

# 2) 各クラスターのサービス情報を集める
results = []
cluster_arns.each do |cluster_arn|
  cluster_name = cluster_arn.split('/').last

  # list_services でサービス ARN を取得
  service_arns = []
  token = nil
  begin
    loop do
      resp = ecs.list_services(cluster: cluster_arn, max_results: 100, next_token: token)
      service_arns.concat(resp.service_arns)
      token = resp.next_token
      break unless token
    end
  rescue => e
    warn "Error listing services for #{cluster_name}: #{e.message}"
    next
  end

  # describe_services でサービス詳細(名前・タスク定義)を取得
  service_arns.each_slice(10) do |batch|
    begin
      ds = ecs.describe_services(cluster: cluster_arn, services: batch)
      ds.services.each do |svc|
        results << {
          cluster:         cluster_name,
          service:         svc.service_name,
          task_definition: svc.task_definition
        }
      end
    rescue => e
      warn "Error describing services for #{cluster_name}: #{e.message}"
      next
    end
  end
end

# 3) 出力前にソート(クラスター名 → サービス名)
results.sort_by! { |r| [r[:cluster], r[:service]] }

# 4) マークダウン表で出力
puts "| Cluster | Service | Task Definition |"
puts "| ------- | ------- | ---------------- |"
results.each do |r|
  puts "| #{r[:cluster]} | #{r[:service]} | #{r[:task_definition]} |"
end

実行方法

$ ruby list_ecs_services.rb -r ap-northeast-1 > ecs_services.md
0
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
0
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?