I need to create two Ec2 instances using AMI and make it as multi-node using CloudFormation template. The AMI installed the elasticsearch in it. I need to make one a master node and another one a data node.
My CF template script,
AWSTemplateFormatVersion: '2010-09-09'
#Transform: 'AWS::Serverless-2016-10-31'
Description: AWS CloudFormation Template with EC2InstanceWithSecurityGroup
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
RemoteAccessLocation:
Description: The IP address range that can be used to access to the EC2 instances
Type: String
MinLength: '9'
MaxLength: '18'
Default: 0.0.0.0/0
AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
Resources:
ES1EC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.2xlarge
SecurityGroups:
- !Ref 'InstanceSecurityGroup'
KeyName: !Ref 'KeyName'
ImageId: ami-xxxxxxxxxxxxxxxx
#DependsOn: ES2EC2Instance
UserData:
Fn::Base64: !Sub |
#!/bin/bash -ex
cat > /etc/elasticsearch/elasticsearch.yml<<EOF1
network.host: "${EC2_PRIVATE_IP}"
http.port: 9200
http.max_content_length: 1gb
node.name: node-1
node.roles: [ master, data, ingest ]
transport.port: 9300-9400
discovery.seed_hosts: ["${ES1EC2Instance.PrivateIp}", "${ES2EC2Instance.PrivateIp}"]
cluster.initial_master_nodes: ["node-1"]
gateway.recover_after_nodes: 2
EOF1
## Restart Elasticsearch
sudo systemctl restart elasticsearch
ES2EC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.2xlarge
SecurityGroups:
- !Ref 'InstanceSecurityGroup'
KeyName: !Ref 'KeyName'
ImageId: ami-xxxxxxxxxxxxxxxx
#DependsOn: ES1EC2Instance
DependsOn: ES1EC2Instance
UserData:
Fn::Base64: !Sub |
#!/bin/bash -ex
cat > /etc/elasticsearch/elasticsearch.yml<<EOF1
network.host: "${ES2EC2Instance.PrivateIp}"
http.port: 9200
http.max_content_length: 1gb
node.name: node-2
node.roles: [ data, ingest ]
transport.port: 9300-9400
discovery.seed_hosts: ["${ES1EC2Instance.PrivateIp}", "${ES2EC2Instance.PrivateIp}"]
cluster.initial_master_nodes: ["node-1"]
gateway.recover_after_nodes: 2
EOF1
## Restart Elasticsearch
sudo systemctl restart elasticsearch
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH (22), HTTP (8080),
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: !Ref 'RemoteAccessLocation'
- CidrIp: 0.0.0.0/0
FromPort: '8080'
IpProtocol: tcp
ToPort: '8080'
- IpProtocol: tcp
FromPort: '9200'
ToPort: '9200'
CidrIp: !Ref 'RemoteAccessLocation'
Outputs:
AZ:
Description: Availability Zone of the newly created EC2 instance for ES
Value: !GetAtt 'ES1EC2Instance.AvailabilityZone'
PublicDNS:
Description: Public DNSName of the newly created EC2 instance for ES
Value: !GetAtt 'ES1EC2Instance.PublicDnsName'
PublicIP:
Description: Public IP address of the newly created EC2 instance for ES
Value: !GetAtt 'ES1EC2Instance.PublicIp'
How can I update the elasticsearch.yaml to make it as a multi-node using CloudFormation Template?