Skip to content

Instantly share code, notes, and snippets.

@ispyhumanfly
Created December 20, 2019 00:13
Show Gist options
  • Save ispyhumanfly/01e1383cd70f4d79e57ee322f3ae7f08 to your computer and use it in GitHub Desktop.
Save ispyhumanfly/01e1383cd70f4d79e57ee322f3ae7f08 to your computer and use it in GitHub Desktop.
import boto3 as aws
import logging
import time
import os
import paramiko
from botocore.exceptions import ClientError
def main():
ec2 = aws.client('ec2')
#Create section
test_script_00 = """#!/bin/bash
sudo apt-get update
sudo apt-get install -y python3.6
sudo apt-get install -y python-pip
pip install pandas
pip install numpy
pip install -U scikit-learn
"""
#This creates and instance in AWS
try:
response = ec2.run_instances(ImageId="ami-011664c11960a591b",InstanceType="t2.micro",KeyName="default",UserData=test_script_00,MinCount=1,MaxCount=1)
except ClientError as e:
logging.error(e)
return None
#Deploy
#
ourInst = ""
time.sleep(10)
response_2 = ec2.describe_instances()
for reservation in response_2["Reservations"]:
for instance in reservation["Instances"]:
# This sample print will output entire Dictionary object
#print(instance)
# This will print will output the value of the Dictionary key 'InstanceId'
#print(instance["InstanceId"])
if instance["State"]["Name"]!= "terminated":
ourInst = instance
print(ourInst)
# instance = aws.resource("ec2").Instance(id=ourInst["InstanceId"])
# instance.wait_until_running()
waiter = ec2.get_waiter("instance_status_ok")
waiter.wait(InstanceIds=[ourInst["InstanceId"]])
#
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ourInst["PublicIpAddress"], username="ubuntu", key_filename="default.pem")
#client.connect("3.134.97.152", username="ubuntu", key_filename="default.pem")
#Setup sftp connection and transmit this script
print("copying")
sftp = client.open_sftp()
sftp.put("Calc.py", "/tmp/Calc.py")
sftp.put("adult.data", "/tmp/adult.data")
sftp.put("new.data", "/tmp/new.data")
sftp.close()
stdin,stdout,stderr= client.exec_command("ls /tmp")
print(stdout.readlines())
stdin,stdout,stderr= client.exec_command("python /tmp/Calc.py --adultData /tmp/adult.data --dataset /tmp/new.data")
for line in stdout.readlines():
print(line)
ids = [ourInst["InstanceId"]]
ec2_1 = aws.resource('ec2')
ec2_1.instances.filter(InstanceIds=ids).stop()
ec2_1.instances.filter(InstanceIds=ids).terminate()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment