Skip to content

Commit 23d89ec

Browse files
committed
launches instances using ec2.create_fleet but not finished
1 parent f150def commit 23d89ec

File tree

2 files changed

+74
-10
lines changed

2 files changed

+74
-10
lines changed

fleet.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ImageId = "ami-fad25980"
2+
KeyName = "your_key_file_name"
3+
SnapshotId = "snap-04007a196c0f3f398"
4+
SubnetId = "subnet-WWWWWWWW"
5+
SecurityGroup= "sg-ZZZZZZZZZ"

run.py

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
CLEAN_DASHBOARD = 'False'
1414

1515
from config import *
16+
from fleet import *
1617

1718
WAIT_TIME = 60
1819
MONITOR_TIME = 60
@@ -557,26 +558,84 @@ def startCluster():
557558
print('Use: run.py startCluster configFile')
558559
sys.exit()
559560

560-
thistime = datetime.datetime.now().replace(microsecond=0)
561-
#Step 1: set up the configuration files
561+
#Step 1: Configure the Spot Fleet Request
562+
ec2client=boto3.client('ec2')
562563
s3client = boto3.client('s3')
564+
iam=boto3.client('iam')
565+
563566
ecsConfigFile=generateECSconfig(ECS_CLUSTER,APP_NAME,AWS_BUCKET,s3client)
564567
spotfleetConfig=loadConfig(sys.argv[2])
565-
spotfleetConfig['ValidFrom']=thistime
566-
spotfleetConfig['ValidUntil']=(thistime+datetime.timedelta(days=365)).replace(microsecond=0)
567-
spotfleetConfig['TargetCapacity']= CLUSTER_MACHINES
568-
spotfleetConfig['SpotPrice'] = '%.2f' %MACHINE_PRICE
568+
569+
# Still need to figure out how these fit in
570+
"""
569571
DOCKER_BASE_SIZE = int(round(float(EBS_VOL_SIZE)/int(TASKS_PER_MACHINE))) - 2
570572
userData=generateUserData(ecsConfigFile,DOCKER_BASE_SIZE)
571573
for LaunchSpecification in range(0,len(spotfleetConfig['LaunchSpecifications'])):
572574
spotfleetConfig['LaunchSpecifications'][LaunchSpecification]["UserData"]=userData
573575
spotfleetConfig['LaunchSpecifications'][LaunchSpecification]['BlockDeviceMappings'][1]['Ebs']["VolumeSize"]= EBS_VOL_SIZE
574-
spotfleetConfig['LaunchSpecifications'][LaunchSpecification]['InstanceType'] = MACHINE_TYPE[LaunchSpecification]
576+
"""
575577

578+
ecsInstanceArn = iam.get_role(RoleName='ecsInstanceRole')['Role']['Arn']
579+
fleetRoleArn = iam.get_role(RoleName='aws-ec2-spot-fleet-tagging-role')['Role']['Arn']
576580

577-
# Step 2: make the spot fleet request
578-
ec2client=boto3.client('ec2')
579-
requestInfo = ec2client.request_spot_fleet(SpotFleetRequestConfig=spotfleetConfig)
581+
LaunchTemplateData={"ImageId": ImageId,
582+
"KeyName": KeyName,
583+
"IamInstanceProfile": {"Arn": ecsInstanceArn},
584+
"BlockDeviceMappings": [
585+
{
586+
"DeviceName": "/dev/xvda",
587+
"Ebs": {
588+
"DeleteOnTermination": True,
589+
"VolumeType": "gp2",
590+
"VolumeSize": 8,
591+
"SnapshotId": SnapshotId
592+
}
593+
},
594+
{
595+
"DeviceName": "/dev/xvdcz",
596+
"Ebs": {
597+
"DeleteOnTermination": True,
598+
"VolumeType": "gp2"
599+
}
600+
}
601+
],
602+
"NetworkInterfaces": [
603+
{
604+
"DeviceIndex": 0,
605+
"SubnetId": SubnetId,
606+
"DeleteOnTermination": True,
607+
"AssociatePublicIpAddress": True,
608+
"Groups": [SecurityGroup]
609+
}
610+
]
611+
}
612+
613+
TemplateName=f'{APP_NAME}_LaunchTemplate'
614+
try:
615+
launch_template = ec2client.describe_launch_templates(LaunchTemplateNames=[TemplateName])['LaunchTemplates'][0]
616+
except ec2client.exceptions.ClientError:
617+
launch_template = ec2client.create_launch_template(LaunchTemplateName=TemplateName,LaunchTemplateData=LaunchTemplateData)['LaunchTemplate']
618+
619+
SpotOptions = {"AllocationStrategy": "lowestPrice"}
620+
LaunchTemplateConfigs=[{'LaunchTemplateSpecification':{'LaunchTemplateId':launch_template['LaunchTemplateId'],
621+
'Version':'$Latest'},
622+
'Overrides':[{'InstanceType':MACHINE_TYPE,
623+
'MaxPrice':'%.2f' %MACHINE_PRICE,
624+
'SubnetId':SubnetId}]}]
625+
TargetCapacitySpecification={'TotalTargetCapacity':CLUSTER_MACHINES,
626+
'OnDemandTargetCapacity':0,
627+
'DefaultTargetCapacityType':'spot'}
628+
629+
# Step 2: Make the spot fleet request
630+
thistime = datetime.datetime.now().replace(microsecond=0)
631+
requestInfo = ec2client.create_fleet(DryRun=False,
632+
SpotOptions=SpotOptions,
633+
LaunchTemplateConfigs=LaunchTemplateConfigs,
634+
TargetCapacitySpecification=TargetCapacitySpecification,
635+
TerminateInstancesWithExpiration=True,
636+
Type="maintain",
637+
ValidFrom=thistime,
638+
ValidUntil=(thistime+datetime.timedelta(days=365)).replace(microsecond=0),)
580639
print('Request in process. Wait until your machines are available in the cluster.')
581640
print('SpotFleetRequestId',requestInfo['SpotFleetRequestId'])
582641

0 commit comments

Comments
 (0)