-
Notifications
You must be signed in to change notification settings - Fork 7
/
spinup.py
executable file
·63 lines (50 loc) · 1.56 KB
/
spinup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
import argparse
import boto3
import os
import signal
import subprocess
import time
parser = argparse.ArgumentParser(description='Spin up an EC2 OpenVPN instance.')
parser.add_argument('--ami', help="AMI Image", default="ami-c9dc4fba")
parser.add_argument('--sg', help="Security Group ID", default="sg-cb0669af")
parser.add_argument('--port', help="OpenVPN Port", default=443)
args = parser.parse_args()
client = boto3.client('ec2')
response = client.run_instances(
DryRun=False,
ImageId=args.ami,
MinCount=1,
MaxCount=1,
SecurityGroupIds=[args.sg],
InstanceType='t2.nano',
Placement={
'AvailabilityZone': 'eu-west-1a',
},
Monitoring={
'Enabled': False
},
InstanceInitiatedShutdownBehavior='terminate',
)
instance_id = response['Instances'][0]['InstanceId']
print("Starting instance: {0}".format(instance_id))
time.sleep(2) # Sleep to wait for a public IP
response = client.describe_instances(
InstanceIds=[
instance_id
]
)
public_ip = response['Reservations'][0]['Instances'][0]['PublicIpAddress']
print("Public IP for {0} is {1}".format(instance_id, public_ip))
def kill_instance(*args, **kwargs):
kill_ret = client.terminate_instances(
InstanceIds=[
instance_id
]
)
print("Instance {0} terminated.".format(instance_id))
signal.signal(signal.SIGTERM, kill_instance)
try:
subprocess.call(['zsh', '{0}/openvpn.zsh'.format(os.path.dirname(os.path.realpath(__file__))), public_ip, str(args.port)])
except KeyboardInterrupt:
kill_instance