My suggestion would be to get rid of dotenv and use real environment variables.
The problem with dotenv is that it simply mocks process.env inside your node process. It does not really modify environment variables for you. They are not available outside of your node process.
I would use direnv instead and replace your .env files with .envrc for local development.
For CI and production environments, the environment variables are now defined in your CodeBuild project so .envrc files are not needed.
Please see this related answer for more information from another angle.
If you wish to continue using dotenv, you have several options:
- Store the
.env file in S3 and retrieve it during the build process.
- Construct the
.env file using shell scripts and environment variables defined on the CodeBuild project.
I'm sure there are others but they are all hacky for me.