简体   繁体   中英

How create Spot instances with request type of fleet using boto3 and AWS Lambda

I am trying to create spot instance with request type of 'fleet' which has.medium', 't2.large', 't3a.medium', 't3a.large', 't3.medium', 't3.large' instances using boto3.

My code runs but I am getting 6 different spot fleet with request type of 'instance' with 6 different instances of same kind.

Here's my code:

import boto3

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')

    # Create a launch template
    response = ec2.create_launch_template(
        DryRun=False,
        LaunchTemplateName='my-launch-template-1',
        VersionDescription='Initial version',
        LaunchTemplateData={
            'ImageId': 'ami-02b20e5594a5e5398',
            'InstanceType': 't2.medium',
            'Placement': {
                'AvailabilityZone': 'us-east-1a',
            },
            'EbsOptimized': True,
            'Monitoring': {
                'Enabled': True
            },
            'SecurityGroupIds': [
                'sg-053a39faea8548b14',
            ]
        }
    )

    # Get the launch template ID
    launch_template_id = response['LaunchTemplate']['LaunchTemplateId']

    # Set the desired capacity of the fleet to the number of instance types specified
    desired_capacity = 6

    # Set the target capacity of the fleet to the desired capacity
    target_capacity = desired_capacity

    # Create a launch template configuration for each instance type in the fleet
    launch_template_configs = []
    instance_types = ['t2.medium', 't2.large', 't3a.medium', 't3a.large', 't3.medium', 't3.large']
    for instance_type in instance_types:
        launch_template_config = {
            'LaunchTemplateSpecification': {
                'LaunchTemplateId': launch_template_id,
                'Version': '$Latest'
            },
            'Overrides': [
                {
                    'InstanceType': instance_type
                }
            ]
        }
        launch_template_configs.append(launch_template_config)

    # Create the fleet
    response = ec2.create_fleet(
        DryRun=False,
        TargetCapacitySpecification={
            'TotalTargetCapacity': target_capacity,
            'OnDemandTargetCapacity': 0,
            'SpotTargetCapacity': target_capacity,
            'DefaultTargetCapacityType': 'spot'
        },
        LaunchTemplateConfigs=launch_template_configs
    )

    print(response)

This what my policy looks like:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "ec2:CreateFleet",
                "ec2:CreateLaunchTemplate",
                "iam:CreateServiceLinkedRole"
            ],
            "Resource": "*"
        },
        {
            "Action": [
                "ec2:RunInstances"
            ],
            "Effect": "Allow",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "ec2:RequestSpotInstances",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "iam:CreateServiceLinkedRole",
            "Resource": "arn:aws:iam::*:role/aws-service-role/ec2.amazonaws.com/AWSServiceRoleForEC2Fleet",
            "Condition": {
                "StringEquals": {
                    "iam:AWSServiceName": "ec2.amazonaws.com"
                }
            }
        }
    ]
}

What I'm getting:

6 种不同的请求类型实例

6 个具有生命周期点的不同实例

instead of this im trying to make 1 spot fleet request that provides 't2.medium', 't2.large', 't3a.medium', 't3a.large', 't3.medium', 't3.large' instances.

What am I doing wrong here? How can I have only fleet request that has six different instances?

EC2 instances in an EC2 fleet are launched based on an allocation strategy . The way this works is that you specify launch templates for each instance type you would like to have in your fleet and AWS will determine what types will be launched based on the strategy chosen.

There are a few strategy types, such as price-capacity-optimized , lowest-price , diversified , etc. You can read about these on the AWS documentation linked above. The default strategy is lowest-price , which means that AWS will try to launch instances from the lowest price pool. This is probably why you get only t3a.medium type instances.

Having these strategies in place means that you cannot explicitly say that I want one instance from each type I specify in the launch_template_configs list. What you can do is to override the default strategy and try to use something like diversified , which will try to distribute instances across all Spot capacity pools.

You can override the strategy if in the create_fleet command:

response = ec2.create_fleet(
    DryRun=False,
    TargetCapacitySpecification={
        'TotalTargetCapacity': target_capacity,
        'OnDemandTargetCapacity': 0,
        'SpotTargetCapacity': target_capacity,
        'DefaultTargetCapacityType': 'spot'
    },
    LaunchTemplateConfigs=launch_template_configs,
    SpotOptions={
        'AllocationStrategy': 'diversified',
    },
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM