Step Functions AWS SAM CLI Local Connection Refused Error
Asked Answered
S

3

6

Following the steps in the AWS documentation

https://docs.aws.amazon.com/step-functions/latest/dg/sfn-local-lambda.html

using aws-stepfuncitons-local docker container

I'm getting a connection refused error at the last step

2019-05-28 12:37:05.004: arn:aws:states:us-east-1:123456789012:execution:HelloWorld:test : 
{
    "Type":"ExecutionFailed",
    "PreviousEventId":5,
    "ExecutionFailedEventDetails":
    {
        "Error":"Lambda.SdkClientException",
        "Cause":"Unable to execute HTTP request: Connect to 127.0.0.1:3001 [/127.0.0.1] failed: Connection refused (Connection refused)"
    }
}

Any help on how to resolve it would be greatly appreciated.

Sarraceniaceous answered 28/5, 2019 at 12:49 Comment(0)
B
9

The docker container can't talk to services on the host network. To get it to work you need to add '--network host'.

Example:

docker run -p 8083:8083 --network host --env-file aws-stepfunctions-local-credentials.txt amazon/aws-stepfunctions-local

More details here and here

Blondellblondelle answered 18/7, 2019 at 7:10 Comment(2)
After countless hours of searching and trial and error, you just save my day. Thank you.Advisement
At least on Mac, this throws a warning: WARNING: Published ports are discarded when using host network mode. I'm not sure how it decides which port it to listen to, but 8083 is incommunicable. Changing LAMBDA_ENDPOINT to host.docker.internal:3001 worked for me.Metaphrase
M
3

Just update LAMBDA_ENDPOINT to http://host.docker.internal:3001

Macroscopic answered 2/10, 2020 at 12:8 Comment(1)
And then use '--endpoint localhost:8083' for all the further steps listed in step 5 and step 6 in docs.aws.amazon.com/step-functions/latest/dg/…Geomorphic
T
1

It took me a few days, so that's what works for me:

  1. on my Mac: LAMBDA_ENDPOINT=http://host.docker.internal:5000/ (5000 for moto_server, use 3001 otherwise) works.

And use docker run -p 8083:8083 --env-file aws-stepfunctions-local-credentials.txt amazon/aws-stepfunctions-local

  1. on ubuntu-latest (github action), however, this combination works:

LAMBDA_ENDPOINT=http://127.0.0.1:5000/

docker run -p 8083:8083 --env-file aws-stepfunctions-local-credentials.txt amazon/aws-stepfunctions-local --network host

=> note the additional --network host

  1. To handle both cases inside the same codebase (in python):
            _, output = subprocess.getstatusoutput('uname -s')

            if output == 'Darwin': # on a Mac
                sam_step_function_local_cmd = subprocess.Popen(
                    [
                     'docker', 'run',
                     '-p=8083:8083',
                     '--env-file=aws-stepfunctions-local-credentials-mac.txt',
                     'amazon/aws-stepfunctions-local:1.7.3'
                     ],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT
                )
            else: # on linux
                sam_step_function_local_cmd = subprocess.Popen(
                    [
                     'docker', 'run',
                     '-p=8083:8083',
                     '--env-file=aws-stepfunctions-local-credentials-linux.txt',
                     '--network=host',
                     'amazon/aws-stepfunctions-local:1.7.3'
                    ],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT
                )

            try:
                mock_ssm(
                    func
                )(*args, **kwargs)
            finally:
                sam_step_function_local_cmd.terminate()

aws-stepfunctions-local-credentials-linux.txt:

AWS_DEFAULT_REGION=eu-west-1
LAMBDA_ENDPOINT=http://127.0.0.1:5000/

aws-stepfunctions-local-credentials-mac.txt:

AWS_DEFAULT_REGION=eu-west-1
LAMBDA_ENDPOINT=http://host.docker.internal:5000/
Tades answered 15/3, 2021 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.