aws-actions/amazon-ecs-render-task-definition

Error: Invalid task definition format: containerDefinitions section is not present or is not an array

Closed this issue · 2 comments

Hey there,

I've been trying to get this GitHub action to work but have been running into issues. I've been programmatically been trying to retrieve the task definition using this command:
aws ecs describe-task-definition --task-definition ${{ env.ECS_TASK_DEFINITION }} > ecs/node/task_definition.json

Then using it for this action. But for some reason, it just isn't liking the containerDefinitions, which are returned in the JSON. I've verified the command does return correctly.

Here is our GitHub actions yaml:

steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: eu-west-1
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1
      - name: Build and push
        uses: docker/build-push-action@v3
        with:
          push: true
          tags: ${{ steps.login-ecr.outputs.registry }}/${{ env.REPO_NAME }}-${{ env.ENVIRONMENT }}:${{ github.sha }}
          build-args: |
            COMMIT=${{ github.sha }}
            BRANCH=${{ github.ref_name }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
      - name: Send image version to SSM
        run: aws ssm put-parameter --name ${{ env.REPO_NAME }}-version-${{ env.ENVIRONMENT }} --value ${{ github.sha }} --overwrite
      - run: mkdir -p ecs/node
      - run: aws ecs describe-task-definition --task-definition ${{ env.ECS_TASK_DEFINITION }} > ecs/node/task_definition.json
      - uses: actions/upload-artifact@v3
        with:
          name: task-definition
          path: ecs/node/task_definition.json
      - uses: actions/download-artifact@v3
        with:
          name: task-definition
      - name: Display structure of downloaded files
        run: ls -R && cat ecs/node/task_definition.json # The file is present here <--------------
      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@97587c9d45a4930bf0e3da8dd2feb2a463cf4a3a
        with:
          task-definition: ecs/node/task_definition.json # Issue arises here <--------------
          container-name: ${{ env.CONTAINER_NAME }}
          image: ${{ steps.login-ecr.outputs.registry }}/${{ env.REPO_NAME }}-${{ env.ENVIRONMENT }}:${{ github.sha }}
      - name: Deploy Amazon ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@de0132cf8cdedb79975c6d42b77eb7ea193cf28e
        with:
          task-definition: ${{ steps.task-def.outputs.task-definition }}
          service: ${{ env.ECS_SERVICE }}
          cluster: ${{ env.ECS_CLUSTER }}
          wait-for-service-stability: true

I was hoping someone could direct me on what may be wrong here, or if I'm doing this incorrectly. I would ideally like to stick to dynamically generating the task definition using the AWS CLI, as it is our separate Terraform repo that spins up the ECS infrastructure, hence why I opted for AWS CLI to grab the task definition in order to use this action.

Hi @sgript

take a look at your describe-task-definition run command step again. you need to make sure its a valid task definition as the raw aws-cli will output the task def with the key taskDefinition

so you can either do:

- run: aws ecs describe-task-definition --task-definition ${{ env.ECS_TASK_DEFINITION }} --query taskDefinition > ecs/node/task_definition.json

or

- run: aws ecs describe-task-definition --task-definition ${{ env.ECS_TASK_DEFINITION }} | jq '.taskDefinition' > ecs/node/task_definition.json

hope that helps.
-andy

Hi, closing this issue as it seems to have been answered. Please feel free to re-open or open a new issue if there are additional concerns. Thanks!