完全マネージド型のコンテナオーケストレーションサービス(Container Orchestration Service)。
オーケストレーションツールにはECSのほかに以下がある。
注意事項。
ECSではサービスやスケジュールタスクの他に1回限りのタスクも作成できるタスク(サービス/スケジュール)を実行するVPCにはインターネットゲートウェイを設定する必要があるタスク定義名を英語ではFamilyを呼ぶ。

DockerfileのCMDコマンドを上書きする。
コマンドをカンマ,で区切る必要がある。
php,Receiver.phpのように上書きすると["php","Receiver.php"]として上書きされるphp Receiver.phpのように上書きすると["php Receiver.php"]として上書きされてエラーになるECSサービスAWS::ECS::Service、スケジュールタスクAWS::Events::RuleともにAssignPublicIp: ENABLEDでないとエラーが発生する。
具体的には、イメージを取得できずにコンテナを起動できない。
停止理由 ResourceInitializationError: unable to pull secrets or registry auth: execution resource retrieval failed: unable to retrieve secrets from ssm: service call has been retried 5 time(s): RequestCanceled: request context canceled caused by: context deadli...
unable to retrieve secrets from ssmという記載もあるがコンテナイメージをECR(registry auth)から取得できずに起動できないのが理由。
スケジュールされたタスクを作成するとCloudWatch(EventBridge) > ルールにルールが作成される。
つまりスケジュールされたタスクはパターンにスケジュール、ターゲットにECSタスクを指定したルールと等価。
Webマネジメントコンソールからルールを作成する場合は、ルール名が必須。
CloudFormationのAWS::Events::RuleはProperties.Nameがオプション。
AWS::Events::RuleのProperties.Name(ルール名)はオプション(省略した場合にどのようなルール名になるかを要確認)idが重複した場合は上書きされるTarget.id。
The ID of the target. We recommend using a memorable and unique string.
パブリックIPの自動割当をENABELEDにしないとECRからのpullするときにエラーが発生する。
ResourceInitializationError: unable to pull secrets or registry auth: pull command failed: : signal: killed
ref. https://forums.aws.amazon.com/thread.jspa?threadID=339634&tstart=0
エラーはクラスター>タスク>StoppedでSTOPPEDしたタスクを調べればわかる。
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/stopped-task-errors.html

ALBを使用してAWS::ECS::Serviceを稼働している場合は、ALBのターゲットグループにサービスを追加する。
(ALBのリスナー > ルールで↑のターゲットグループを指定する)
FooSebService:
Type: AWS::ECS::Service
Properties:
#...
LoadBalancers:
- TargetGroupArn: {{targetGroupArn}}
スケジュールされたタスクを作成するとCloudWatch > ルールにルール(AWS::Events::Rule)が作成されるルールを追加するとCloudWoatch Events/EventBridgeによってScheduleExpressionで指定した時刻にクラスターとidで指定したルールが実行される。
FooSchedule:
Type: AWS::Events::Rule
Properties:
Description: "For Schedule Task"
ScheduleExpression: "cron(*/15 * * * ? *)" # 15分ごと
#...
Targets:
- Arn:{{クラスターARN}}
Id: "foo-schedule"
以下のリソースをWebマネジメントコンソールやCloudFormationによって作成済みと仮定する。
aws ecs run-task \
--cluster hello-world-cluster \
--launch-type FARGATE \
--network-configuration awsvpcConfiguration="{subnets=subnet-xxxxxxxxxx,securityGroups=sg-xxxxxxxxxx,assignPublicIp=ENABLED}" \
--propagate-tags TASK_DEFINITION \
--task-definition hello-world-task-definition
containerOverridesのcommandで、コンテナイメージのCMDを上書きする。
ENTRYPOINTを上書きできるかは要調査。
aws ecs run-task \
--cluster hello-world-cluster \
--launch-type FARGATE \
--network-configuration awsvpcConfiguration="{subnets=subnet-xxxxxxxxxx,securityGroups=sg-xxxxxxxxxx,assignPublicIp=ENABLED}" \
--propagate-tags TASK_DEFINITION \
--task-definition hello-world-task-definition \
--overrides '{"containerOverrides":[{"name":"HelloWorld","command":["echo", "Hello AWS ECS"]}]}'
Dockerfile
ROM alpine
CMD ["echo", "Hello World!"] # これを↑の"command":["echo", "AWS ECS"]で上書き
AWS ECSのサーバレスな実行環境(データプレーン)。
AWS ECSの実行環境としてEC2とFargateがある。
EC2の場合はコンテナを実行するためのOSレベルの面倒を見る必要がある。
Fargateはマネージドサービスなので面倒を見る必要がない
Fargateはコンテナが稼動している間のみ課金される(サーバレス)。
ECSでFargate起動タイプを使用する場合は、awsvpcモード を利用することとなる。
- タスク毎にENIが自動的に割り当てられる。
ref.
ref. https://garafu.blogspot.com/2018/11/release-strategy.html#blue-green
Secretsに渡すSSMパラメータはコンテナ内では環境変数として受け取られる(これが基本)Environmentとの違いは、 SecretsはSSMパラメータなどのセキュアなサービスに格納した値を渡せること。AWS::ECS::TaskDefinition ContainerDefinitionのEnvironmentとSecretsの違い。
Environment 通常の環境変数The environment variables to pass to a container. This parameter maps to Env in the Create a container section of the Docker Remote API and the --env option to docker run.
Secrets 環境変数と渡されることはおなじだがSecrets ManagerやSSMパラメータの値を渡せる The secrets to pass to the container. For more information, see Specifying Sensitive Data in the Amazon Elastic Container Service Developer Guide.