pythondjangoamazon-web-servicesamazon-ec2amazon-app-runner

Communication between AWS App Runner & EC2 instance


I've read about this subject in a couple of questions, but I haven't found the solution yet. So, I'm reasking almost the same question, as many others have done before:

I have a Django app deployed on an App Runner server, and I have a backend process (in Python too) deployed on an EC2 instance.

I need to be able to communicate values between this two servers, so when the user interacts with the django app, this calls the EC2 backend, processes the info and returns an output.

Currently, I'm trying this like so:

On my Django app I have this code. On button click it executes the ecconsult():

from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import json
import requests

def home(request):
    return render(request, 'index.html')


def ecconsult(request):
    data_to_send = "communication test"
    response = requests.post('http://specific_host:specific_port/api/procesar', json=data_to_send)

    if response.status_code == 200:
        jsondatabe = response.text
        response_data = {'jsondatabe': jsondatabe}

    else:
        print('Error al procesar la call del backend')

    return render(request, 'index.html', response_data)

And on my EC2 BackEnd server, I recieve the data like so:

from flask import Flask, request, jsonify
from flask_cors import CORS

app = Flask(__name__)

CORS(app, resources={r"/api/*": {"origins": "Django_app_domain"}})

@app.route('/')
def hello():
    print("accesing from outside")
    return 'Hello backend!'

@app.route('/api/procesar', methods=['POST'])
def procesar_datos():
    data = request.get_json()
    print(data)

    response = "connection successful"
    print("Returning data to webapp")
    return response

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=specific_port)

If I run the Django app in localhost, it does the communication with the EC2 server perfectly, but when I deploy it on the App Runner server, it gives me a:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Someone has any advice? If I deploy the django app on the App Runner, works perfectly without the "request" library, but with it gives the previous error (it shouldn't be the installation of this library, but I don't rule anything out either).

I've also tried the flask_cors in order to prevent the CORS to shut down the error of trying to access an external unknown source.


Solution

  • It turned out that the log process was giving me this:

    botocore 1.31.72 requires urllib3<1.27,>=1.25.4; python_version < "3.10", but you have urllib3 2.0.7 which is incompatible.
    

    In localhost I was working with the latest urllib3 compatible with the latest python, but I had to change it into the 1.25.4 version because of the App Runner dependencies.

    Thanks for the advice!