![](/img/trans.png)
[英]not getting all aws instances using python boto3 - problem with Reservation
[英]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"
}
}
}
]
}
我得到了什么:
而不是我试图发出 1 个提供“t2.medium”、“t2.large”、“t3a.medium”、“t3a.large”、“t3.medium”、“t3.large”实例的现货车队请求。
我在这里做错了什么? 我怎样才能只有具有六个不同实例的车队请求?
EC2 队列中的 EC2 实例是根据分配策略启动的。 其工作方式是您为队列中的每种实例类型指定启动模板,AWS 将根据所选策略确定启动哪些类型。
有几种策略类型,例如price-capacity-optimized
、 lowest-price
、 diversified
等。您可以在上面链接的 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.