forked from ttlequals0/autovpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_ec2.py
executable file
·66 lines (53 loc) · 1.63 KB
/
create_ec2.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
64
65
66
import time
import boto
import boto.ec2
import sys
keyname=sys.argv[1]
instance_type=sys.argv[2]
region=sys.argv[3]
ami=sys.argv[4]
if region:
conn_region = boto.ec2.connect_to_region(region)
else:
conn_region = boto.connect_ec2()
def auto_vpn(ami=ami,
instance_type=instance_type,
key_name=keyname,
group_name="vpn_2",
ssh_port="22",
vpn_port="1194",
cidr="0.0.0.0/0",
tag="auto_vpn",
user_data=None):
ec2 = conn_region
try:
group = ec2.get_all_security_groups(groupnames=[group_name])[0]
except ec2.ResponseError, e:
if e.code == 'InvalidGroup.NotFound':
group = ec2.create_security_group(group_name,
'A group that allows VPN access')
else:
raise
try:
group.authorize('tcp',ssh_port,ssh_port,cidr)
group.authorize('udp',vpn_port,vpn_port,cidr)
except ec2.ResponseError, e:
if e.code == 'InvalidPermission.Duplicate':
"""print ('Security group %s already exists') % group_name"""
else:
raise
reservation = ec2.run_instances(ami,
key_name=key_name,
security_groups=[group_name],
instance_type=instance_type,
user_data=user_data)
instance = reservation.instances[0]
while instance.state != 'running':
time.sleep(30)
instance.update()
instance.add_tag(tag)
global host
host = instance.ip_address
print "%s" % host
if __name__ == "__main__":
auto_vpn()