Package the application

You will need to build a Docker image that runs your script in order to deploy the application to a cluster in AWS. The Dockerfile is very simple.

The image starts with the base Node.js 12 Alpine image, installs dependencies, sets the AWS_REGION environment variable, adds the rest of the source code and defines an entrypoint for containers.

Another important file is .dockerignore. When Docker builds an image, it takes the working directory and sends everything in it to the Docker daemon. But there are some files that are better to ignore, such as dependencies that have been installed locally. This saves time during builds and shrinks the size of the Docker image.

Docker will ignore the node_modules and the code tarball that you downloaded.

Build the image and run it locally

  1. You can build the Docker image with the following command:

    docker build -t twitch-sentiment .
  2. When it’s done building, run the container with a channel argument as input. You need to mount the .aws folder so that the main script can authenticate from within the container:

    docker run -v /${HOME}/.aws/://root/.aws twitch-sentiment -c esl_csgo

    After a couple minutes, new values will show up in your CloudWatch Metrics graph. The container will automatically stop running after five minutes. If you want to stop it before then you have to use the Docker client to stop the container.

  3. In a new terminal, run the following:

    docker ps

    This will print information about the running container, something like:

    CONTAINER ID        IMAGE               COMMAND                    ...
    3eaef242facb        twitch-sentiment    "node index.js -c st..."   ...
  4. Copy the Container ID and pass it in as an argument to the docker stop command. For example:

    docker stop 3eaef242facb

    The container should now be stopped.