How to use request_spot_fleet method in localstack

Best Python code snippet using localstack_python

test_spot_fleet.py

Source:test_spot_fleet.py Github

copy

Full Screen

...70@mock_ec271def test_create_spot_fleet_with_lowest_price():72 conn = boto3.client("ec2", region_name="us-west-2")73 subnet_id = get_subnet_id(conn)74 spot_fleet_res = conn.request_spot_fleet(75 SpotFleetRequestConfig=spot_config(subnet_id)76 )77 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]78 spot_fleet_requests = conn.describe_spot_fleet_requests(79 SpotFleetRequestIds=[spot_fleet_id]80 )["SpotFleetRequestConfigs"]81 len(spot_fleet_requests).should.equal(1)82 spot_fleet_request = spot_fleet_requests[0]83 spot_fleet_request["SpotFleetRequestState"].should.equal("active")84 spot_fleet_config = spot_fleet_request["SpotFleetRequestConfig"]85 spot_fleet_config["SpotPrice"].should.equal("0.12")86 spot_fleet_config["TargetCapacity"].should.equal(6)87 spot_fleet_config["IamFleetRole"].should.equal(88 "arn:aws:iam::{}:role/fleet".format(ACCOUNT_ID)89 )90 spot_fleet_config["AllocationStrategy"].should.equal("lowestPrice")91 spot_fleet_config["FulfilledCapacity"].should.equal(6.0)92 len(spot_fleet_config["LaunchSpecifications"]).should.equal(2)93 launch_spec = spot_fleet_config["LaunchSpecifications"][0]94 launch_spec["EbsOptimized"].should.equal(False)95 launch_spec["SecurityGroups"].should.equal([{"GroupId": "sg-123"}])96 launch_spec["IamInstanceProfile"].should.equal(97 {"Arn": "arn:aws:iam::{}:role/fleet".format(ACCOUNT_ID)}98 )99 launch_spec["ImageId"].should.equal(EXAMPLE_AMI_ID)100 launch_spec["InstanceType"].should.equal("t2.small")101 launch_spec["KeyName"].should.equal("my-key")102 launch_spec["Monitoring"].should.equal({"Enabled": True})103 launch_spec["SpotPrice"].should.equal("0.13")104 launch_spec["SubnetId"].should.equal(subnet_id)105 launch_spec["UserData"].should.equal("some user data")106 launch_spec["WeightedCapacity"].should.equal(2.0)107 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)108 instances = instance_res["ActiveInstances"]109 len(instances).should.equal(3)110@mock_ec2111def test_create_diversified_spot_fleet():112 conn = boto3.client("ec2", region_name="us-west-2")113 subnet_id = get_subnet_id(conn)114 diversified_config = spot_config(subnet_id, allocation_strategy="diversified")115 spot_fleet_res = conn.request_spot_fleet(SpotFleetRequestConfig=diversified_config)116 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]117 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)118 instances = instance_res["ActiveInstances"]119 len(instances).should.equal(2)120 instance_types = set([instance["InstanceType"] for instance in instances])121 instance_types.should.equal(set(["t2.small", "t2.large"]))122 instances[0]["InstanceId"].should.contain("i-")123@mock_ec2124def test_create_spot_fleet_request_with_tag_spec():125 conn = boto3.client("ec2", region_name="us-west-2")126 subnet_id = get_subnet_id(conn)127 tag_spec = [128 {129 "ResourceType": "instance",130 "Tags": [131 {"Key": "tag-1", "Value": "foo"},132 {"Key": "tag-2", "Value": "bar"},133 ],134 }135 ]136 config = spot_config(subnet_id)137 config["LaunchSpecifications"][0]["TagSpecifications"] = tag_spec138 spot_fleet_res = conn.request_spot_fleet(SpotFleetRequestConfig=config)139 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]140 spot_fleet_requests = conn.describe_spot_fleet_requests(141 SpotFleetRequestIds=[spot_fleet_id]142 )["SpotFleetRequestConfigs"]143 spot_fleet_config = spot_fleet_requests[0]["SpotFleetRequestConfig"]144 spot_fleet_config["LaunchSpecifications"][0]["TagSpecifications"][0][145 "ResourceType"146 ].should.equal("instance")147 for tag in tag_spec[0]["Tags"]:148 spot_fleet_config["LaunchSpecifications"][0]["TagSpecifications"][0][149 "Tags"150 ].should.contain(tag)151 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)152 instances = conn.describe_instances(153 InstanceIds=[i["InstanceId"] for i in instance_res["ActiveInstances"]]154 )155 for instance in instances["Reservations"][0]["Instances"]:156 for tag in tag_spec[0]["Tags"]:157 instance["Tags"].should.contain(tag)158@mock_ec2159def test_cancel_spot_fleet_request():160 conn = boto3.client("ec2", region_name="us-west-2")161 subnet_id = get_subnet_id(conn)162 spot_fleet_res = conn.request_spot_fleet(163 SpotFleetRequestConfig=spot_config(subnet_id)164 )165 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]166 conn.cancel_spot_fleet_requests(167 SpotFleetRequestIds=[spot_fleet_id], TerminateInstances=True168 )169 spot_fleet_requests = conn.describe_spot_fleet_requests(170 SpotFleetRequestIds=[spot_fleet_id]171 )["SpotFleetRequestConfigs"]172 len(spot_fleet_requests).should.equal(0)173@mock_ec2174def test_modify_spot_fleet_request_up():175 conn = boto3.client("ec2", region_name="us-west-2")176 subnet_id = get_subnet_id(conn)177 spot_fleet_res = conn.request_spot_fleet(178 SpotFleetRequestConfig=spot_config(subnet_id)179 )180 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]181 conn.modify_spot_fleet_request(SpotFleetRequestId=spot_fleet_id, TargetCapacity=20)182 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)183 instances = instance_res["ActiveInstances"]184 len(instances).should.equal(10)185 spot_fleet_config = conn.describe_spot_fleet_requests(186 SpotFleetRequestIds=[spot_fleet_id]187 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]188 spot_fleet_config["TargetCapacity"].should.equal(20)189 spot_fleet_config["FulfilledCapacity"].should.equal(20.0)190@mock_ec2191def test_modify_spot_fleet_request_up_diversified():192 conn = boto3.client("ec2", region_name="us-west-2")193 subnet_id = get_subnet_id(conn)194 spot_fleet_res = conn.request_spot_fleet(195 SpotFleetRequestConfig=spot_config(subnet_id, allocation_strategy="diversified")196 )197 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]198 conn.modify_spot_fleet_request(SpotFleetRequestId=spot_fleet_id, TargetCapacity=19)199 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)200 instances = instance_res["ActiveInstances"]201 len(instances).should.equal(7)202 spot_fleet_config = conn.describe_spot_fleet_requests(203 SpotFleetRequestIds=[spot_fleet_id]204 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]205 spot_fleet_config["TargetCapacity"].should.equal(19)206 spot_fleet_config["FulfilledCapacity"].should.equal(20.0)207@mock_ec2208def test_modify_spot_fleet_request_down_no_terminate():209 conn = boto3.client("ec2", region_name="us-west-2")210 subnet_id = get_subnet_id(conn)211 spot_fleet_res = conn.request_spot_fleet(212 SpotFleetRequestConfig=spot_config(subnet_id)213 )214 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]215 conn.modify_spot_fleet_request(216 SpotFleetRequestId=spot_fleet_id,217 TargetCapacity=1,218 ExcessCapacityTerminationPolicy="noTermination",219 )220 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)221 instances = instance_res["ActiveInstances"]222 len(instances).should.equal(3)223 spot_fleet_config = conn.describe_spot_fleet_requests(224 SpotFleetRequestIds=[spot_fleet_id]225 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]226 spot_fleet_config["TargetCapacity"].should.equal(1)227 spot_fleet_config["FulfilledCapacity"].should.equal(6.0)228@mock_ec2229def test_modify_spot_fleet_request_down_odd():230 conn = boto3.client("ec2", region_name="us-west-2")231 subnet_id = get_subnet_id(conn)232 spot_fleet_res = conn.request_spot_fleet(233 SpotFleetRequestConfig=spot_config(subnet_id)234 )235 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]236 conn.modify_spot_fleet_request(SpotFleetRequestId=spot_fleet_id, TargetCapacity=7)237 conn.modify_spot_fleet_request(SpotFleetRequestId=spot_fleet_id, TargetCapacity=5)238 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)239 instances = instance_res["ActiveInstances"]240 len(instances).should.equal(3)241 spot_fleet_config = conn.describe_spot_fleet_requests(242 SpotFleetRequestIds=[spot_fleet_id]243 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]244 spot_fleet_config["TargetCapacity"].should.equal(5)245 spot_fleet_config["FulfilledCapacity"].should.equal(6.0)246@mock_ec2247def test_modify_spot_fleet_request_down():248 conn = boto3.client("ec2", region_name="us-west-2")249 subnet_id = get_subnet_id(conn)250 spot_fleet_res = conn.request_spot_fleet(251 SpotFleetRequestConfig=spot_config(subnet_id)252 )253 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]254 conn.modify_spot_fleet_request(SpotFleetRequestId=spot_fleet_id, TargetCapacity=1)255 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)256 instances = instance_res["ActiveInstances"]257 len(instances).should.equal(1)258 spot_fleet_config = conn.describe_spot_fleet_requests(259 SpotFleetRequestIds=[spot_fleet_id]260 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]261 spot_fleet_config["TargetCapacity"].should.equal(1)262 spot_fleet_config["FulfilledCapacity"].should.equal(2.0)263@mock_ec2264def test_modify_spot_fleet_request_down_no_terminate_after_custom_terminate():265 conn = boto3.client("ec2", region_name="us-west-2")266 subnet_id = get_subnet_id(conn)267 spot_fleet_res = conn.request_spot_fleet(268 SpotFleetRequestConfig=spot_config(subnet_id)269 )270 spot_fleet_id = spot_fleet_res["SpotFleetRequestId"]271 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)272 instances = instance_res["ActiveInstances"]273 conn.terminate_instances(InstanceIds=[i["InstanceId"] for i in instances[1:]])274 conn.modify_spot_fleet_request(275 SpotFleetRequestId=spot_fleet_id,276 TargetCapacity=1,277 ExcessCapacityTerminationPolicy="noTermination",278 )279 instance_res = conn.describe_spot_fleet_instances(SpotFleetRequestId=spot_fleet_id)280 instances = instance_res["ActiveInstances"]281 len(instances).should.equal(1)282 spot_fleet_config = conn.describe_spot_fleet_requests(283 SpotFleetRequestIds=[spot_fleet_id]284 )["SpotFleetRequestConfigs"][0]["SpotFleetRequestConfig"]285 spot_fleet_config["TargetCapacity"].should.equal(1)286 spot_fleet_config["FulfilledCapacity"].should.equal(2.0)287@mock_ec2288def test_create_spot_fleet_without_spot_price():289 conn = boto3.client("ec2", region_name="us-west-2")290 subnet_id = get_subnet_id(conn)291 # remove prices to force a fallback to ondemand price292 spot_config_without_price = spot_config(subnet_id)293 del spot_config_without_price["SpotPrice"]294 for spec in spot_config_without_price["LaunchSpecifications"]:295 del spec["SpotPrice"]296 spot_fleet_id = conn.request_spot_fleet(297 SpotFleetRequestConfig=spot_config_without_price298 )["SpotFleetRequestId"]299 spot_fleet_requests = conn.describe_spot_fleet_requests(300 SpotFleetRequestIds=[spot_fleet_id]301 )["SpotFleetRequestConfigs"]302 len(spot_fleet_requests).should.equal(1)303 spot_fleet_request = spot_fleet_requests[0]304 spot_fleet_config = spot_fleet_request["SpotFleetRequestConfig"]305 len(spot_fleet_config["LaunchSpecifications"]).should.equal(2)306 launch_spec1 = spot_fleet_config["LaunchSpecifications"][0]307 launch_spec2 = spot_fleet_config["LaunchSpecifications"][1]308 # AWS will figure out the price309 assert "SpotPrice" not in launch_spec1310 assert "SpotPrice" not in launch_spec2

Full Screen

Full Screen

spot_fleet.py

Source:spot_fleet.py Github

copy

Full Screen

...50 ]51 }52def get_launch_template_list(region_number, instance_type_list):53 return list(map(lambda type: get_launch_template(region_number, type), instance_type_list))54def request_spot_fleet(ec2_client, region_number, number_of_instances, instance_type_list):55 LOGGER.info("Requesting spot fleet")56 LOGGER.info("Creating node_name_tag: %s" %57 utils.get_node_name_tag(region_number))58 # https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.request_spot_fleet59 response = ec2_client.request_spot_fleet(60 # DryRun=True,61 SpotFleetRequestConfig={62 # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet.html#spot-fleet-allocation-strategy63 'AllocationStrategy': 'diversified',64 'IamFleetRole': 'arn:aws:iam::656503231766:role/RichardFleetRole',65 'LaunchSpecifications': create_launch_specification_list(region_number, instance_type_list),66 # 'SpotPrice': 'string', # The maximum price per unit hour that you are willing to pay for a Spot Instance. The default is the On-Demand price.67 'TargetCapacity': number_of_instances,68 'Type': 'maintain'69 }70 )71 return response["SpotFleetRequestId"]72def request_spot_fleet_with_on_demand(ec2_client, region_number, number_of_instances, number_of_on_demand, instance_type_list):73 LOGGER.info("Requesting spot fleet")74 LOGGER.info("Creating node_name_tag: %s" %75 utils.get_node_name_tag(region_number))76 # https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.request_spot_fleet77 response = ec2_client.request_spot_fleet(78 # DryRun=True,79 SpotFleetRequestConfig={80 # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet.html#spot-fleet-allocation-strategy81 'AllocationStrategy': 'diversified',82 'IamFleetRole': 'arn:aws:iam::656503231766:role/RichardFleetRole',83 'LaunchTemplateConfigs': get_launch_template_list(region_number, instance_type_list),84 # 'SpotPrice': 'string', # The maximum price per unit hour that you are willing to pay for a Spot Instance. The default is the On-Demand price.85 'TargetCapacity': number_of_instances,86 'OnDemandTargetCapacity': number_of_on_demand,87 'Type': 'maintain'88 }89 )90 return response91def get_instance_ids(client, request_id):92 res = client.describe_spot_fleet_instances(93 SpotFleetRequestId=request_id94 )95 return [ inst["InstanceId"] for inst in res["ActiveInstances"] ]96def run_one_region(region_number, number_of_instances, fout, fout2):97 client = utils.create_client(utils.CONFIG, region_number)98 instance_type_list = ['t2.micro', 't2.small', 'm3.medium']99 # node_name_tag = request_spot_fleet_with_on_demand(100 # client, region_number, int(number_of_instances), 1, instance_type_list)101 request_id = request_spot_fleet(102 client, region_number, int(number_of_instances), instance_type_list)103 instance_ids = get_instance_ids(client, request_id)...

Full Screen

Full Screen

request_spot_instance.py

Source:request_spot_instance.py Github

copy

Full Screen

...9EC2_ROLE = 'arn:aws:iam::11111111111111:instance-profile/ec2-deep-learning'10REGION = 'us-west-2'11KEY_NAME = 'KEY_NAME'12SECURITY_GRUOP_ID = 'sg-111111'13def request_spot_fleet(event, user_data):14 ami_id = event['ami_id']15 instance_type = event['instance_type']16 spot_price = event['spot_price']17 18 ec2_client = boto3.client('ec2',19 region_name = REGION20 )21 response = response = ec2_client.request_spot_fleet(22 SpotFleetRequestConfig = {23 'SpotPrice': spot_price,24 'TargetCapacity': TARGET_CAPACITY,25 'IamFleetRole': FLEET_ROLE,26 'LaunchSpecifications': [27 {28 'ImageId': ami_id,29 'KeyName': KEY_NAME,30 'InstanceType': instance_type,31 'UserData': user_data,32 'Placement':{},33 'SecurityGroups': [34 {35 'GroupId' : SECURITY_GRUOP_ID36 }37 ],38 'IamInstanceProfile': {39 'Arn': EC2_ROLE40 }41 },42 ],43 'AllocationStrategy': 'lowestPrice',44 'Type': 'request'45 }46 )47 return response48def create_user_data(event):49 shell='''#!/bin/sh50 cd /home/ubuntu51 sudo -u ubuntu git clone {5}52 sudo -u ubuntu mkdir {0}53 sudo -u ubuntu mkdir {1}54 sudo -u ubuntu echo "*/5 * * * * /home/ubuntu/.pyenv/shims/aws s3 sync {1} {4}/output > /dev/null 2>&1" >> mycron55 sudo -u ubuntu echo "*/1 * * * * /home/ubuntu/.pyenv/shims/aws s3 cp /home/ubuntu/trace.log {4}/ > /dev/null 2>&1" >> mycron56 sudo -u ubuntu echo "*/1 * * * * /home/ubuntu/.pyenv/shims/aws s3 cp /home/ubuntu/completed.log {4}/ > /dev/null 2>&1" >> mycron57 sudo -u ubuntu /usr/bin/crontab mycron58 sudo -u ubuntu /bin/rm /home/ubuntu/mycron59 sudo -u ubuntu cd /home/ubuntu/{6}60 sudo -u ubuntu touch trace.log61 sudo -u ubuntu -i which python >> trace.log 2>&162 sudo -u ubuntu -i which aws >> trace.log 2>&163 sudo -u ubuntu echo 'repository_name: {6}' >> trace.log 2>&164 sudo -u ubuntu echo 'dataget_command: {7}' >> trace.log 2>&165 sudo -u ubuntu echo 'exec_command: {8}' >> trace.log 2>&166 sudo -u ubuntu echo 'loading data...' >> trace.log 2>&167 sudo -u ubuntu -i {7} > /dev/null 2>> trace.log68 sudo -u ubuntu echo 'starting executing command...' >> trace.log 2>&169 sudo -u ubuntu -i {8} >> trace.log 2>&170 sudo -u ubuntu -i aws s3 sync {1} {4}/output >> trace.log 2>&171 sudo -u ubuntu touch /home/ubuntu/completed.log72 '''73 s3_url = u's3://%s/%s' % (event['bucket_name'], event['exec_name'])74 shell_code = shell.format(75 event["data_dir"],76 event["output_dir"],77 os.environ.get('S3_ACCESS_KEY_ID'),78 os.environ.get('S3_SECRET_ACCESS_KEY'),79 s3_url,80 event["repository_url"],81 event["repository_name"],82 event["data_get_command"],83 event["exec_command"]84 )85 return base64.encodestring(shell_code.encode('utf-8')).decode('ascii')86def lambda_handler(event, context):87 user_data = create_user_data(event)88 response = request_spot_fleet(event, user_data)89 event["spot_fleet_request_id"] = response["SpotFleetRequestId"]...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful