【问题标题】:boto3 create auto scaling groupboto3 创建弹性伸缩组
【发布时间】:2021-03-16 08:46:00
【问题描述】:

我正在尝试使用 boto3 创建一个具有不同模板和类型的自动缩放组,

希望有人可以在这里帮助我 :) 这是我使用的代码:

import boto3


client = boto3.client('autoscaling')


def create_auto_scaling_group(**kwargs):
    response = client.create_auto_scaling_group(
        AutoScalingGroupName='TEST-ASG',
        LaunchTemplate={
            'LaunchTemplateName': 'TEST-Template',
            'Version': '5'
        },
        MixedInstancesPolicy={
            'LaunchTemplate': {
                'LaunchTemplateSpecification': {
                    'LaunchTemplateName': 'TEST-Template',
                    'Version': '5'
                },
                'Overrides': [
                    {
                        'InstanceType': 'g4dn.xlarge',
                        'LaunchTemplateSpecification': {
                            'LaunchTemplateName': 'TEST-Template',
                            'Version': '5'
                        }
                    },
                    {
                        'InstanceType': 'g3s.xlarge',
                        'LaunchTemplateSpecification': {
                            'LaunchTemplateName': 'TEST-Template',
                            'Version': '4'
                        }
                    },
                    {
                        'InstanceType': 'p3.2xlarge',
                        'LaunchTemplateSpecification': {
                            'LaunchTemplateName': 'TEST-Template',
                            'Version': '6'
                        }
                    },
                ]
            },
            'InstancesDistribution': {
                'OnDemandBaseCapacity': 0,
                'OnDemandPercentageAboveBaseCapacity': 0,
                'SpotAllocationStrategy': 'capacity-optimized',
            }
        },
        MinSize=0,
        MaxSize=0,
        DesiredCapacity=0,
        AvailabilityZones=[
            'string',
            'string',
            'string',
        ],
        Tags=[
            {
                'ResourceId': 'TEST-ASG',
                'ResourceType': 'auto-scaling-group',
                'Key': 'Name',
                'Value': 'TEST-ASG',
                'PropagateAtLaunch': True
            },
        ],
    )

create_auto_scaling_group()

我收到以下回复,但似乎找不到原因:

Traceback (most recent call last):
  File "c:/Users/bena/PycharmProjects/pythonProject/pythonProject/New/AutoScaleOverride.py", line 69, in <module>
    create_auto_scaling_group()
  File "c:/Users/bena/PycharmProjects/pythonProject/pythonProject/New/AutoScaleOverride.py", line 8, in create_auto_scaling_group
    response = client.create_auto_scaling_group(
  File "C:\Users\bena\AppData\Local\Programs\Python\Python38\lib\site-packages\botocore\client.py", line 357, in _api_call       
    return self._make_api_call(operation_name, kwargs)
  File "C:\Users\bena\AppData\Local\Programs\Python\Python38\lib\site-packages\botocore\client.py", line 676, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationError) when calling the CreateAutoScalingGroup operation: Valid requests must contain either LaunchTemplate, LaunchConfigurationName, InstanceId or MixedInstancesPolicy parameter.

在我尝试的单独 aws-cli 脚本上,它工作正常,这意味着所有内容都创建时没有错误。

第一次尝试,非常感谢任何帮助。

谢谢

【问题讨论】:

  • 您已经定义了一个名为create_auto_scaling_group 的函数,但我看不到该函数的调用位置。
  • @JohnRotenstein,是的,抱歉,用我收到的新回复编辑了帖子。

标签: python-3.x amazon-web-services amazon-ec2 boto3 aws-auto-scaling


【解决方案1】:

似乎自己管理:)

在这里添加对我有用的东西:

import boto3


client = boto3.client('autoscaling')


response = client.create_auto_scaling_group(
    AutoScalingGroupName='ASGName',
    MixedInstancesPolicy={
        'LaunchTemplate': {
            'LaunchTemplateSpecification': {
                'LaunchTemplateId': 'Id',
                'Version': '5'
            },
            'Overrides': [
                {
                    'InstanceType': 'g4dn.xlarge',
                    'LaunchTemplateSpecification': {
                        'LaunchTemplateId': 'Id',
                        'Version': '5'
                    }
                },
                {
                    'InstanceType': 'g3s.xlarge',
                    'LaunchTemplateSpecification': {
                        'LaunchTemplateId': 'Id',
                        'Version': '4'
                    }
                },
                {
                    'InstanceType': 'p3.2xlarge',
                    'LaunchTemplateSpecification': {
                        'LaunchTemplateId': 'Id',
                        'Version': '6'
                    }
                },
            ]
        },
        'InstancesDistribution': {
            'OnDemandBaseCapacity': 0,
            'OnDemandPercentageAboveBaseCapacity': 0,
            'SpotAllocationStrategy': 'capacity-optimized',
        }
    },
    MinSize=0,
    MaxSize=0,
    DesiredCapacity=0,
    VPCZoneIdentifier='subnet-1, subnet-2, subnet-3',
    Tags=[
        {
            'ResourceId': 'ASGName',
            'ResourceType': 'auto-scaling-group',
            'Key': 'Name',
            'Value': 'ASGName',
            'PropagateAtLaunch': True
        },
    ],
)

print(response)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-01
    • 2020-04-29
    • 2019-05-14
    • 2019-11-17
    • 2013-03-09
    • 2020-03-18
    • 1970-01-01
    • 2019-11-24
    相关资源
    最近更新 更多