Last active
March 16, 2018 01:46
-
-
Save zulrang/d9cca8f06de02cb5b5a214cac5cd55f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python.exe -u | |
import subprocess | |
import boto3 | |
# constants | |
INSTANCE_NAME = 'Development' | |
SSHFS_CMD = '/usr/bin/sshfs' | |
MOUNT_POINT = '/awsec2' | |
ec2 = boto3.resource('ec2') | |
# get first instance by name | |
instance = [x for x in ec2.instances.filter(Filters=[{'Name': 'tag:Name', | |
'Values': [INSTANCE_NAME]}])][0] | |
print("Instance {} ({}) current state: {}".format(INSTANCE_NAME, | |
instance.id, | |
instance.state['Name'])) | |
# ensure instance is running | |
if instance.state['Name'] == 'stopped': | |
# start instance | |
print('Starting instance') | |
instance.start() | |
instance.wait_until_running() | |
print('Instance started') | |
host = instance.public_dns_name | |
print('Public DNS name: ', host) | |
# mount local sshfs | |
subprocess.Popen([SSHFS_CMD, "ec2-user@{}:/home/ec2-user".format(host), MOUNT_POINT], | |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) | |
# start ssh | |
subprocess.call('ssh %s' % host, shell=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment