Accessing local SQS service from another docker container using environment variables

佐手、 提交于 2019-12-02 09:56:22

I think there is no configuration for SSL in your docker-compose file, so the issue with might be with https. try to change https to http.

QUEUE_ENDPOINT=http://sqs_service:9324
FEEDER_QUEUE_URL=http://sqs_service:9324/queue/feeder
PREDICTION_QUEUE_URL=http://sqs_service:9324/queue/prediction

Also, try to call SQS container by name without using ENV for debugging.

import boto3
sqs=boto3.client('sqs', endpoint_url="http://sqs_service:9324", region_name='default')
# Create a SQS queue
response = sqs.create_queue(
    QueueName='SQS_QUEUE_NAME',
    Attributes={
        'DelaySeconds': '60',
        'MessageRetentionPeriod': '86400'
    }
)

print(response['QueueUrl'])

Tested with alpine-sqs image.

Somehow I was not able to use the SQS queue using the service name mentioned in the environment variables. This is what I did instead.

Got the service name from environment variables, got the IP address of the service using socket library in python and used the IP address to format and create the QUEUE endpoint url.

import socket
queue_endpoint_service = os.getenv("QUEUE_ENDPOINT_SERVICE")
queue_endpoint_port = os.getenv("QUEUE_ENDPOINT_PORT")
feeder_queue = os.getenv("FEEDER_QUEUE")
prediction_queue = os.getenv("PREDICTION_QUEUE")
queue_endpoint_ip = socket.gethostbyname(queue_endpoint_service)
queue_endpoint = f"http://{queue_endpoint_ip}:{queue_endpoint_port}"
mc = boto3.client('sqs', endpoint_url=queue_endpoint, region_name='default')
feeder_queue_url = f"{queue_endpoint}/queue/{feeder_queue}"
prediction_queue_url = f"{queue_endpoint}/queue/{prediction_queue}"

I'm able to send the messages now by hitting the endpoints in flask app.

Note: Also used the new docker image which Adiii mentioned. Not using the old image anymore.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!