def ec2create_Cloudwatch(): import boto3 from datetime import datetime, timedelta from ec2list_instances import ec2list_Instances cloudwatch = boto3.resource('cloudwatch') ec2 = boto3.resource('ec2') ec2list_Instances() instid = input("Please enter instance ID: ") instance = ec2.Instance(instid) instance.monitor() metric_iterator = cloudwatch.metrics.filter(Namespace='AWS/EC2', MetricName='CPUUtilization', Dimensions=[{ 'Name': 'InstanceId', 'Value': instid }]) metric = list(metric_iterator)[0] response = metric.get_statistics(StartTime=datetime.utcnow() - timedelta(minutes=5), EndTime=datetime.utcnow(), Period=300, Statistics=['Average']) print("Average CPU utilisation:", response['Datapoints'][0]['Average'], response['Datapoints'][0]['Unit'])
def addImgtoEc2(): from ec2list_instances import ec2list_Instances from S3list_bucket import S3list_Bucket from addImgtoec2 import addMetaData ec2 = boto3.resource('ec2') s3 = boto3.resource('s3') print("running instances: ") print("") ec2list_Instances() ids = input("Enter Id for the instance for the server: ") for instance in ec2.instances.all(): if instance.id == ids: try: ids = (instance.id) public_ip_address = instance.public_ip_address S3list_Bucket() bucket_name = input( "Enter the destiontion bucket name for the img: ") object_name = input( "Enter the name of the file you'd like to show: ") addMetaData(bucket_name, public_ip_address, object_name) except Exception as error: print(error)
def ec2delete_Instance(): import sys import boto3 from ec2list_instances import ec2list_Instances #importing list instance so you dont have to search on aws ec2 = boto3.resource('ec2') ec2list_Instances() print("please Enter the Instance you'd like to delete: ") ids = (input(), ) ec2.instances.filter(InstanceIds=ids).terminate() print("instance was deleted")
def ec2SSH(): import sys import boto3 import subprocess from ec2list_instances import ec2list_Instances ec2 = boto3.resource('ec2') print("yes") ec2list_Instances() print("This is remote instance access") print("please enter the Id of the Instance youd like to remotley access: ") ids = input() #adding in user input print("your password from your laptop") for instance in ec2.instances.all(): if instance.id == ids: ids = (instance.id) dns = instance.public_dns_name ssh_cmd = 'sudo ssh -i Douglas_key.pem ec2-user@' + dns #ssh with sudo to be able to use ssh and add an image onto my instance subprocess.run(ssh_cmd, shell=True)
def menu(): import boto3 import sys import subprocess import ec2create_instance import ec2list_instances import S3create_bucket import S3put_bucket import S3list_bucket import ec2create_cloudwatch import S3delete_bucket import ec2delete_instance import ec2list_instances import S3deletecontents_bucket import ec2ssh import addImgtoec2 import urladdpictureto s3 = boto3.resource('s3') ec2 = boto3.resource('ec2') menu_text = ''' 𝓜𝓪𝓲𝓷 𝓜𝓮𝓷𝓾 ''' print(menu_text) print("1. Launch a new Instance") print("2. List instances") print("3. Create S3 Bucket") print("4. add contents inside bucket") print("5. List Buckets") print("6. Create Cloudwatch") print("7. Delete Bucket") print("8. Delete Instance") print("9. Delete Contents Of The Bucket") print("10. SSH on to an Instance") print("11. Add a Url to Bucket") print("12. Add Image to Instance") print("0. Exit") print("Please select an option") choice = input() if choice == '1': print("") ec2create_instance.ec2create_Instance() if choice == '2': ec2list_instances.ec2list_Instances() if choice == '3': S3create_bucket.S3create_Bucket() if choice == '4': S3put_bucket.S3put_Bucket() if choice == '5': S3list_bucket.S3list_Bucket() if choice == '6': ec2create_cloudwatch.ec2create_Cloudwatch() if choice == '7': S3delete_bucket.S3Delete_Bucket() if choice == '8': ec2delete_instance.ec2delete_Instance() if choice == '9': S3deletecontents_bucket.S3deletecontents_Bucket() if choice == '10': ec2ssh.ec2SSH() if choice == '11': urladdpictureto.Urladdpictureto() if choice == '12': addImgtoec2.addImgtoEc2() if choice == '0': print("You have left ec2Instance maker") exit()