Skip to content

Commit 735ac22

Browse files
committed
launches instances using ec2.create_fleet but not finished
1 parent 787f005 commit 735ac22

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
@@ -14,6 +14,7 @@
1414
AUTO_MONITOR = 'False'
1515

1616
from config import *
17+
from fleet import *
1718

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

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

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

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

0 commit comments

Comments
 (0)