Aws-sam-cli: Issue connecting to postgres on localhost

Created on 2 Mar 2018  Â·  8Comments  Â·  Source: aws/aws-sam-cli

I'm trying to connect my lambda to a local instance of postgres. However, I'm getting the following error. I found this suggestion but that didn't work either.
Any suggestions? Thanks in advance.
{ Error: connect ECONNREFUSED 127.0.0.1:5432 at Object.exports._errnoException (util.js:1018:11) at exports._exceptionWithHostPort (util.js:1041:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14) code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 5432 }

Most helpful comment

I resolved this issue by replacing localhost with host.docker.internal.

Reference: https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

All 8 comments

Have you tried using your IP address instead of localhost (e.g 127.0.0.1)?
If the logs are right this error is expected as the service port your
Lambda is trying to connect to isn’t available.

If you’re running this DB in a container by any chance then simply create a
Docker Network, run the DB and SAM Local in the same network
On Fri, 2 Mar 2018 at 20:01, Walker Langley notifications@github.com
wrote:

I'm trying to connect my lambda to a local instance of postgres. However,
I'm getting the following error. I found this suggestion
https://stackoverflow.com/questions/33357567/econnrefused-for-postgres-on-nodejs-with-dockers
but that didn't work either.
Any suggestions? Thanks in advance.

{ Error: connect ECONNREFUSED 127.0.0.1:5432
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432 }```

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/awslabs/aws-sam-local/issues/318, or mute the thread
https://github.com/notifications/unsubscribe-auth/ADL4BH5zIBgmt0edvhQ6dyFp36dq2xtWks5taaUjgaJpZM4SabEd
.

Thanks @heitorlessa. I tried using my local IP and that didn't work either. I think the issue is that the lambda is running in a container, so it doesn't know what "localhost" or "127.0.0.1" is. I've tried looking around to see if there is a way to get a docker container to point to my local machine and it seems using --net="host" is an option (suggestion here) when launching the docker container. However, since the docker container is spun up using the sam local start-api command and I've never used Docker, I'm pretty lost. I was trying to hijack the run command here but this seems like it's starting to be more of a hassle than it's worth. Unless there is an easy fix I think I'll just put a test db up in aws and point to that.

You’re right on that Lambda won’t be able to access localhost — It’s the
way Docker Networking works.

I’m on my way to bed now but here’s what I did a while ago using DynamoDB
Local + SAM local in the same Docker Network:

https://github.com/heitorlessa/sam-local-python-hot-reloading

Take a look at the create network step _and_ the sam Local start-api with
the Network flag.

Then take a look at the endpoint_url option as I use the name of the
DynamoDB Local Docker Container instead of an IP:

https://github.com/heitorlessa/sam-local-python-hot-reloading/blob/master/users/users.py

If you still have issues ping me over on twitter (@heitor_lessa) tomorrow
and I’m happy to help
On Fri, 2 Mar 2018 at 21:28, Walker Langley notifications@github.com
wrote:

Thanks @heitorlessa https://github.com/heitorlessa. I tried using my
local IP and that didn't work either. I think the issue is that the lambda
is running in a container, so it doesn't know what "localhost" or
"127.0.0.1" is. I've tried looking around to see if there is a way to get a
docker container to point to my local machine and it seems using
--net="host" is an option here
https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach
when launching the docker container. However, since the docker container is
spun up using the sam local start-api command and I've never used Docker,
I'm pretty lost. I was trying to hijack the run command here
https://github.com/cnadiminti/docker-aws-sam-local/blob/master/Makefile
but this seems like it's starting to be more of a hassle than it's worth.
Unless there is an easy fix I think I'll just put a test db up in aws and
point to that.

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/awslabs/aws-sam-local/issues/318#issuecomment-370057703,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADL4BIgaMbI-JVhg0U8XSCgtr0vvdKyIks5tablsgaJpZM4SabEd
.

@heitorlessa: Thanks for the info. I'll take a look at your example and try to familiarize myself with docker a little more before I message you. Thanks so much for your help!

I resolved this issue by replacing localhost with host.docker.internal.

Reference: https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

@pawsong thanks a ton!!!

@pawsong This doesn't work on linux. Is there any other ways?

@pawsong try this:

sam local start-api --docker-network=host

Works for me after few reloads...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rhlsthrm picture rhlsthrm  Â·  4Comments

debuggins picture debuggins  Â·  4Comments

terlar picture terlar  Â·  3Comments

drumadrian picture drumadrian  Â·  3Comments

asyba picture asyba  Â·  3Comments