-1

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

4
  • Might help: Auto-Stop EC2 instances when they finish a task - DEV Community 👩‍💻👨‍💻 Commented Nov 23, 2021 at 3:24
  • @Sidh What would be the trigger that 'starts' the instance? Would it always run the same Python script, or would it need to copy it from S3 each time the instance starts? Commented Nov 23, 2021 at 3:25
  • it would be the same python script. the requirement is to host a python file on aws and run it as a scheduled job at a specific time everyday. As of now i am able to fetch the python file residing on s3 and was able to execute it on top of ec2 manually. Commented Nov 23, 2021 at 3:48
  • How long does the script take to run? Have you considered using AWS Lamba to run the script? (Note: It Lambda functions can only run for a maximum of 15 minutes). Commented Nov 23, 2021 at 12:09

3 Answers 3

1

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

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

Comments

0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.