sam.hay
sam.hay

Reputation: 127

How to send data between 2 EC2 instances

I have two AWS EC2 instances, one running a.py and b.py. These two programs use data produced by the other to complete tasks, a.py waits for b.py to create some data that it uses to create some data that a.py will use to create data that b.py will .... basically, they will keep passing data back and forth until a condition is met.

I haven't been able to find a place that concretely defines how to do this. Optimally, I want to do this with the smallest time lag.

Upvotes: 0

Views: 117

Answers (1)

Marek Knappe
Marek Knappe

Reputation: 442

As you are using AWS already, the native solution for things like that is SQS queue. To achieve that task, you need to create two SQS queue:

  • SQS-Queue-App-A
  • SQS-Queue-App-B

Then make a.py, something along these lines:

import boto3

# Create SQS client
sqs = boto3.client('sqs')

queue_a_url = 'SQS_QUEUE_URL'
queue_b_url = 'SQS_QUEUE_URL'

while (1):
  messages = sqs.receive_messages(
        MaxNumberOfMessages=10,
        WaitTimeSeconds=10,
        QueueUrl=queue_a_url,
    )
  for msg in messages:
    logger.info("Received: %s: %s", msg.message_id, msg.body)
    #Do whatever you need to do with the message 
    
    response = sqs.send_message(
      QueueUrl=queue_b_url,

      MessageBody=(
        'something to process by script B'
      )
    )

You can create them as FIFO queues to be sure that messages are in sequences.

Upvotes: 2

Related Questions