Currently I'm working on a database migration. We use cloudformation to handle our resources and we have some lambda functions which create direct connections to our current database. We use secrets manager to handle the database credentials (username, password, endpoint/host, port, etc...).
What we want to have done is that when I modify the, let's say, the endpoint/host on the secrets, the connection on all the lambda functions we have which make a direct connection to the database would be updated.
I have read this question and its answers and I have tried to force a cold-start using a script which executes the aws lambda update-function-configuration
command for the lambdas that I need to refresh their runtime.
The issue with this approach is that it seems to not be enough to completely refresh the lambda runtime because the database connection is still behaving as before making changes on the values stored on the secrets.
We cannot afford the time to make a full deployment of the stacks responsible for the lambdas that we need to "restart".
I'm not sure if the UpdateFunctionCode API endpoint will be useful to me since some of our lamdbas use are image based and others are ZipFile based using a runtime.
Reading this helped me solve my issue. Since updating an environment variable forces the lambda runtime to be restarted, I can just retrieve the existing environment variables and add a new one to restart a given lambda's runtime.
I made a little script to cold-start the lambda functions listed on an array:
NOTE: I'm not the best at bash scripting; so, this could be optimized. Also, I did not have time to learn to handle json objects on bash; that's why I used a second script (python) for that.
The python script is pretty simple:
I know that this is kinda hacky, but I wanted to share since it may be useful for someone.