user1903663
user1903663

Reputation: 1725

how can I connect, from a flask app, to a remote elasticsearch cluster on aws?

I have an elasticsearch cluster running on an EC2 server. I get a variety of different error mesages when I try to connect.

Currently, in the elasticsearch.yml file all the transport items are commented out but I have tried:

network.host: 0.0.0.0
and
network.host: ec2-xx-xxx-xxx.aws.instance.com

In my flask app the code is as follows:

from datetime import datetime
from flask import Flask, jsonify, request
from elasticsearch import Elasticsearch

#es = Elasticsearch()http://34.245.51.240/
es = Elasticsearch(['ec2-34-xxx-xx-240.eu-west-1.compute.amazonaws.com','9200'])
#es = Elasticsearch(['34.245.51.240','9200'])

application = Flask(__name__)

@application.route('/', methods=['GET'])
def index():
    #results = es.get(index='contents', doc_type='title', id='my-new-slug')
    #return jsonify(results['_source'])
    doc = {
    'author': 'kimchy',
    'text': 'Elasticsearch: cool. bonsai cool.',
    'timestamp': datetime.now(),
    }
    res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
    print(res['res'])
    return res


#application.run(port=5000, debug=True)
if __name__ == '__main__':
    application.debug = True
    application.run()

I have googled multiple times and tried every possible configuration that I can find.

What is the correct way to achieve this?

Thank you.

Upvotes: 0

Views: 878

Answers (1)

user1903663
user1903663

Reputation: 1725

I hope this will help other people.

elasticsearch defaults to Port 9200 so it is necessary to open such a Port on the EC2 server as such:

CustomTCP TCP 9200 0.0.0.0

This is done by editing the security group wizard that set up security groups when you set up the server.

Then in your Python application the connection string is:

es = Elasticsearch("http://00.111.222.33")  //the public IP you can see on your EC2 dashboard

That's it. Hours of anguish and so simple.

Upvotes: 1

Related Questions