Skip to content

Instantly share code, notes, and snippets.

@hassaananjum
Last active March 7, 2018 06:50
Show Gist options
  • Save hassaananjum/be4a4bb4024b4ec8dbd309e4cab508e7 to your computer and use it in GitHub Desktop.
Save hassaananjum/be4a4bb4024b4ec8dbd309e4cab508e7 to your computer and use it in GitHub Desktop.
# Simple script to start/stop Amazon EC2 instances
import boto3
import sys
region = 'us-east-1'
instances = ['X-XXXXXXXX']
aws_id = "XXXXXXXXXXXXXXXXXXXX"
aws_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
def start_instance():
ec2 = boto3.client('ec2',
aws_access_key_id = aws_id,
aws_secret_access_key = aws_secret
)
print 'starting instance '
ec2.start_instances(InstanceIds=instances)
ec2resource = boto3.resource('ec2')
instance = ec2resource.Instance(instances[0])
instance.wait_until_running()
instance.load()
print 'started your instance: ' + str(instances)
print(instance.public_dns_name)
def stop_instance():
ec2 = boto3.client('ec2',
aws_access_key_id = aws_id,
aws_secret_access_key = aws_secret,
region_name = region
)
ec2.stop_instances(InstanceIds=instances)
print 'stopped your instance: ' + str(instances)
if sys.argv[1] == 'start':
start_instance()
elif sys.argv[1] == 'stop':
stop_instance()
else:
print('Invalid Command')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment