16

There is a method to setup an EC2 machine to execute Kafka starting script on startup? I use also java Aws SDK, so I accept both solution for a program java that run command on EC2 instance and solutions for a bash script mode that run kafka script at startup.

5 Answers 5

11

A script can be passed in the User Data property.

If you are using the Amazon Linux AMI, and the first line of the script begins with #!, then the script will be executed the first time that the instance is started.

For details, see: Running Commands on Your Linux Instance at Launch

Sign up to request clarification or add additional context in comments.

Comments

10

Adding a script under User Data in CloudFormation only runs once, right when the instance is launched but not when the instance is restarted which is what I needed for my use case. I use the rc.local approach as commented above and here. The following in effect appends my script to the rc.local file and performs as expected:

Resources:
  VM:
    Type: 'AWS::EC2::Instance'
    Properties:
      [...]
      UserData:
        'Fn::Base64': !Sub |
          #!/bin/bash -x
          echo 'INSTANCEID="$(curl -s http://169.254.169.254/latest/meta-data/instance-id)"' >> /etc/rc.local
          #echo 'INSTANCEID=$(ls /var/lib/cloud/instances)' >> /etc/rc.local
          echo 'echo "aws ec2 stop-instances --instance-ids $INSTANCEID --region ${AWS::Region}" | at now + ${Lifetime} minutes' >> /etc/rc.local

Additional tip: You can inspect the user data (the current script) and modify it using the AWS console by following these instructions: View and update the instance user data.

Comments

4

What is the OS of EC2 instance?

  1. You could use userdata script at instance launch time. Remember this is just 1time activity

  2. If your requirement is to start the script everytime you reboot EC2 instance then you could make use of rc.local file on Linux instances which is loaded at OS boot time.

3 Comments

I'm using Ubuntu server 16.04. I need to start Zookeeper and Kafka at launch of free tier EC2 istances.
Make use of user data script at instance launch time
I don't think current Amazon Linux uses rc.local. No luck with it, no matter how i chmod it etc.
4

rc.local didn't work for me.

I used crontab following this guide, which sets up a cron job that can run a script on start up.

https://phoenixnap.com/kb/crontab-reboot

It's essentially

crontab -e
<select editor>
@reboot <script to run>

Comments

0

If you are running a windows EC2 you'll want to read: https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html

Example:

<script>
echo Current date and time >> %SystemRoot%\Temp\test.log
echo %DATE% %TIME% >> %SystemRoot%\Temp\test.log
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.