Quantcast
Channel: Active questions tagged amazon-ec2 - Stack Overflow
Viewing all articles
Browse latest Browse all 29245

How can I connect to an Elasticsearch cluster on EC2 from a Flask application?

$
0
0

I have an EC2 machine running Ubuntu with 4G of RAM. Elasticsearch 7.6.0 is installed and I can access it remotely from my local machine using:

curl http://ec2-ip-address:9200

result is:

{
  "name" : "ip-xxx-xx-x-xx",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "_na_",
  "version" : {
    "number" : "7.6.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "7f634e9f44834fbc12724506cc1da681b0c3b1e3",
    "build_date" : "2020-02-06T00:09:00.449973Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

The elasticsearch.yml config file is as follows:

# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#

Everything else is left unchanged which means it is all commented.

The jvm.options file is as follows:

################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms1g
-Xmx1g

################################################################

For the rest of this file default options retained.

On the EC2 server, inbound rules are as follows:

Ports   Protocol    Source  launch-wizard-3
80  tcp 0.0.0.0/0, ::/0 ✔
22  tcp 0.0.0.0/0   ✔
9200    tcp 0.0.0.0/0, ::/0 ✔

As I said previously I can connect from my terminal but when I attempt to connect with a flask app running on my local machine I get an error message which basically says connection timed out.

elasticsearch.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host='xx.xxx..xx', port=9200): Read timed out. (read timeout=10))

The Python code is as follows:

from flask import Flask
import json
from datetime import datetime
from elasticsearch import Elasticsearch

application = Flask(__name__)

es = Elasticsearch("http://xx.219.xx.x6")
# es = Elasticsearch([{'host': 'ec2-xx-218-xx-4.us-west-2.compute.amazonaws.com', 'port': 443,  'use_ssl': True}])

# es = Elasticsearch("http://1xx.31.xx.xx")

@application.route('/', methods=['GET', 'POST'])
def index():
    print(es.cluster.health())
    return "OK"

As you can see I have tried multiple connection configs all with same error. I had originally thought the error was caused by not specifically opening port 9200 on the EC2 server but this will not resolve the problem now.

Possibly there is insufficient RAM. I have tried different timeout=xx options in the code. All unsuccessful.

Can anybody help please or point me to a Flask/Ealsticsearch tutorial? Thanks!


Viewing all articles
Browse latest Browse all 29245

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>