I can't find a solution on how to deploy stand-alone task. It is a database migration script that I have to execute during the deployment.
I created an image and task definition and from UI I can do it:
or it could be done from cmd:
aws ecs run-task --launch-type FARGATE --cluster MyECSCluster --task-definition app-migrations:1 --network-configuration "awsvpcConfiguration={subnets=[subnet-xxxx,subnet-yyyy],securityGroups=[sg-xxxxxxxxxxx]}")
.
But I want to use CDK for this (my whole stack is in CDK).
In CDK I found EcsRunTask in the step function package but I don't know how to use it. But, as fary I understund it is dedicated to handle flow with the lambdas and I'm not sure if it is the correct approach for me.
Maybe, someone, has code sniped with the example. I use typescript but it could be in any language.
If not snippet maybe some suggestions on how to deal with this.
CodePudding user response:
The CDK (and CloudFormation) don't support running a single ECS task on deployments like that. There is an answer here that appears to use an EventBridge event to trigger the ECS task.
Alternatively, you could run it as a non-essential container in your main ECS task, so that it starts up and runs every time your ECS task starts, and being marked as non-essential the container can exit without ECS trying to redeploy the task. However you might need to look into some sort of distributed locking mechanism if you are running multiple instances of your task and your DB migration tools don't handle locking automatically.
I've had to solve this exact problem on multiple projects, and I've stopped trying to run the DB migration in ECS all together. I'm using AWS CodePipeline now to deploy application updates, and spawning a CodeBuild task inside the VPC which runs the DB migration as part of the deployment.