Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building gRPC server on Amazon EC2

I met a problem when I try to build a gRPC server/client on Amazon EC2 instances.

I have an instance A (with private ip: for example 1.2.3.4). The server code is like

from concurrent import futures
import time
import math

import grpc

import helloworld_pb2

_ONE_DAY_IN_SECONDS = 60 * 60 * 24

class Greeter(helloworld_pb2.GreeterServicer):

  def SayHello(self, request, context):
    return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)


def serve():
  server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
  helloworld_pb2.add_GreeterServicer_to_server(Greeter(), server)
  server.add_insecure_port('1.2.3.4:50051')

  server.start()
  try:
    while True:
      time.sleep(_ONE_DAY_IN_SECONDS)
  except KeyboardInterrupt:
    server.stop(0)

if __name__ == '__main__':
  serve()

On the other hand, the instance B has private ip 2.3.4.5, and I would like to run client script on it

from __future__ import print_function

import grpc

import helloworld_pb2


def run():
  channel = grpc.insecure_channel('1.2.3.4:50051')
  stub = helloworld_pb2.GreeterStub(channel)
  response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
  print("Greeter client received: " + response.message)


if __name__ == '__main__':
  run()

The client and server code runs well on a local machine. However, when I try to run them on ec2 clusters, the client fails to find the server

Traceback (most recent call last):
  File "helloworld_client.py", line 47, in <module>
    run()
  File "helloworld_client.py", line 42, in run
    response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
  File "/usr/local/lib/python3.4/dist-packages/grpc/_channel.py", line 481, in __call__
    return _end_unary_response_blocking(state, False, deadline)
  File "/usr/local/lib/python3.4/dist-packages/grpc/_channel.py", line 432, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNAVAILABLE, )>

What should I do to get the script running?

Thanks.

like image 814
fois Avatar asked Nov 09 '22 07:11

fois


1 Answers

I found where the problem is. By setting Security Group - Input - types - all traffic, the connection between server and client works.

like image 130
fois Avatar answered Dec 04 '22 14:12

fois