Last active
March 16, 2018 01:43
-
-
Save zulrang/5662b6bcaf5a711069de70b290aa6c56 to your computer and use it in GitHub Desktop.
"Cloud9" with local editor, terminal, and file system
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 = 'C:/Tools/sshfs/sshfs.exe' | |
PRIVATE_KEY_FILE = 'C:/Certs/aws.pem' | |
DRIVE_LETTER = 'n' | |
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, '-d', DRIVE_LETTER, | |
'-r', '/home/ec2-user', '-h', host, '-u', 'ec2-user', '-k', | |
PRIVATE_KEY_FILE], | |
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