There will be at times you will like to start and run a task at one of your EC2 instances, however you will not prefer to have it running all the time. Most likely reason behind could be the aws costs. In general, you could save around 70% of the aws costs by effectively maintaining the run -time of your EC2 instances.
Here I will show you a simple Python script using Boto3 to control the state of your EC2 instances. Using this script, you will do the following steps in order:
- Initiate your EC2 instance if its not found in "running" state.
- Connect to your EC2 instance using the paramiko module.
- Run your tasks.
- Stop your EC2 instance.
In order to achieve the following you will need two more files in addition to the code displayed here:
- aws_config.ini : In this file you need to specify your AWS credentials as well as other information regarding the instance and your ssh-access key path.
- task.sh: A simple text (or shell script) file where you need to specify a list of shell commands which the instance will execute.
Following you will find the main script which uses the above mentioned files to start your instance and then run your tasks mentioned in the task.sh file and then finally stop your instance.
from botocore.exceptions import ClientError
def __init__(self, config_file):
self.config = configparser.ConfigParser(allow_no_value=True)
ec2 = boto3.resource(
# Hard coded strings as credentials, not recommended.
instance_id = self.config["aws"]["INSTANCE_ID"]
self.instance = ec2.Instance(id=instance_id) # instance id
if self.instance.state["Name"] != "running":
# DRY RUN to verify the permissions.
except ClientError as e:
if 'DryRunOperation' not in str(e):
# On DRY RUN success, initiate the instance
print("Starting the instance.")
print("Waiting for status checks ..")
def run_task(self, task_file):
aws_key = paramiko.RSAKey.from_private_key_file(self.config["aws"]["KEY_PATH"]) # your private key for ssh
ssh_client = paramiko.SSHClient()
print("Connecting to shell using ssh")
print("Executing commands on EC2 instance")
task_file = open(task_file, "r")
tasks = task_file.read()
stdin, stdout, stderr = ssh_client.exec_command(tasks)
for line in iter(lambda: stdout.readline(2048), ""):
print("All tasks have been executed.")
print("Stopping the instance.")
ec2_instance = Ec2Instance(config_file="aws_config.ini")
Hopefully this code could serve your pupose and most improtantly reduce your AWS costs.
More details and even easier approach can also be found in my GitHub repository (i.e. by just using
pip install ec2-task)