Why are my environment variables undefined when deploying to AWS?
P粉008829791
P粉008829791 2024-03-28 19:58:03
0
1
432

I am currently developing a vue application. I set the environment variables in a file called "config.ts" which contains code similar to:

export const configs = {
  baseURL:
    process.env.VUE_APP_API_BASEURL ||
    'http://test.api.co/',
}

I tested the environment variables locally using .env as shown below

VUE_APP_API_BASEURL=https://test2.api.com

And the effect is very good.

I then dockerized the application using a Dockerfile like this:

FROM private_image_container/node:v16 as build-stage

# declare args
ARG VUE_APP_API_BASEURL

# set env variables
ENV VUE_APP_API_BASEURL=$VUE_APP_API_BASEURL
RUN echo "VUE_APP_API_BASEURL=$VUE_APP_API_BASEURL" > .env
WORKDIR /app
COPY package.json ./
COPY yarn.lock ./
RUN yarn
COPY . .
RUN yarn build

# production stage
FROM private_image_container/nginx:latest as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

When deploying the application, you will not see the variables even if they are defined in the task definition.

P粉008829791
P粉008829791

reply all(1)
P粉916553895

So I ran into this problem because our DevOp people insisted that we have runtime environment variables.

The solution I developed was to write a bash script that injects a script to attach the configuration to the window object, making it accessible as a runtime.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template