Sounds like there are two issues here (automatically starting Elasticsearch and discovering other instances). I'll try to answer both.
Starting automatically
Assuming your build is based on a recent Linux distribution that has systemd available, you can install a system unit file to automatically start Elasticsearch. According to the docs at https://www.elastic.co/guide/en/elasticsearch/reference/current/setting-system-settings.html#systemd it looks like the official RPM and APT packages include one. Assuming you installed from a package the system unit may actually already be there.
It is likely disabled and you can check:
andy@search-logs2:~$ sudo systemctl is-enabled elasticsearch
disabled
If this is the case, during your AMI build you would just need to run:
andy@search-logs2:~$ sudo systemctl enable elasticsearch
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
If you installed Elasticsearch another way and don't have a unit file, the official Ansible Elasticsearch role installs one. You could just copy theirs as an example: https://github.com/elastic/ansible-elasticsearch/blob/master/templates/systemd/elasticsearch.j2. You would then use Ansible or systemctl
to enable the unit to start on boot.
Discovering other isntances
There are plugins for discovering instances in cloud providers like AWS. Check out the EC2 discovery plugin at https://www.elastic.co/guide/en/elasticsearch/plugins/6.3/discovery-ec2.html. By default, it will try and discover any other instances in the account. Limit by setting groups
or host_type
settings: https://www.elastic.co/guide/en/elasticsearch/plugins/6.3/_settings.html.
I have a similar setup but in Google Cloud and when adding an instance the new node starts automatically using the system unit file and joins the cluster a minute later using the GCP discovery plugin.