AWSTemplateFormatVersion: 2010-09-09 Description: (qs-1r0baum14) Creates Cassandra Node (1-3). Parameters: OSSVersion: AllowedValues: # - 3.11.5 # - 3.11.6 - 4.0 Default: 4.0 Description: "Apache Cassandra version to install." Type: String ClusterName: Description: Name of the Cassandra Cluster. Type: String Default: Cassandra-Cluster CreateClusterWithPublicIP: Description: "Whether to create the Cassandra Cluster nodes in public subnet" Type: String Default: "false" AllowedValues: - "false" - "true" DatacenterName: Description: Name of the Cassandra Datacenter. Type: String Default: OSS-dc0 SubnetID: Description: Subnet Id Type: 'AWS::EC2::Subnet::Id' KeyPairName: Type: 'AWS::EC2::KeyPair::KeyName' ConstraintDescription: Name of an existing EC2 KeyPair. SecGroup: Description: "Security Group ID." Type: String InstanceType: AllowedValues: - t2.medium - t2.large - t2.xlarge - t2.2xlarge - m4.large - m4.xlarge - m4.2xlarge - m4.4xlarge - m4.10xlarge - m4.16xlarge - c4.large - c4.xlarge - c4.2xlarge - c4.4xlarge - c4.8xlarge - r4.large - r4.xlarge - r4.2xlarge - r4.4xlarge - r4.8xlarge - r4.16xlarge ConstraintDescription: "Must be a valid EC2 instance type." Default: m4.large Description: "EC2 instance type" Type: String VolumeSize: Type: Number Description: "EBS volume size of the Cassandra Cluster Nodes in GB" Default: 512 SeedNodeIps: Description: "csv IPs of the seedsnodes." Type: String Default: "" NodeNumber: Description: "Optional name for the node." Type: String AllowedValues: - 1 - 2 - 3 - "" Default: "" Conditions: CreateInPublicSubnet: !Equals - !Ref CreateClusterWithPublicIP - true Seed2: !Equals [!Ref NodeNumber, 2] Mappings: AWSAMIRegionMapNode: ap-northeast-1: US1804HVM: ami-0278fe6949f6b1a06 ap-northeast-2: US1804HVM: ami-00edfb46b107f643c ap-south-1: US1804HVM: ami-0b44050b2d893d5f7 ap-southeast-1: US1804HVM: ami-0f7719e8b7ba25c61 ap-southeast-2: US1804HVM: ami-04fcc97b5f6edcd89 ca-central-1: US1804HVM: ami-0edd51cc29813e254 eu-central-1: US1804HVM: ami-0e342d72b12109f91 me-south-1: US1804HVM: ami-051274f257aba97f9 ap-east-1: US1804HVM: ami-c790d6b6 eu-north-1: US1804HVM: ami-050981837962d44ac eu-west-1: US1804HVM: ami-0701e7be9b2a77600 eu-west-2: US1804HVM: ami-0eb89db7593b5d434 eu-west-3: US1804HVM: ami-08c757228751c5335 sa-east-1: US1804HVM: ami-077d5d3682940b34a us-east-1: US1804HVM: ami-085925f297f89fce1 us-gov-west-1: US1804HVM: ami-adecdbcc us-gov-east-1: US1804HVM: ami-c29975b3 us-east-2: US1804HVM: ami-07c1207a9d40bc3bd us-west-1: US1804HVM: ami-0f56279347d2fa43e us-west-2: US1804HVM: ami-003634241a8fcdec0 cn-north-1: US1804HVM: ami-0071f6f4df15863cc cn-northwest-1: US1804HVM: ami-0a22b8776bb32836b Resources: CassandraNode: Type: 'AWS::EC2::Instance' CreationPolicy: ResourceSignal: Timeout: PT15M Metadata: 'AWS::CloudFormation::Init': configSets: cs_install: - install_and_enable_cfn_hup - install_cassandra - post_install install_and_enable_cfn_hup: files: /etc/cfn/cfn-hup.conf: content: !Join - '' - - | [main] - stack= - !Ref 'AWS::StackId' - |+ - region= - !Ref 'AWS::Region' - |+ mode: '000400' owner: root group: root /etc/cfn/hooks.d/cfn-auto-reloader.conf: content: !Join - '' - - | [cfn-auto-reloader-hook] - | triggers=post.update - > path=Resources.CassandraNode.Metadata.AWS::CloudFormation::Init - 'action=/usr/local/bin/cfn-init -v ' - ' --stack ' - !Ref 'AWS::StackName' - ' --resource CassandraNode ' - ' --configsets cs_install ' - ' --region ' - !Ref 'AWS::Region' - |+ - | runas=root /lib/systemd/system/cfn-hup.service: content: !Join - '' - - | [Unit] - |+ Description=cfn-hup daemon - | [Service] - | Type=simple - | ExecStart=/usr/local/bin/cfn-hup - |+ Restart=always - | [Install] - WantedBy=multi-user.target commands: 01enable_cfn_hup: command: systemctl enable cfn-hup.service 02start_cfn_hup: command: systemctl start cfn-hup.service install_cassandra: sources: /home/ubuntu: https://datastax-public.s3.amazonaws.com/aws-qs-oss/oss.tar commands: 00_install_ansible: command: | sudo apt-add-repository ppa:ansible/ansible -y sudo apt update sudo apt-get install openjdk-8-jdk -y sudo apt install ansible -y sudo sed -i '/host_key_checking = False/s/^#*\s*//g' /etc/ansible/ansible.cfg 01_link_directory: command: "ln -s /home/ubuntu /home/cassandra" 02_keygen: command: "/home/cassandra/keygen.sh" 03_run_playbooks: command: "/home/cassandra/playbooks.sh" 04_install_cassandra: command: !Sub | #!/bin/bash -xe PrivateIp=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4/) echo "---------CreateClusterWithPublicIP----------" echo ${CreateClusterWithPublicIP} if [ "${CreateClusterWithPublicIP}" = "true" ]; then PublicIp=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4/) else PublicIp="" fi echo $PublicIp echo "-----------" echo "----Before---------" echo ${SeedNodeIps} echo $PrivateIp echo "----Before---------" if [ -z "${SeedNodeIps}" ]; then ansible-playbook -v -u ubuntu -i /home/cassandra/ansible-hosts.cfg \ --private-key /home/ubuntu/.ssh/id_rsa /home/cassandra/cassandra-install.yml \ --extra-vars "cassandra_version=${OSSVersion} cluster_name=${ClusterName} dc=${DatacenterName} seeds=$PrivateIp publicip=$PublicIp" else ansible-playbook -v -u ubuntu -i /home/cassandra/ansible-hosts.cfg \ --private-key /home/ubuntu/.ssh/id_rsa /home/cassandra/cassandra-install.yml \ --extra-vars "cassandra_version=${OSSVersion} cluster_name=${ClusterName} dc=${DatacenterName} seeds=${SeedNodeIps} publicip=$PublicIp" fi echo "----After---------" echo ${SeedNodeIps} echo $PrivateIp echo "----After---------" post_install: commands: 01_post_install_cassandra: command: "touch /tmp/01_post_install_cassandra" Properties: InstanceType: !Ref InstanceType NetworkInterfaces: - DeleteOnTermination: true DeviceIndex: "0" SubnetId: !Ref SubnetID GroupSet: - !Ref SecGroup KeyName: !Ref KeyPairName ImageId: !FindInMap - AWSAMIRegionMapNode - !Ref 'AWS::Region' - US1804HVM BlockDeviceMappings: - DeviceName: /dev/xvdf Ebs: VolumeSize: !Ref VolumeSize VolumeType: gp2 Tags: - Key: Name Value: !Sub - Cassandra_Node${Num} - { Num: !Ref NodeNumber} UserData: Fn::Base64: !Sub | #!/bin/bash #CFN Signaling fuctions (begin) yum install git -y || apt-get install -y git || zypper -n install git function cfn_fail { cfn-signal -e 1 --stack ${AWS::StackName} --region ${AWS::Region} --resource CassandraNode exit 1 } function cfn_success { cfn-signal -e 0 --stack ${AWS::StackName} --region ${AWS::Region} --resource CassandraNode exit 0 } #Load Linux utils until git clone https://github.com/aws-quickstart/quickstart-linux-utilities.git ; do echo "Retrying"; done cd /quickstart-linux-utilities; source quickstart-cfn-tools.source; # Constants # Prep operating systems qs_update-os || qs_err; #qs_bootstrap_pip || qs_err " pip bootstrap failed "; qs_aws-cfn-bootstrap || qs_err " cfn bootstrap failed "; #Run cfn-init configsets cfn-init -v --stack '${AWS::StackName}' --resource CassandraNode --configsets cs_install --region ${AWS::Region} || cfn_fail # Signal cfn-init (final check) [ $(qs_status) == 0 ] && cfn_success || cfn_fail Outputs: Seed1PrivateIpAddress: Value: !GetAtt CassandraNode.PrivateIp Description: C* Node Private IP Seed1PublicIpAddress : Value: !If [CreateInPublicSubnet, !GetAtt CassandraNode.PublicIp, ""] Description: C* Node Public IP Seed2PrivateIpAddress: Condition: Seed2 Value: !GetAtt CassandraNode.PrivateIp Description: C* Node Private IP Seed2PublicIpAddress : Condition: Seed2 Value: !If [CreateInPublicSubnet, !GetAtt CassandraNode.PublicIp, ""] Description: C* Node Public IP