2 Steps to Install Elasticsearch on an AWS EC2 Amazon Linux 2 Instance

The best way to do it in 2 minutes.

Photo by Csaba Balazs on Unsplash

Truth be told, there are tons of solutions out there to set up an Elasticsearch instance right away with a lot of awesome features and with the least headache.

That said, for many reasons, you may need a self-hosted instance — for your staging deployment or to decrease your bill, to learn, and so on.

AWS offers to anyone that creates a new account, 12 months of free usage of most of its services. In this article, I’ll walk you through the steps to install your own Elasticsearch instance on AWS and we’ll be using Amazon Linux 2 as our instance Operating System.

Prerequisites

  • You need an AWS account
  • … and that’s all.

1- Set up the EC2 instance

First, head up to your AWS console and type EC2 in the AWS services search bar. Once in the EC2 section, launch a new instance.

Choose Amazon Linux 2 as the platform to install your instance.

And then complete all the needed steps till the generation of a key pair that will allow you to connect to the newly created instance through SSH.

Give a name to your key pair. I chose aws_elasticsearch_instance_key.pem.

Then download the key pair and PLEASE PLEASE, DON’T SHARE IT WITH ANYONE.

Then click on Launch instances to start your instance. We’re done with this first step. Let’s work on the second one.

2- Install Elasticsearch

This is the most relevant step in our setup journey.

Launch a terminal and let’s connect to the newly created EC2 instance. We need the .pem file you’ve just downloaded. Go to your AWS console and click on the instance you created then on the Connect button.

Go to the SSH tab and copy the given example of the command that AWS provides you. Run that command in your terminal and you should get connected to the instance.

That’s done. Let’s first install Java. Elasticsearch uses Java so installing it is mandatory. In your terminal, run:

sudo yum install java-1.8.0 -y

Elasticsearch evolves very quickly so we need to check the latest version to be able to install it with the latest and fancy features.

Get the current Elastic version from this link: https://www.elastic.co/guide/en/elasticsearch/reference/current/rpm.html#install-rpm.

The latest version at the time of writing this article is 7.15.2. Don’t forget to run the following commands with the version you’ve checked and chosen.

Let’s download Elasticsearch and its SHA key to be sure that we’re not going to get a corrupted file.

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.15.2-x86_64.rpmwget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.15.2-x86_64.rpm.sha512

Then let’s check if the download file is secured:

sha512sum -c elasticsearch-7.15.2-x86_64.rpm.sha512

You should get “OK” as output and that means that we got the file coming from Elastic.

Now let’s install Elasticsearch.

sudo rpm --install elasticsearch-7.15.2-x86_64.rpm

We need to launch our Elasticsearch instance immediately after the system boots up. To do so, run the following commands:

sudo systemctl daemon-reload
sudo systemctl enable elasticsearch

Let’s start the Elasticsearch service:

sudo systemctl start elasticsearch

To stop your Elasticsearch that is running, you just have to run:

sudo -i service elasticsearch stop

Now guys, let’s check if Elasticsearch is running on our instance.

You can test that your Elasticsearch instance is running by sending an HTTP request to port 9200 on localhost:

curl -XGET -H "Content: application/json" "127.0.0.1:9200/_cluster/health?pretty"

You should get this output:

{
"cluster_name" : "elasticsearch",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 1,
"active_shards" : 1,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}

And Tada !!! We’re done. Our cluster is well set up. In the coming articles, we’ll learn how to secure our Elasticearch instance and how to enable its remote access.

Thank you’ll guys. Take care and be learning.

Certified AWS Solution Architect, Fullstack Software Engineer & DevOps. I like Solving Challenging Software Engineering Problems & Building Amazing Solutions.