-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2_user_data.py
executable file
·57 lines (45 loc) · 1.46 KB
/
ec2_user_data.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
#!/usr/bin/env python
import boto3
import botocore.exceptions
ec2 = boto3.resource('ec2')
# allow HTTP access
try:
security_group = ec2.create_security_group(GroupName='http_access',
Description='pug talk')
security_group.authorize_ingress(IpProtocol='tcp',
CidrIp='0.0.0.0/0',
FromPort=80,
ToPort=80)
except botocore.exceptions.ClientError:
pass
# user-data script to install web server
user_data="""#!/bin/bash
# install web server
yum install httpd -y
echo "hello world!" > /var/www/html/index.html
service httpd start
"""
# launch an instance with
instances = ec2.create_instances(ImageId='ami-11032472',
InstanceType='t2.micro',
MinCount=1,
MaxCount=1,
SecurityGroupIds=['http_access'],
UserData=user_data)
print('launching')
instances[0].wait_until_running()
print('running (and installing web server)')
ip_address = ec2.Instance(instances[0].id).public_ip_address
import time
import socket
while True:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
try:
s.connect((ip_address, 80))
break
except:
pass
s.close()
time.sleep(1)
print('type: open http://' + ip_address)