Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables not available in AWS ECS instance. #9049

Closed
unclejustin opened this issue Mar 13, 2024 · 4 comments
Closed

Environment variables not available in AWS ECS instance. #9049

unclejustin opened this issue Mar 13, 2024 · 4 comments
Labels
bug:unverified needs-response We need a response from the original author about this issue/PR

Comments

@unclejustin
Copy link

Reproduction

You need AWS to reproduce. So best I can do is outline the steps.

  1. Create an ECS service.
  2. Set any environment variable
  3. ssh into server and confirm that env var is available
  4. log env var in a loader function (see below)
  5. Check server logs and notice that env var is not available

Process.env will only ever have the default environment variables provided by node.

I was able to get around this by using a custom express server and adding process.env to the context like this:

app.all(
  "*",
  createRequestHandler({
    build,
    getLoadContext() {
      // eslint-disable-next-line no-undef
      return { env: process.env };
    },
  }),
);

Now if I look at context inside of a loader function the context is populated but process.env is not.

export const loader = ({ context }: LoaderFunctionArgs) => {
  // context will have the env vars loaded
  console.log("🚀 ~ loader ~ context:", context);

  // process.env will not
  console.log("🚀 ~ loader ~ process.env", process.env);

  const GOOGLE_CALLBACK_URL = getEnvVar("GOOGLE_CALLBACK_URL");
  const DATABASE_URL = getEnvVar("DATABASE_URL");

  return json({ GOOGLE_CALLBACK_URL, DATABASE_URL });
};

System Info

System:
    OS: Linux 5.10 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (2) x64 AMD EPYC 7R13 Processor
    Memory: 6.88 GB / 7.57 GB
    Container: Yes
    Shell: 5.2.15 - /bin/bash
  Binaries:
    Node: 18.19.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.4 - /usr/local/bin/npm
  npmPackages:
    @remix-run/express: ^2.8.1 => 2.8.1 
    @remix-run/node: ^2.8.0 => 2.8.1 
    @remix-run/react: ^2.8.0 => 2.8.1 
    @remix-run/serve: ^2.8.0 => 2.8.1 
    @remix-run/server-runtime: ^2.8.0 => 2.8.1

Used Package Manager

npm

Expected Behavior

process.env.VARIABLE_NAME should be available.

Actual Behavior

process.env.VARIABLE_NAME is undefined.

@brophdawg11
Copy link
Contributor

How are you setting the environment variable? I'm not too familiar with AWS but I found https://docs.aws.amazon.com/AmazonECS/latest/developerguide/taskdef-envfiles.html and https://docs.aws.amazon.com/AmazonECS/latest/developerguide/use-environment-file.html on a quick search - are you doing either of those to set the value?

@brophdawg11 brophdawg11 added the needs-response We need a response from the original author about this issue/PR label Mar 18, 2024
@unclejustin
Copy link
Author

Thanks for the reply @brophdawg11 ! I am setting the env vars in the task definition like it shows in the first link.

The strangest thing to me is that the envars are available in ssh and in context. Almost like node is "waiting" for them.

I am not confident this is "Remix" problem either, but a node problem. Currently I am working on generating a .env file during the container build and referencing that, which seems to be working so far. I'll report back once I have a workaround.

@github-actions github-actions bot removed the needs-response We need a response from the original author about this issue/PR label Mar 18, 2024
@brophdawg11 brophdawg11 added the needs-response We need a response from the original author about this issue/PR label Mar 19, 2024
Copy link
Contributor

This issue has been automatically closed because we haven't received a response from the original author 🙈. This automation helps keep the issue tracker clean from issues that are unactionable. Please reach out if you have more information for us! 🙂

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 29, 2024
@unclejustin
Copy link
Author

Just in case anyone stumbles on this in the future, here's my workaround.

In my github action where I'm building the docker container I inline all of the build args like this:

- name: Build, tag, and push image to Amazon ECR
        id: build-image
        run: |
          # Build a docker container and
          # push it to ECR so that it can
          # be deployed to ECS.
          docker build --progress=plain -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \
            --build-arg GOOGLE_CALLBACK_URL=${{ env.GOOGLE_CALLBACK_URL }} \
            .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          echo "imageid=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT

The important bit is the --build-arg. Then in the Dockerfile

ARG GOOGLE_CALLBACK_URL
ENV GOOGLE_CALLBACK_URL=${GOOGLE_CALLBACK_URL}

Now GOOGLE_CALLBACK_URL will be available in the app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug:unverified needs-response We need a response from the original author about this issue/PR
Projects
None yet
Development

No branches or pull requests

2 participants