1
votes

Before I begin just letting you know I ma pretty new to the whole thing of cloudformation and coding in general

I have been trying to write a cloud formation template to launch an instance with a single zookeeper node and going step by step. So I wrote my userdata to download Kafka tar and unzip it.

now instead of using " bin/zookeeper-server-start.sh -daemon config/zookeeper.properties " to start the zookeeper, I want to make a file named zookeeper and then write a shell script on it and make it executable how do I do this in cloudformation ??

 DAEMON_PATH=/home/ubuntu/kafka/bin
 DAEMON_NAME=zookeeper
 
 PATH=$PATH:$DAEMON_PATH
 
 # See how we were called.
 case "$1" in
   start)
         # Start daemon.
         pid=`ps ax | grep -i 'org.apache.zookeeper' | grep -v grep | awk '{print $1}'`
         if [ -n "$pid" ]
           then
             echo "Zookeeper is already running";
         else
           echo "Starting $DAEMON_NAME";
           $DAEMON_PATH/zookeeper-server-start.sh -daemon /home/ubuntu/kafka/config/zookeeper.properties
         fi
         ;;
   stop)
         echo "Shutting down $DAEMON_NAME";
         $DAEMON_PATH/zookeeper-server-stop.sh
         ;;
   restart)
         $0 stop
         sleep 2
         $0 start
         ;;
   status)
         pid=`ps ax | grep -i 'org.apache.zookeeper' | grep -v grep | awk '{print $1}'`
         if [ -n "$pid" ]
           then
           echo "Zookeeper is Running as PID: $pid"
         else
           echo "Zookeeper is not Running"
         fi
         ;;
   *)
         echo "Usage: $0 {start|stop|restart|status}"
         exit 1
 esac
 
 exit 0

This is the script I am trying to write so I can start and stop zookeeper using sudo service zookeeper start/stop

manually using the CLI I did the below and then just paste the script in zookeeper and save it.

sudo nano /etc/init.d/zookeeper
sudo chmod +x /etc/init.d/zookeeper
sudo chown root:root /etc/init.d/zookeeper

Thank you, hope you guys understand my question.

1

1 Answers

0
votes

There are few ways of doing this:

  • Store the file in S3 and download it using AWS SDK in your UserData.
  • Create the file directly in UserData
  • Use AWS::CloudFormation::Init to define the file.
  • Create pre-baked AMI with the script and launch instance using the AMI

Below is an example of UserData which uses HEREDOC in bash to create such file.

      UserData: 
        Fn::Base64: |
              #!/bin/bash -ex                            
           
              cat >/etc/init.d/zookeeper << 'EOL'

              DAEMON_PATH=/home/ubuntu/kafka/bin
              DAEMON_NAME=zookeeper

              PATH=$PATH:$DAEMON_PATH

              # See how we were called.
              case "$1" in
                start)
                      # Start daemon.
                      pid=`ps ax | grep -i 'org.apache.zookeeper' | grep -v grep | awk '{print $1}'`
                      if [ -n "$pid" ]
                        then
                          echo "Zookeeper is already running";
                      else
                        echo "Starting $DAEMON_NAME";
                        $DAEMON_PATH/zookeeper-server-start.sh -daemon /home/ubuntu/kafka/config/zookeeper.properties
                      fi
                      ;;
                stop)
                      echo "Shutting down $DAEMON_NAME";
                      $DAEMON_PATH/zookeeper-server-stop.sh
                      ;;
                restart)
                      $0 stop
                      sleep 2
                      $0 start
                      ;;
                status)
                      pid=`ps ax | grep -i 'org.apache.zookeeper' | grep -v grep | awk '{print $1}'`
                      if [ -n "$pid" ]
                        then
                        echo "Zookeeper is Running as PID: $pid"
                      else
                        echo "Zookeeper is not Running"
                      fi
                      ;;
                *)
                      echo "Usage: $0 {start|stop|restart|status}"
                      exit 1
              esac

              exit 0
              EOL

              chmod +x /etc/init.d/zookeeper
              chown root:root /etc/init.d/zookeeper

Please not I haven't run this code, thus I can't verify if it will work out of the box.