Created
October 29, 2018 17:29
-
-
Save chrisking/376136b9d9dbe1c2d0db16520373fa01 to your computer and use it in GitHub Desktop.
Fabric Config for Starting / Stopping EC2 Instances with SSH and Jupyter Access
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
| # import fabrics API functions | |
| from fabric.api import * | |
| from requests import get | |
| # Update with your security group and instance ID | |
| SECURITY_GROUP = "sg-9bdc1fd0" | |
| INSTANCE_ID = "i-06c072747655b16e5" | |
| def modify_security_group(enable=False): | |
| ip = get('https://api.ipify.org').text | |
| CIDR = ip + "/32" | |
| if enable: | |
| aws_command = "aws ec2 authorize-security-group-ingress --group-id " + SECURITY_GROUP | |
| else: | |
| aws_command = "aws ec2 revoke-security-group-ingress --group-id " + SECURITY_GROUP | |
| aws_command += " --protocol tcp --port 22 --cidr " + CIDR | |
| local(aws_command) | |
| if enable: | |
| aws_command = "aws ec2 authorize-security-group-ingress --group-id " + SECURITY_GROUP | |
| else: | |
| aws_command = "aws ec2 revoke-security-group-ingress --group-id " + SECURITY_GROUP | |
| aws_command += " --protocol tcp --port 8888 --cidr " + CIDR | |
| local(aws_command) | |
| def enable_access(): | |
| modify_security_group(enable=True) | |
| def disable_access(): | |
| modify_security_group(enable=False) | |
| def start_work(): | |
| local("aws ec2 start-instances --instance-ids " + INSTANCE_ID) | |
| enable_access() | |
| def stop_work(): | |
| local("aws ec2 stop-instances --instance-ids " + INSTANCE_ID) | |
| disable_access() | |
| def get_status(): | |
| local("aws ec2 describe-instances --instance-ids " + INSTANCE_ID) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment