Skip to main content

Fargate deploy

Fargate is the service type of ECS service For running fargate you need to create next resources (create with terraform):

  • VPC - network, in which service will run
  • SQS - Queue from which fargate task will pull messages
  • IAM - Roles assigned to ECS service and ECS task, here we describe permissions to other services to allow communications with them inside VPC
  • Cloud Watch - Log group to collect logs from fargate tasks
  • ECS - describe ECS service and it's task definition
  • Cloud Watch Alert - alert, escalated on new messages to SQS queue
  • Auto Scaling - Describe rules for tasks autoscaling

Flow

  • new message arrives to the queue
  • cloud watch alert checks it every 2 minutes and if there is a new message it goes to alert state and notifies all services about it
  • autoscaling service gets a notification about new message and increases Desired Task count in ECS Service by 1
  • As soon as Desired Count increased, ECS Service creates new Task
  • When Task starts working it pull message from queue and aquire block for itself to prevent forced shutdown*.
  • After finish its work Task release block and decreate Desired Count by 1
  • ECS Service kill unblocked Task

Forced shutdown: Once we decrease Desired Count parametr in ECS Service, it will chose any running Task and kill it, to prevent Task that are busy we need to aquire block.

So the command to run to deploy your function is: nx run ${action-name}:deploy --stage={stage}

Important - Tech Debt - REWRITE IT env files for container needs to be specified in terraform/modules/ecs/main under task defenition