By using AWS re:Post, you agree to the AWS re:Post Terms of Use

How do I utilize user data to automatically run a script with every restart of my Amazon EC2 Linux instance?

3 minute read
2

I want to utilize user data to run a script every time my Amazon Elastic Compute Cloud (Amazon EC2) instance is restarted.

Short description

By default, user data command and cloud-init directives run only during the first boot cycle when an EC2 instance is launched. However, you can configure your user data command and cloud-init directives with a mime multi-part file.

A mime multi-part file allows your command to override how frequently user data is run in the cloud-init package. Then, the file runs the user command. For more information on mime multi-part files, see Mime multi-part archive on the cloud-init website.

Note: It's a best practice to create a snapshot of your instance before you proceed with the following resolution.

Resolution

Warning: Before you start this procedure, review the following:

Complete the following steps:

  1. Make sure that the latest version of cloud-init is installed and functions properly on your EC2 instance.
  2. Make sure that the IAM user or role you use has the ec2:ModifyInstanceAtrribute permission listed in its IAM policy. For more information, see Editing IAM policies.
  3. Open the Amazon EC2 console.
  4. Stop your instance.
  5. Choose Actions, choose Instance Settings, and then choose Edit User Data.
  6. Copy your user command into the Edit user data box, and then choose Save.
    The following example is a shell command that writes "Hello World" to a testfile.txt file in a /tmp directory.
    Content-Type: multipart/mixed; boundary="//"
    MIME-Version: 1.0
     
    --//
    Content-Type: text/cloud-config; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment;
     filename="cloud-config.txt"
     
    #cloud-config
    cloud_final_modules:
    - [scripts-user, always]
    --//
    Content-Type: text/x-shellscript; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="userdata.txt"
     
    #!/bin/bash
    /bin/echo "Hello World" >> /tmp/testfile.txt
    --//--
    By default, cloud-init allows only one content type in user data at a time. However, this example shows both text/cloud-config and text/x-shellscript content-types in a mime-multi part file.
    Set the scripts-user parameter to always so the text/cloud-config content type overrides how frequently user data is run in the cloud-init package.
    The text/x-shellscript content type provides the user command to run the cloud-init cloud_final_modules module.
    Note: Replace the line /bin/echo "Hello World." >> /tmp/testfile.txt with your shell command that you want to run during the instance boot.
  7. Start your EC2 instance again, and then validate that your script runs correctly.
    Note: If your script did not run as expected, then check the /var/log/cloud-init-output.log file. For more information, see How to debug cloud-init on the cloud-init website.

Related information

Run commands when you launch an EC2 instance with user data input

How do I run a command on a new EC2 Windows instance at launch?

AWS Systems Manager Automation

User data formats on the cloud-init website

AWS OFFICIAL
AWS OFFICIALUpdated 3 months ago
6 Comments

To run a script on every boot of an instance, place the script into this directory:

/var/lib/cloud/scripts/per-boot/

Amazon Linux uses Cloud-Init to run User Data on first boot. It will also run any scripts in the above directory after EVERY boot.

replied 2 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 2 years ago

#!/bin/bash /bin/echo "Hello World" >> /tmp/testfile.txt

where this content is written inside ec2 ?

replied 2 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
EXPERT
replied 2 years ago

I am trying to run a jar file while starting the EC2 instance.

java -jar /home/ec2-user/springboot.0.0.1-SNAPSHOT.jar

I ssh into EC2 and lsof -i:8090 it does not show any pid.

How can I make sure that the spring boot application is running on port 8090 (logs?)

replied 2 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
EXPERT
replied 2 years ago