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

Inserts a container image URI into an Amazon ECS task definition JSON file.
MIT License
280 stars 142 forks source link

I think `v1.3.0` not working with command starts with `NODE_ENV=` #296

Closed ensia96 closed 6 months ago

ensia96 commented 6 months ago

Github actions runs well, but on ECS, container exits with error

Error: Cannot find module '/usr/src/app/NODE_ENV=dev'

The code below is the code of my project.

      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: ${{ env.ECS_TASK_DEFINITION }}
          container-name: ${{ env.CONTAINER_NAME }}
          image: ${{ steps.build-image.outputs.image }}
          environment-variables: |
            # envs
          command: NODE_ENV=${{ env.ENV }} node dist/app.local.js

So, I fixed code above to below, and now the container works well.

      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        # before 'support command override' update
        # https://github.com/aws-actions/amazon-ecs-render-task-definition/blob/v1.3.0/CHANGELOG.md
        uses: aws-actions/amazon-ecs-render-task-definition@v1.2.0
        with:
          task-definition: ${{ env.ECS_TASK_DEFINITION }}
          container-name: ${{ env.CONTAINER_NAME }}
          image: ${{ steps.build-image.outputs.image }}
          environment-variables: |
            # envs
          command: NODE_ENV=${{ env.ENV }} node dist/app.local.js

I saw your update changelog.

And found this

image

amazreech commented 6 months ago

Hi @ensia96,

Here is what I was able to find, hope this helps.

Command field was not supported in earlier versions of ecs-render-task-definition. This field recently got supported as part of the latest release v1.3.0 and PR 284

Passing in the Command in earlier versions of ecs-render-task-def, I believe would do nothing. This field, I think would not be passed on to ECS Task definition at all.

However, starting v1.3.0 the Command field is being read and passed on to ECS task definition.

Why the command field you passed not be working:

Here is ECS documentation explaining how Command is used in ECS: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html.

As per the documentation the command field maps to COMMAND parameter on docker run. Below is the relevant paragraph from the documentation:

The command that's passed to the container. This parameter maps to Cmd in the Create a container section of the Docker Remote API and the COMMAND parameter to docker run. For more information about the Docker CMD parameter, see https://docs.docker.com/engine/reference/builder/#cmd. If there are multiple arguments, make sure that each argument is a separated string in the array.

I tested by trying to pass in a command like you mentioned when running a docker image: docker run -it <Image-Id> NODE_ENV=dev node index.js

Looks like we get a similar error here from docker when trying to run above command:

internal/modules/cjs/loader.js:638
    throw err;
    ^
Error: Cannot find module '/home/node/app/NODE_ENV=dev'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

What might work:

To get around this issue, I was able to set the Environment variable in my docker file with something like: ENV NODE_ENV=dev

And then pass node dist/app.local.js as command to the github workflow.

...
environment-variables: |
  # env vars
command: node dist/app.local.js
...

Let me know if this helps.

ensia96 commented 6 months ago

I will reflect your recommendations into my code in next deployment, and then share the result.

      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: ${{ env.ECS_TASK_DEFINITION }}
          container-name: ${{ env.CONTAINER_NAME }}
          image: ${{ steps.build-image.outputs.image }}
          environment-variables: |
          environment-variables: |
            NODE_ENV=${{ env.ENV }}
            # envs
          command: node dist/app.local.js

Thank you!

ensia96 commented 6 months ago

It works!

I will close this issue. Thank you for your fast reply!