Repository navigation
Expand file tree
/
Copy pathmodules.py
More file actions
51 lines (39 loc) · 1.63 KB
/
Copy pathmodules.py
File metadata and controls
51 lines (39 loc) · 1.63 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
import logging
from typing import List
import boto3
from model import Model
logger = logging.getLogger()
def create_instance(model: Model, key_names: List[str]) -> List[str]:
"""Create ec2 instances based on supplied parameters.
Args:
model (Model): pydantic model used for supplying parameters to the function.
key_names (List[str]): name of private keys supplied.
Returns:
List[str]: List of created ec2 instances.
"""
logger.info("Creating instance...")
try:
instance_ids = []
with open("./docker_install.sh", "r") as file:
userdata = file.read()
ec2_res = boto3.client(
service_name="ec2",
aws_access_key_id=model.aws_access_key_id,
aws_secret_access_key=model.aws_secret_access_key,
region_name=model.region_name,
)
for private_key in key_names:
new_instance = ec2_res.run_instances(
ImageId=model.ami_id,
MinCount=model.num_instances,
MaxCount=model.num_instances,
UserData=userdata,
InstanceType=model.instance_type,
KeyName=private_key,
# TagSpecifications=[{'ResourceType': 'instance','Tags': [{'Key': 'Name','Value': instance_name}]}], # For adding instance name
)
logger.info("Instance created with ID: " + str(new_instance["Instances"][0]["InstanceId"]))
instance_ids.append(new_instance["Instances"][0]["InstanceId"])
return instance_ids
except Exception as e:
logger.error("Failed creating the instance: " + str(e))