LoginSignup
13
9

More than 5 years have passed since last update.

Run Fargate task by StepFunctions

Posted at

StepFunctionsからFargateが起動できるようになってました(2018年12月末くらい)

より簡単にFargateを利用しやすくて良いですね。

Fargateを使うための下準備は以下のようなもの。

  • 接続対象のVPC、サブネット、ルートテーブル
  • ECR
  • ECS(クラスタ定義、タスク定義 ※並列で処理しないならサービス定義は不要)
  • StepFunctionsにFargateタスク連携用のアクティビティを登録(※ポーリング処理でタスク実行しているなら必要)

下記のようにStepFunctionsのステートマシンを設定すれば、

  1. StepFunctionsの1つ目のStepがFargateタスクを起動(起こすだけ)
  2. 2つ目のStepで、アクティビティに紐づくFargateタスクを起動(処理本体はこちら)

といったことが可能です。


{
    "Comment": "Run Fargate Task Statemacine",
    "StartAt": "Run Fargate Task",
    "TimeoutSeconds": 3600,
    "States": {
        "Run Fargate Task": {
            "Type": "Task",
            "Resource": "arn:aws:states:::ecs:runTask",
            "Parameters": {
                "LaunchType": "FARGATE",
                "Cluster": "sample-cluster",
                "TaskDefinition": "sample-task",
                "NetworkConfiguration": {
                    "AwsvpcConfiguration": {
                        "Subnets": [
                            "subnet-0123456789abcdefg"
                        ],
                        "AssignPublicIp": "ENABLED"
                    }
                }
            },
            "Next": "sayHello"
        },    
      "sayHello": {
      "Type": "Task",
      "Resource": "arn:aws:states:ap-northeast-1:9999999999999:activity:sample-activity",
      "End": true
    }
    }
}

Fargateタスクを起こすと同時に任意処理を完了するまで待ちたい場合は、
”Resource”末尾に"sync"を添えるとStepは次に進まずに待機してくれます。


"Resource": "arn:aws:states:::ecs:runTask",

"Resource": "arn:aws:states:::ecs:runTask.sync",

以上です。

※公式ドキュメントの試せばわかるべ的な突き放し感はいつもとおり。
https://docs.aws.amazon.com/ja_jp/step-functions/latest/dg/connectors-ecs.html

13
9
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
13
9