Code Monkey home page Code Monkey logo

run-commands-on-floating-ec2's Introduction

Run commands on Floating EC2 instances

MIT licensed

This project allows you to run remote code on Floating EC2 instances.

The code will:

  • Create an EC2 instance based on the AMI ID you provide.
  • Run the command/script you provide via SSM.
  • Delete the EC2 instance once the job is done.

This process could be used to schedule heavy tasks execution that cannot be containerized on Windows/Linux EC2 instances (like rendering and enormos builds) on AWS.

Prerequisites

  • python3
  • pip
  • virtualenv
  • AWS-CLI
  • AWS Access Keys with the privilges to:
    • Create/destroy EC2 instances
    • SSM get/list/run commands
  • IAM instance-profile with the "AmazonSSMManagedInstanceCore" policy attached to it
  • S3 bucket to store the job logs

Installation

  • Clone the project and create/activate virtualenv.
  • Install needed libraries.
# On Windows
cd PROJECT_PATH
virtualenv .env # Create virtualenv - only the first time
.\.env\Scripts\activate # Activate virtualenv
pip install -r requirements.txt # install required packages
# On Linux
cd PROJECT_PATH
virtualenv .env # Create virtualenv - only the first time
. .env/bin/activate # Activate virtualenv
python3 -m pip install -r requirements.txt # install required packages

Load AWS credentials

# On Windows
aws configure --profile "PROFILE_NAME"
$env:AWS_PROFILE="PROFILE_NAME"
# Or
$env:AWS_DEFAULT_REGION="ca-central-1"
$env:AWS_ACCESS_KEY_ID="XXX-XXX-XXX-XXX"
$env:AWS_SECRET_ACCESS_KEY="XXXXXXXXXXXXXXX"
aws configure --profile "PROFILE_NAME"
export AWS_PROFILE="PROFILE_NAME"
# Or
export AWS_DEFAULT_REGION="ca-central-1"
export AWS_ACCESS_KEY_ID="XXX-XXX-XXX-XXX"
export AWS_SECRET_ACCESS_KEY="XXXXXXXXXXXXXXX"

Configuration

The service configuration is stored in the config/settings.yml file.
Use the config/settings.yml.sample as a base.

How to run

How to use on Windows example:

python .\handler.py -a "ami-XXXXXX" -p "YOUR_PROJECT_NAME" -c @"
`$ErrorActionPreference = 'Stop'
git clone https://github.com/taherbs/scriptbox.git c:/scriptbox
if (-not `$?) { throw \"clone command failed\"}
cd "c:/scriptbox"
./run_script.ps1
"@

How to use on Linux example:

COMMAND_EXEC=$(cat <<-END
\$ErrorActionPreference = 'Stop'
git clone https://github.com/taherbs/scriptbox.git c:/scriptbox
if (-not \$?) { throw "clone command failed"}
cd "c:/scriptbox"
./run_script.ps1
END)
python ./handler.py -a "test" -p "rendering-farm-foxtrot" -c "$COMMAND_EXEC"

TO_DO

  • Change the instance type to spot to optimize cost
  • Better logging when error

run-commands-on-floating-ec2's People

Contributors

taherbs avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.