-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_aws.py
41 lines (30 loc) · 1 KB
/
create_aws.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
import os
import boto3
from pathlib import Path
def launch_app_server():
ec2 = boto3.resource('ec2')
USER_SCRIPT = Path('aws-setup.sh').read_text()
# create a new EC2 instance
instances = ec2.create_instances(
ImageId='ami-0653e888ec96eab9b',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
UserData=USER_SCRIPT,
KeyName='jack',
SecurityGroupIds=[os.getenv('SecurityGroupIds')]
)
instances[0].wait_until_running()
instances[0].reload() # from stack-overflow, ensure to get public ip
ip_address = instances[0].public_ip_address
print("Application Server launched at {}".format(ip_address))
return ip_address
def terminate_app_server(ip_address):
ec2 = boto3.resource('ec2')
filter_instances = ec2.instances.filter(Filters=[{
'Name': 'ip-address',
'Values': [ip_address]}])
for instance in filter_instances:
instance.terminate()
if __name__ == '__main__':
launch_app_server()