簡體   English   中英

如何使用 boto3 和 AWS 創建具有隊列請求類型的 Spot 實例 Lambda

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

我正在嘗試創建請求類型為“fleet”的 spot 實例,其中有“medium”、“t2.large”、“t3a.medium”、“t3a.large”、“t3.medium”、“t3.large”實例使用 boto3。

我的代碼運行但我得到 6 個不同的 spot 隊列,請求類型為“實例”,有 6 個不同的相同類型的實例。

這是我的代碼:

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)

這就是我的政策:

{
    "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"
                }
            }
        }
    ]
}

我得到了什么:

6 種不同的請求類型實例

6 個具有生命周期點的不同實例

而不是我試圖發出 1 個提供“t2.medium”、“t2.large”、“t3a.medium”、“t3a.large”、“t3.medium”、“t3.large”實例的現貨車隊請求。

我在這里做錯了什么? 我怎樣才能只有具有六個不同實例的車隊請求?

EC2 隊列中的 EC2 實例是根據分配策略啟動的。 其工作方式是您為隊列中的每種實例類型指定啟動模板,AWS 將根據所選策略確定啟動哪些類型。

有幾種策略類型,例如price-capacity-optimizedlowest-pricediversified等。您可以在上面鏈接的 AWS 文檔中閱讀這些內容。 默認策略是lowest-price ,這意味着 AWS 將嘗試從最低價格池中啟動實例。 這可能就是為什么您只獲得t3a.medium類型實例的原因。

有了這些策略意味着您不能明確地說我想要我在launch_template_configs列表中指定的每種類型的一個實例。 您可以做的是覆蓋默認策略並嘗試使用類似diversified的策略,它會嘗試在所有 Spot 容量池中分配實例。

如果在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