Search code examples
amazon-web-servicesamazon-ecsaws-fargate

can you scale down desiredcount to 0 via same container in fargate


I have a fargate service where a task running a container. So, my container is a custom made pipeline, which runs based on SQS count. So, if all the records are processed then I stop the pipeline from the container. So, what I'm wondering, instead of stopping the pipeline, can I set the desired count of the service to 0?(this service is the parent of this container's task)


Solution

  • Yes, you can set desire count to zero once the process complete, but there are few things that you need to consider.

    • Your node process should exit once it complete operation
    • Dockerfile should have a valid entrypoint, aws-cli
    • Fargate service should have to ecs service update opertation

    For example Dockerfile

    FROM node:alpine
    RUN apk add --no-cache python py-pip
    
    RUN pip install awscli
    RUN aws --version
    WORKDIR /app
    COPY app.js .
    COPY entrypoint.sh /entrypoint.sh
    RUN chmod +x /entrypoint.sh
    ENTRYPOINT /entrypoint.sh
    

    entrypoint that will deal with nodejs process and set service count to 0 when the nodejs process completed from SQS.

    #!/bin/sh
    # start the node process, but it should exit once it processes all SQS
    node app.js
    # once process is done, we are good to scale down the service
    aws ecs update-service --cluster my-cluster --region us-west-2 --service my-http-service --desired-count 0
    
    

    An example node process that will exit after 1 minute, app.js

    function intervalFunc() {
      console.log('Container will stop  in 1 minutes');
    }
    
    setInterval(intervalFunc, 1500);
    setTimeout((function() {
        return process.exit(0);
    }), 60000);
    
    

    So Docker image play an important role and the node process should exit otherwise it will keep running.