Skip to main content

Deploy Hazelcast Cluster on AWS EC2

What You'll Learn​

In this tutorial, you will create two EC2 instances with Hazelcast members and see them connecting to each other and forming a cluster via Hazelcast AWS plugin.

Before you Begin​

Steps 1 to 4 cover the creation and the configuration of VPC, internet gateway, route table, subnet and IAM role. If you already have these configured you can skip them. However, make sure that you have these properties before creating instances:

  • Have an IAM role with ec2:DescribeInstances permission
  • Have a security group with at least port 5701 open
  • Able to establish SSH connections to instances

1. Create and Configure a VPC​

Let's start with creating a VPC on which our instances will run.

1.1. Create a VPC​

Under VPC > Your VPCs section of AWS console, create a new VPC and give it a name you can remember in the next steps:

ss1 create vpc

1.1.1. Create an Internet Gateway​

Now create an internet gateway to be attached to your VPC. You will need this gateway to establish SSH connections to instances:

ss1.1.1 create igw

1.1.2. Attach the Internet Gateway​

After creating an internet gateway, attach it to your VPC.

Navigate to VPC > Internet gateways > Attach to VPC section. Choose the VPC you just created among the available ones:

ss1.1.2 attach igw

1.2. Add Default Route​

Now add the default route to the route table. This step will make you able to connect to instances via SSH.

Under the VPC description, navigate to route table shown in the orange box:

ss1.2.1 nav to routes

Under Routes tab, you need the default one (0.0.0.0/0) listed. Edit routes to add this one:

ss1.2.2 list routes

As the target, pick the internet gateway you created in section 1.1.1 and save:

ss1.2.3 add route

2. Create a Subnet in the VPC​

Let's continue with creating a subnet in the VPC.

Under VPC > Subnets section, choose Create Subnet. Pick the proper VPC and give the subnet a recognizable name:

ss2 create subnet

3. Create an IAM Role​

The EC2 instances we will create need the IAM role to have ec2:DescribeInstances permission. This way, Hazelcast members are able to fetch other instance IPs and connect them dynamically. If you already have an IAM role, check the permissions. Otherwise, create a new one with the permission. For instance, AmazonEC2ReadOnlyAccess policy contains DescribeInstances permission and is enough to complete this guide.

  • Navigate to IAM > Roles and create a new role on Access Management > Role > Create Role section for EC2 use case:

ss3 create iam role

  • Attach permission policies for the role:

ss3 create iam with permission

4. Create a Security Group​

As the last step, create a security group in your VPC with the proper inbound rules for Hazelcast. Allow port 5701 among inbound rules as it's the default port of Hazelcast. If you plan to run more than one Hazelcast member on an EC2 Instance, then you should open more ports. Also, do not forget to allow SSH port:

Navigate to VPC > Security Groups and create a new one:

ss4 create security group

5. Create EC2 Instances​

Let's start creating our instances via LaunchInstanceWizard under EC2 > Launch Instances on AWS Console.

  • Choose an Amazon Machine Image (AMI). Amazon Linux is used in this guide:

ss5.1 create instance

  • Choose an instance type:

ss5.2 create instance

  • Now configure instance details with the VPC, subnet and IAM roles you created above. Notice that the number of instances is 2. Also, enable Auto-assign Public IP to establish SSH connections later on.

ss5.3 create instance

  • Next, add a unique tag to the instances. This is optional but recommended if your AWS account has many running instances associated with:

ss5.4 create instance

  • Finally, select the security group you created above:

ss5.5 create instance

As the last step, select your key pair for the instances and that's it. You can launch instances now.

6. Create a Hazelcast Cluster​

  • Now that you have 2 instances running with the same IAM role, let's connect to each of them via SSH:
shell
ssh -i "<your-key-pair>.pem" ec2-user@<instance-public-ip>
note

If you encounter any problem regarding the SSH connection, see SSH troubleshooting page in AWS documentation.

  • After SSH connection is established, install Hazelcast CLI to the instances:
shell
wget https://repository.hazelcast.com/rpm/hazelcast-rpm.repo -O hazelcast-rpm.repo
sudo mv hazelcast-rpm.repo /etc/yum.repos.d/
sudo yum install hazelcast
  • Now start Hazelcast members in both EC2 instances:
shell
hz start

When Hazelcast members find each other, you will see a log similar to below for each instance:

shell
Members {size:2, ver:2} [
Member [10.0.x.x]:5701 - 1cc76eb9-4032-4ba2-870c-43baba3cbd88
Member [10.0.y.y]:5701 - 3e8b66fc-52eb-4379-ae11-4b6e30549055 this
]
note

By default, Hazelcast will use the current region, the IAM Role attached to the EC2 instance and the port range 5701-5708 to discover other Hazelcast members in other instances. You can configure Hazelcast such that it tries to connect to certain EC2 instances only. For instance, if you use the tag from Section 5, Hazelcast will filter the available instances based on this tag and won't attempt to connect if the tag does not match:

shell
export HZ_NETWORK_JOIN_AWS_ENABLED=true
export HZ_NETWORK_JOIN_AWS_TAGKEY=cluster-tag
export HZ_NETWORK_JOIN_AWS_TAGVALUE=guide-ec2-cluster
hz start

You can find all discovery configuration details on Hazelcast AWS discovery plugin documentation.

Summary​

In this tutorial, you created all AWS components you need to form a Hazelcast cluster on EC2. Then you started two Hazelcast members on two different EC2 instances and saw them connecting each other and forming a cluster. If you created more EC2 instances and Hazelcast members in the same way, these members would also find each other and they all would form a single cluster.

See Also​