Bug: Cant Connect to a database behind a SSL tunnel in my machine using `sam local start-api`
Description
I'm trying to connect to a external database that is outside an SSL tunnel with a .pem file. The tunnel is in my local machine in the port (for example 27001) . The docker container cant acces to the port of my machine. I tried to set the container in the host network using host.docker.internal but it doesnt work... I'm using Manjaro Linux.
Steps to reproduce
I tried to:
- Create a custom dockerfile with the port binding
- Bind the host network to the sam local start-api command and use host.docker.internal
Im Using:
Os: Manjaro linux sam version: 1.127.0
None of these works
Hi. I found some other similar reports to this in the past, and it could be that the --docker-network parameter (that should allow this behavior) doesn't work correctly.
#7732 #3609
What does work as a workaround, is to add a host to your container, making localhost inside of it to refer to your actual localhost where your tunnel is running.
sam local start-api --add-host localhost:host-gateway
With this, you can call localhost:27001 from inside your Lambda function and it will call the host-gateway, which refers to your laptop or environment where you're running the sam command. You can also change localhost in that command and choose whatever name you want, and then inside your function's code, you can make a call to a port using that name.
Cool! I will try it. Thank you!