Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MatTheProgrammer's avatar

Changing environment Values in Runtime

Hey,

Short background: we are small tech company hosting an api with a few million requests a week and rebuilding our tech stack in laravel. Currently we are holding some of our environment values in AWS Services like Secrets Manager and Parameter Store and after deploying to ECS we'll run config:cache to store all values in the config. We need to cache the keys/passwords locally since caling the AWS Services everytime we need a value would add a round trip to our request and also would cost for every call. We wrote a helper class to replace the env() method calls in the config files (config/app.php, config/database.php and so on), which first tries to read from .env then the AWS Services till it finally finds the place where the value is stored. This works totally fine.

But we use password/key rotations so this process needs to be run periodically by a cronjob during runtime to ensure we always have the newest keys cached in the config. Since we are holding information like database connections etc in the config, we can't just run config:cache since this would delete the bootstrap/cache/config.php file to recreate it which makes the database unavailable for the time till the new cache file is created. Therefor we override the config:cache command and do the same things expect deleting the old file(no config:clear) but creating a new file and moving it to right place to replace the existing one. this also works like a charm. The Problem is, that the config files aren't read any more as long as the bootstrap/cache/config.php file exists.

So here is the question, can we somehow manage to force laravel to read from the config files without deleting the old file? Maybe there is a trick to make it run? Or should we do a total different approach to hold rotating keys/passwords. I couldn't find any result while researching since it looks like everyone just puts their keys in the .env and caches them one time and is happy with it.

0 likes
1 reply
jomisacu's avatar

You could flush all environment vars to /etc/profile.d/custom.sh or /etc/environment at the deployment/key-rotation process. With this approach you can hold the Laravel code without modifications. So, you can overwrite environment variable files and then rebuild the cache.

1 like

Please or to participate in this conversation.