2

I am new to Python and looking for some kind of help. Python Lambda function below is throwing me expected error.

Unable to import module 'lambda_function': No module named 'stomp'

Python Lambda Script:-

import time
import boto3
import stomp

kinesis_client = boto3.client('kinesis')


class Listener(stomp.ConnectionListener):
    def on_error(self, headers, message):
        print('received an error "%s"' % message)

    def on_message(self, headers, message):
        print('received a message "%s"' % message)
        kinesis_client.put_record(
            StreamName='inter-lambda',
            Data=u'{}\r\n'.format(message).encode('utf-8'),
            PartitionKey='0'
        )

I use mac to execute command and below is the error:-

sudo pip install stompy -t /Users/kalyanbhave/Documents/stomp/
Password:
The directory '/Users/kalyanbhave/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/kalyanbhave/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting stompy
  Downloading https://files.pythonhosted.org/packages/af/21/8f3bdf1f304ba65847706c29d1950525513ec11d941ed0630ee8af51b361/stompy-0.2.9.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/tmp/pip-install-rgmh5da6/stompy/setup.py", line 2, in <module>
        from stompy import distmeta
      File "/private/tmp/pip-install-rgmh5da6/stompy/stompy/__init__.py", line 1, in <module>
        from stompy.stomp import Stomp, NotConnectedError
      File "/private/tmp/pip-install-rgmh5da6/stompy/stompy/stomp.py", line 49
        except socket.timeout, exc:
                             ^
    SyntaxError: invalid syntax

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/tmp/pip-install-rgmh5da6/stompy/

1 Answers1

4

You are using AWS Lambda. The module stomp is not included by default.

You'll need to create your own deployment package including stomp as described here.

Rob Bricheno
  • 4,467
  • 15
  • 29
  • Thank you. I ran into another error after following your link. Please suggest –  Nov 19 '18 at 15:02
  • What happens if you try using `sudo -H` as suggested in the error output? Also are you trying to make a Python 3 package or a Python 2 package? I think MacOS uses Python2 as default so you may need to get Python 3 via Homebrew (or similar) and start there instead of using the system python. – Rob Bricheno Nov 19 '18 at 15:08
  • I don't want to create own deployment package because If I do so, I need to push them as a zip file. And this will be a problem if I create infrastructure via terraform. Do we have alternate solution –  Nov 19 '18 at 17:30
  • @Tinku I think this is the best solution for you, the next alternative would be using a service like Elastic Beanstalk or EC2 if you want to go the AWS route, neither of which will be easier than this. – Rob Bricheno Nov 19 '18 at 17:53
  • @Rob-- Could you please provide me steps to do it. As the link provided to you is lil challenging for me to do the same in mac machine –  Nov 19 '18 at 17:57
  • @Tinku I will write it up one day, when I have more time. Until then you could try the following: 1. install homebrew https://www.howtogeek.com/211541/homebrew-for-os-x-easily-installs-desktop-apps-and-terminal-utilities/ 2. install python3 and virtualenv using homebrew: `brew install python3 pyenv-virtualenv` 3. follow the instructions here to produce a zip using virtualenv https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html#with-s3-example-deployment-pkg-python – Rob Bricheno Nov 19 '18 at 20:08
  • @Tinku or you could not use stomp, which I don't think will work with lambda anyway, and use for example AWS own queue service SQS https://aws.amazon.com/sqs/ which would integrate better with lambda. – Rob Bricheno Nov 19 '18 at 20:11
  • By any chance can you provide me some time on your calendar. So that I can debug the same –  Nov 20 '18 at 14:06
  • Hi @Rob Bricheno -- I have created a zip file with has stompy and received below error. https://stackoverflow.com/questions/53395336/aws-syntax-error-in-module-invalid-syntax-stomp-py-line-49 .. Please suggest –  Nov 20 '18 at 14:41