Skip to content

Spot Instance scheduling

0

i want to run a task (1 hour) everyday morning. task is a python script. it will access the objects in S3 and extract ome details. i want to use spot ec2 also after successsful completion of task i want to stop/termiante the instance and start next day ina automated way

Give a complete solution like how can i configure spot instance scheduler, and others. I want the task to be completed if the spot is terminated next instance should start automatically and work. task is stateless. upon completion ofthe task instance shoul terminate and next day it should start in schedule

asked 20 days ago64 views
3 Answers
0
Accepted Answer

You want to run a daily Python script on a Spot EC2 instance, auto-start it in the morning, and shut it down after it's done. Here's how: Upload your script to S3. Create a Launch Template for a Spot instance that: Pulls the script from S3 using a User Data script. Runs it. Terminates itself after finishing. Set up an Auto Scaling Group (ASG) with: Desired capacity = 0 by default. A scheduled action (via EventBridge) to scale to 1 every morning. ASG will launch a Spot instance daily → script runs → instance terminates. Since your task is stateless, even if Spot is interrupted, ASG can retry.

Let me know if you want the exact scripts or Terraform for this.

answered 20 days ago
AWS
EXPERT
reviewed 20 days ago
  • Yeah can you please share the terraform code also @Sambhav Mittal

0

For your daily task automation with spot instances, here's a comprehensive solution:

  1. Setting up a scheduler: You have several options to schedule your daily task:
  • EventBridge Scheduler: Create a recurring schedule using a cron expression to run every morning at your desired time. This gives you flexibility with scheduling options using either cron-based schedules (specific times) or rate-based schedules (regular intervals).

  • AWS Systems Manager's Quick Setup: This allows you to specify tag keys and values for instances, set schedule options including time zone, days, and times for starting instances.

  • AWS Instance Scheduler: This solution provides more flexibility for complex scheduling needs and can be deployed via CloudFormation.

  1. Configuring spot instances:
  • Create an AMI with your Python script and all necessary dependencies
  • Ensure the instance has the proper IAM role to access S3
  • Configure your Python script to run at startup via user data
  • Include shutdown commands at the end of your script (e.g., sudo shutdown -h now)
  • When using EventBridge Scheduler, you can configure a flexible time window which helps optimize costs with spot instances
  1. Handling spot instance termination: If your spot instance is terminated by AWS in the middle of your task:
  • Your task will be interrupted and incomplete
  • You'll need to implement checkpointing in your Python script to track progress
  • Consider saving intermediate results to S3 so the next instance can resume where the previous one left off
  1. Automatic continuation: By default, if a spot instance is terminated, a new one won't automatically start and continue the task. You'll need to:
  • Implement logic in your script to check for incomplete tasks from previous runs
  • Store task state in a persistent storage like S3 or DynamoDB
  • Have your script detect and resume incomplete work
  • Configure your scheduler to start a new instance the next day regardless of previous day's outcome

Complete solution approach:

  1. Create an AMI with your Python script and dependencies
  2. Set up your script to:
  • Run automatically on instance startup
  • Check for and resume incomplete work
  • Save progress periodically to S3
  • Shut down the instance upon completion
  1. Configure EventBridge Scheduler to start your spot instance daily
  2. Ensure proper IAM permissions for S3 access and instance management
  3. Implement logging to CloudWatch for monitoring task execution
  4. Consider setting up SNS notifications for task failures

This serverless approach to scheduling will ensure your task runs daily, processes the S3 objects, and then terminates the instance automatically to minimize costs.
Sources
Scheduled task automation | AWS re:Post
Stop and start EC2 instances automatically on a schedule using Quick Setup - AWS Systems Manager
How to schedule automatic instance start and stop (QuickSetup) | AWS re:Post

answered 20 days ago
AWS
EXPERT
reviewed 20 days ago
0

I would do this a different way.

  • Run a ECS Task instead of EC2.
  • Schedule ECS task to run daily on Spot on Fargate
  • As its DOCKER, updating the image and pre-requisites should be easier
EXPERT
answered 19 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.