Automatically execute script when AWS instance starts

Modify user data per instance

Instance => Operations  =>  Instance Settings  =>  Edit User Data

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
--//--

- [scripts-user, always]: Execute this script again after reboot

/bin/echo "Hello World" >> /tmp/testfile.txt : This part can be replaced with your own script code

Note: This script task is executed by the root user , so you need to pay attention to the path of ~ in the shell code

reference:

Running User Data After EC2 Instance Initial Launch | AWS re:Post

Run commands on your Linux instance at launch - Amazon Elastic Compute Cloud

Read script to perform auto-configuration issues after restarting EC2 instance on Amazon AWS. _Will aws userdata restart the instance?_Chaer Beer Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/GrimRaider/article/details/131461571