1
votes

I am trying to test aws lambda function locally and was successful in mocking aws resource like dynamo DB using moto library but when in introduced a component i.e request for invoking third party libaries i got error as connection refused.

requests.exceptions.ConnectionError: Connection refused: GET https://www.google.com/

It is mainly because of moto libraries reponse.py class i.e httpPretty library.

What are the other alternatives we can use to fix these or test lambda function locally having aws resources as well as call to third party libraries.

My lambda function source :

  import boto3
  import requests    
  def lambda_handler(event,context):
        client=boto3.client('dynamodb')
        response = client.put_item(TableName='divyanayan_test',
                                  Item={'id': {'S': 'hello1'}})
        r = requests.get('https://www.google.com/')
        print(r)

Also did tried with localstack library and deployed the image to docker but it give a url not Could not connect to the endpoint URL: "http://localhost:4569/".

Reference : https://github.com/localstack/localstack

1

1 Answers

0
votes

The solution to the moto library third part libraries i figured out was mocking the third party response as well as in the expected response and hence did got got any connection refused error .

And for the localstack lib for local lambda testing ,it was mistake from my side as i had to use docker machine ip instead of localhost and the url was reachable then.

In case of any doubts please let me know.