I am current building/testing a node/NestJS based API, that I am deploying to AWS Lambda.
I chose AWS SAM for the deployment tooling, as the integration is simple, lightweight and declarative.
Running the service locally, npm run start:dev, works perfectly and connects to the DB, mongo, hosted in Docker without a problem
Before deploying, I webpack the project to 1 file and run it in a lambda equivalent environment, which also runs on Docker, using "sam local start-api" and here is where the problem appears...
My service is unable to connect to mongo, when both are running in Docker.
After the usual questions... is MongoDB running, can I connect to it, etc, I knew there was an issue.
Initially all I got was:
Function 'xxxxxx' timed out after 3 seconds
Really helpful.
After some messing around, I eventually reset the SAM timeout, retried and got:
MongoServerSelectionError: connect EHOSTUNREACH 127.0.0.1:27017
Now we are getting somewhere... it is unable to reach mongo.
To cut a long story short, after trying many different things, I eventually found the correct IP to call Mongo running in Docker. It's right down the bottom of the config if you are interested.
More importantly, I then looked up how to extract it directly...
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mongodb
Call that, stick the IP address in your environment variables, and tada.. bob's your aunties brother.
Should also work for any other db / service that you need to connect to, when running it all in Docker.
No comments:
Post a Comment