Home > Enterprise >  ec2instance automation with python script
ec2instance automation with python script

Time:11-24

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

CodePudding user response:

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).

CodePudding user response:

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.)

CodePudding user response:

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.

  • Related