I have a requirement, where I need to run a shell script inside my ec2 instance when the instance state changes from stopped to running.
I used cloud watch for this requirement but the event is trigerring the shell script when the instance is in pending state but not in running state(I have used "ec2 instance state-change notification" and "running" as parameter. Target as RunCommand).
Now I am trying to use SSM association I was succeded in trigerring the shell script manually but i need to automate it (i.e., when ec2 instance status changed to running only the bash script should start running)
I think what you're looking for is how to automatically execute a script with every restart of your EC2 instance (these options do not use AWS Systems Manager).
Use user data scripts and cloud-init directives
By default, user data scripts and cloud-init directives run only
during the first boot cycle when an instance is launched. However, you
can configure your user data script and cloud-init directives with a
mime multi-part file. A mime multi-part file allows your script to
override how frequently user data is executed in the cloud-init
package. Then, the file executes the user script.
https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/
Alternatively, you can execute a cron job after system reboot, see more info here:
https://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/
Related
I would like to perform domain join on any servers provisioned in my AWS environment.
For ASG and EKS, can launch template be used to run custom scripts as soon as new nodes are launched?
Yes, with ASG you can run custom scripts. You need to modify your User data in your launch template or launch configuration. You can retrive scripts saved in s3 or pass any commands or script directly. If it's just a join to freeipa for example. You can do it with a simple one line command(don't foget to add #!/bin/bash in the begining of user data) instead of a script.
More information can be found here and here.
This question already has answers here:
Aws Ec2 run script program at startup
(5 answers)
Closed 1 year ago.
I am trying to run a python script on ec2 instance . The python file is residing on s3.
I am able to run manually from ec2 instance using iam role which allow access to s3 folder and files.
The question is , how can i automate the start and stop of ec2 instance whenever needed and how to invoke /pass a python file to run upon starting the ec2 instance and stop the instance once the python files completes the execution.
Thanks,
Nikhil
Your requirements seem to be:
Schedule an Amazon EC2 instance to start at a specific time every day
The instance should run a Python script after starting
When the Python script has finished running, Stop the instance
Start EC2 instance on a schedule
You can use Amazon EventBridge to trigger an AWS Lambda function on a schedule.
You can code the Lambda function to call StartInstances() on the EC2 instance to Start it.
Run a script on startup
Install a script into the /var/lib/cloud/scripts/per-boot/ directory. This script can download the Python program from S3 and then run it.
When the EC2 instance starts up, it will automatically run any script in that directory.
Stop the instance when the script is finished
At the end of the script, add the command:
shutdown -h now
This will turn off the instance and place it in the Stopped state.
(This assume that the script is running as root. If it is running as another user, it will need to use sudo shutdown -h now.)
EC2 instances use cloudinit which you can customize to run a given script on each boot. You can use use regular os tools from python to shutdown your instance (e.g. shutdown -h now).
Here another alternative could be to use lambda function instead of EC2 instance to run the python script if maximum execution time of script is less than 15 minutes. Go serverless with AWS lambda rather than EC2. just add your script code in AWS lambda and schedule lambda function from AWS event bridge to invoke it.
I want to run a shell script just before EC2 terminates or stops, i am using Amazon2 AMI. There is no support for Traditional SysVinit in Amazon2 Image, it relies on Daemon services . So i want to make use of systemD services.
My shell script will just gracefully shutdown the tomcat and make some DB changes, should not get executed for more than 2m.
Please suggest.
P.S - I do not want to use LifeCycle Hooks.
Unfortunately by the time you have stopped/terminated the EC2 instance it has entered a shutting down state which would stop this script from running.
If you're hesitant to trigger a script via lifecycle hooks then you will need to script this action.
Look at using Systems Manager Run Command programatically.
Add to the script the terminate or shutdown logic.
I am creating a AWS EC2 launch template that includes commands within the User Data field to perform actions when the instance is first launched (package updates, install software, format EBS volumes, etc). In addition to this I also want to perform tasks on reboot or subsequent starting of the instance, such as mounting existing EBS volumes and configuring and mounting volatile SSD volumes. I see that I can use MIME-type to have code run when instance restarts here:
https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/
So, I can clearly modify User Data after I initially launch the instance, but this is cumbersome as it likely needs manual intervention or requires waiting for the instance to have executed the initial User Data code that runs on initialization of the instance.
My question is:
Can the multi-part MIME format be configured to run code that will execute on initialization of the instance and other code that will run every time the instance restarts?
I understand that what you're trying to achieve is passing two sets of commands using Userdata. One set which will be executed on the instance creation and another set which should be executed every reboot. Please lemme know if I misunderstood it.
For the first part, you can use Userdata itself as you already know. For the commands that should run on every reboot, you can leverage rc.local .
The script /etc/rc.local is for use by the system administrator. It is traditionally executed after all the normal system services are started, at the end of the process of switching to a multiuser runlevel, etc. You might use it to start a custom service or for mounting additional Volumes.
To write into /etc/rc.local , you can download the command set from S3 and copy into the file or you can simply echo it. Example:
echo 'echo HelloWorld' >> /etc/rc.local
Hope this helps.
I can see a generic way of starting an EC2 from lambda in Start and Stop Instances at Scheduled Intervals Using Lambda and CloudWatch.
Suppose I use that method to start an EC2, and suppose the AMI is a windows server 2019 customised to have a .bat file on the desktop, and also suppose I'm using a python lambda.
How can I execute this batch file from the lambda? (i.e. just as though someone had RDP'd into the instance and double-clicked on it)
Note: To be very clear, basically I want to start the EC2 using the method given in the AWS docs (above), and right after the instance has started, to run the batch file that will be sitting on the instance's desktop
I think you have a few concepts mixed together.
AWS Lambda functions run on the Lambda service, without having to use Amazon EC2 instances. This is what makes them "serverless".
If you have a batch file on an Amazon EC2 instance, you would presumably want to run that batch file on the EC2 instance itself, without involving Lambda (since you have got a server).
If you wish to run a script on an EC2 instance when it launches for the first time, you can provide a PowerShell or Command-Line script via the User Data field. Software on the AMI will automatically execute this script the first time that the instance starts.
This script could do all the work itself, or it could simply call another script that is stored on the disk. Some people use the script to download another script from a repository (eg Amazon S3 or GitHub) and then execute the downloaded script.
For more information, see: Running Commands on Your Windows Instance at Launch - Amazon Elastic Compute Cloud
If the Amazon EC2 instance is already running and you wish to trigger a script to execute, you can use the AWS Systems Manager Run Command. This works by having an agent on the instance which can be remotely triggered, thereby running scripts without having to login to the instance.