Search code examples
amazon-web-servicesrabbitmqpython-pika

Connect to RabbitMQ instance remotely (created on AWS)


I'm having trouble connecting to a RabbitMQ instance (it's my first time doing so). I've spun one up on AWS, and been given access to an admin panel which I'm able to access.

I'm trying to connect to the RabbitMQ server in python/pika with the following code:

import pika
import logging

logging.basicConfig(level=logging.DEBUG)

credentials = pika.PlainCredentials('*******', '**********')
parameters = pika.ConnectionParameters(host='a-25c34e4d-a3eb-32de-abfg-l95d931afc72f.mq.us-west-1.amazonaws.com',
                                       port=5671,
                                       virtual_host='/',
                                       credentials=credentials,
                                       )

connection = pika.BlockingConnection(parameters)

I get pika.exceptions.IncompatibleProtocolError: StreamLostError: ("Stream connection lost: ConnectionResetError(54, 'Connection reset by peer')",) when I run the above.


Solution

  • you're trying to connect through AMQP protocol and AWS is using AMQPS, you should add ssl_options to your connection parameters like this

    import ssl
    
    logging.basicConfig(level=logging.DEBUG)
    
    credentials = pika.PlainCredentials('*******', '**********')
    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    parameters = pika.ConnectionParameters(host='a-25c34e4d-a3eb-32de-abfg-l95d931afc72f.mq.us-west-1.amazonaws.com',
                                           port=5671,
                                           virtual_host='/',
                                           credentials=credentials,
                                           ssl_options=pika.SSLOptions(context)
                                           )
    
    connection = pika.BlockingConnection(parameters)