Issue Connecting AWS SAM Lambda Handlers to LocalStack Services

Hi,

I’d like to use my AWS SAM setup with my Localstack services but can’t manage to properly. I set up localstack and all is working on localhost:4566 with my docker compose file. However, when I also start up AWS SAM with my lambda handlers build to access localstack services I get this error:

‘errorMessage’: ‘Could not connect to the endpoint
URL: “http://localhost:4566/”’

I’m encountering an issue integrating AWS SAM with LocalStack for local development. Despite setting up LocalStack successfully (accessible at ‘localhost:4566’ via Docker Compose), I’m unable to connect my Lambda handlers, initiated by AWS SAM, to LocalStack’s services. Instead, I receive the following error:

‘errorMessage’: ‘Could not connect to the endpoint URL: “http://localhost:4566/”’

^ This occurs when I try to make a call to boto3 cognito at localstack’s endpoint

I want to ensure that my Lambda functions, while being tested locally through AWS SAM, interact exclusively with LocalStack instead of reaching out to actual AWS cloud services.

Does anyone know what might be causing this connectivity issue? Any guidance or suggestions would be greatly appreciated.

Hi @krhobs , thanks for raising this issue.

The reason localhost:4566 works from your host is that your docker configuration is publishing port 4566 from the container to the host.

Trying to access localhost:4566 from a separate docker container is not going to work because port 4566 is not forwarded from your LocalStack container to your lambda container.

In general, we have some networking troubleshooting guides that should help you out here. Your specific use case is also covered in our Transparent Endpoint Injection documentation, specifically this section. Depending on your chosen language, you may not need any configuration at all, as we inject AWS_ENDPOINT_URL into the lambda environment, and supported language SDKs use this environment variable to configure their endpoint, or you may need to set the endpoint URL manually (to $AWS_ENDPOINT_URL).

Hi so I tried setting my endpoint_url to

os.getenv(“AWS_ENDPOINT_URL”)

But this returns None and then connects to the real AWS service, also using

os.environ[“AWS_ENDPOINT_URL”]

does not work either, it returns a key error. It seems like I don’t have this as an environment variable. What do I do? (For more context I use python 3.10.12)