简体   繁体   English

如何使用 boto3 和 AWS 创建具有队列请求类型的 Spot 实例 Lambda

[英]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.我正在尝试创建请求类型为“fleet”的 spot 实例,其中有“medium”、“t2.large”、“t3a.medium”、“t3a.large”、“t3.medium”、“t3.large”实例使用 boto3。

My code runs but I am getting 6 different spot fleet with request type of 'instance' with 6 different instances of same kind.我的代码运行但我得到 6 个不同的 spot 队列,请求类型为“实例”,有 6 个不同的相同类型的实例。

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.而不是我试图发出 1 个提供“t2.medium”、“t2.large”、“t3a.medium”、“t3a.large”、“t3.medium”、“t3.large”实例的现货车队请求。

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 . EC2 队列中的 EC2 实例是根据分配策略启动的。 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.其工作方式是您为队列中的每种实例类型指定启动模板,AWS 将根据所选策略确定启动哪些类型。

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.有几种策略类型,例如price-capacity-optimizedlowest-pricediversified等。您可以在上面链接的 AWS 文档中阅读这些内容。 The default strategy is lowest-price , which means that AWS will try to launch instances from the lowest price pool.默认策略是lowest-price ,这意味着 AWS 将尝试从最低价格池中启动实例。 This is probably why you get only t3a.medium type instances.这可能就是为什么您只获得t3a.medium类型实例的原因。

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.有了这些策略意味着您不能明确地说我想要我在launch_template_configs列表中指定的每种类型的一个实例。 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.您可以做的是覆盖默认策略并尝试使用类似diversified的策略,它会尝试在所有 Spot 容量池中分配实例。

You can override the strategy if in the create_fleet command:如果在create_fleet命令中,您可以覆盖该策略:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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