amazon-web-servicesamazon-ecscirclecicircleci-2.0circleci-workflows

How to run an AWS ECS task inside a service of AWS ECS cluster and not ouside the service from Circle CI's "aws-ecs/run-task"


I am using Circle CI to build and push the image to AWS ECR, then use this image to deploy a container(with FARGATE as instance) in a service inside a cluster in AWS ECS. The problem is, the tasks are being run outside of this service, but in the same cluster.

Here's task sitting along the task that was started automatically by AWS: enter image description here The one that has group called service:adp-ecs-service is the one running inside service and the one with group adp-ecs-service is the one that is running outside of service. The one that has group called service:adp-ecs-service will be restarted automatically with the image in ECR tagged 'latest_ci', if i stop it, but other one won't start. And this service can only have one service at a time.

By looking at group in this image, I tried to specify the name of the service in group tag in 'config.yml' file in multiple ways, but to no avail. Here's all I have tried(you can see these in my commits here):

  1. 'service:adp-ecs-service'
  2. service:adp-ecs-service Both of them(with and without quotes) genereted following error:
An error occurred (InvalidParameterException) when calling the RunTask operation: Invalid namespace for group.
  1. adp-ecs-service
  2. service

Both of them ran the task outside the service(you can see the output of 3 in the image shown above. I have even tried environment variables for 1 and 2 too.


Here is the code for "aws-ecs/run-task" in "/.circleci/config.yml" (complete file can be found here):

      - aws-ecs/run-task:
          requires:
            - aws-ecr/build-and-push-image
          task-definition: 'adp-ecs-family'
          group: adp-ecs-service
          cluster: 'adp-cluster'
          aws-access-key-id: AWS_ACCESS_KEY_ID
          aws-secret-access-key: AWS_SECRET_ACCESS_KEY
          aws-region: 'ap-south-1'
          awsvpc: true
          launch-type: FARGATE
          subnet-ids: $AWS_SUBNETS
          security-group-ids: $AWS_ADP_SG
          started-by: 'circle-ci'
          assign-public-ip: ENABLED

And here is the service: enter image description here As you can see in the above picture, running task count and the desired task count is 1, but there are 2 tasks running in this cluster, one outside of the service.

What I want here is, if I run a new task inside the service, it should start a container by pulling the latest image from ECR by stopping the previous one. So, How do I properly specify the name of the service and accomplish this?


Solution

  • Simply put aws-ecs/run-task is not what you want. You need to deploy the new task definition to the service, not run it.

    You are looking for aws-ecs/deploy-service-update and update-task-definition if you haven't already.