-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathec2deploy.py
More file actions
executable file
·78 lines (67 loc) · 2.38 KB
/
ec2deploy.py
File metadata and controls
executable file
·78 lines (67 loc) · 2.38 KB
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
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
import boto3
import argparse
import time
parser = argparse.ArgumentParser(description='')
parser.add_argument('bucket_name')
parser.add_argument('stack_name')
parser.add_argument('region')
parser.add_argument('enable_cors')
args = parser.parse_args()
#comment occurences of AWS_SESSION_TOKEN and session_token
#if you are not using an AWS Educate Account
session = boto3.Session()
access_key = session.get_credentials().access_key
secret_key = session.get_credentials().secret_key
session_token = session.get_credentials().token
ec2 = session.resource('ec2')
start_up_script="""#!/usr/bin/env bash
export AWS_ACCESS_KEY_ID='{access_key}'
export AWS_SECRET_ACCESS_KEY='{secret_key}'
export AWS_SESSION_TOKEN='{session_token}'
trap 'shutdown now' EXIT
set -x -e
apt update
apt install -y awscli docker.io zip
cd /home/ubuntu
git clone https://github.com/Aishwarya26l/lambda-nbconvert.git
cd lambda-nbconvert
./scripts/deploy.sh '{bucket_name}' '{stack_name}' '{region}' '{enable_cors}'
cat<<'END'
--------------------------------------------------------------------------------
Stack {stack_name} was successfully deployed
--------------------------------------------------------------------------------
END
sleep 10
""".format(access_key=access_key,
secret_key=secret_key,
session_token=session_token,
bucket_name=args.bucket_name,
stack_name=args.stack_name,
region=args.region,
enable_cors=args.enable_cors)
i = ec2.create_instances(ImageId='ami-0ac019f4fcb7cb7e6',
InstanceType='t2.micro',
UserData=start_up_script,
MinCount=1,
MaxCount=1,
InstanceInitiatedShutdownBehavior='terminate')[0]
print('Instance {} was started. Waiting for output ...'.format(i.instance_id))
prev_output=''
while True:
time.sleep(10)
o = i.console_output()
if 'Output' in o:
output = o['Output']
if output != prev_output:
print('...\n'*3)
print(output)
prev_output=output
if i.state['Name'] == 'terminated':
break
success_string='Stack {} was successfully deployed'.format(args.stack_name)
print(('#'*60+'\n')*3)
if success_string in prev_output:
print(success_string)
else:
print('There were some problems. See above output for more details')