gzimbron / amplify-adapter

MIT License
22 stars 2 forks source link

No env variable on amplify #15

Open Pierre-Demessence opened 5 days ago

Pierre-Demessence commented 5 days ago

Hello, I'm using dynamic public variables:

import { env } from '$env/dynamic/public';
const { SVELTE_APP_API_ENDPOINT } = env;

It's working with an .env or .env.local file when building with npm run dev and npm run preview. But once it's deployed on Amplify, it's not working, the variables are empty. I tried to set the following variables on Amplify panel:

I also tried what's written on Amplify doc: https://docs.aws.amazon.com/amplify/latest/userguide/ssr-environment-variables.html Tried putting the .env.production inside the build folder.

Nothing has worked for now. I've checked with an .env command in the build steps in amplify.yml and the variables exist.

Versions:

gzimbron commented 4 days ago

Hello @Pierre-Demessence please checkout this example repository:

https://github.com/gzimbron/example-env-variables-amplify-adapter

You need to add Environment variables in amplify app control panel

Pierre-Demessence commented 3 days ago

You need to add Environment variables in amplify app control panel

Yes, they have been added.

I've checked your repo and I do exactly the same, aside from using dynamic env instead of static env. So I tried, and it appears it works for static env.

So this doesn't work:

import { env } from '$env/dynamic/public';
console.log('DYANMIC SVELTE_APP_API_ENDPOINT', env.SVELTE_APP_API_ENDPOINT); // undefined

But this works:

import * as env_static from '$env/static/public';
console.log('STATIC SVELTE_APP_API_ENDPOINT', env_static.SVELTE_APP_API_ENDPOINT); // correct value

As you can see I have changed the prefix to SVELTE_APP_ (for retro-compatibility), but I doubt that's the issue. I have a solution right now by switching to static env, but since I might need dynamic variables, I don't think the issue is solved yet.

gzimbron commented 3 days ago

It seems that, for now, you can only use static variables, since AWS Amplify uses a Docker image (Amazon Linux 2023). Unless you use a custom image, it doesn’t seem possible to add environment variables to the image.