【问题标题】:unable to tag the ec2 resources boto3 python无法标记 ec2 资源 boto3 python
【发布时间】:2019-06-22 00:50:03
【问题描述】:

我想使用 boto3 python api 标记我正在启动的主机

response = client.allocate_hosts(
AutoPlacement='on'|'off',
AvailabilityZone='string',
ClientToken='string',
InstanceType='string',
Quantity=123,
TagSpecifications=[
    {
        'ResourceType': 'dedicated-host',
        'Tags': [
            {
                'Key': 'string',
                'Value': 'string'
            },
        ]
    },
])

这就是我正在做的事情

Availability Zone,Instance Type,Quantity 参数化,我使用字典输入数据

            count = 10
            input_dict = {}
            input_dict['AvailabilityZone'] = 'us-east-1a'
            input_dict['InstanceType'] = 'c5.large'
            input_dict['Quantity'] = int(count)
            instance = client.allocate_hosts(**input_dict,)
            print(str(instance))

此代码适用于我,但我也需要标记资源

TagSpecifications=[
    {
        'ResourceType': 'customer-gateway'|'dedicated-host'|'dhcp-options'|'elastic-ip'|'fleet'|'fpga-image'|'image'|'instance'|'internet-gateway'|'launch-template'|'natgateway'|'network-acl'|'network-interface'|'reserved-instances'|'route-table'|'security-group'|'snapshot'|'spot-instances-request'|'subnet'|'transit-gateway'|'transit-gateway-attachment'|'transit-gateway-route-table'|'volume'|'vpc'|'vpc-peering-connection'|'vpn-connection'|'vpn-gateway',
        'Tags': [
            {
                'Key': 'string',
                'Value': 'string'
            },
        ]
    },
]

我怎样才能将它输入到字典中.. 似乎标签规范在 dict 中有字典.. 我正在犯语法错误。我尝试了以下代码但没有成功。

 input_dict['TagSpecifications'] = [{'ResourceType':'dedicated-host','Tags':[{'key':'Name','Value':'demo'},]},]

【问题讨论】:

  • 只是检查...您确定要使用专用主机吗?该用例通常仅限于需要控制套接字和处理器分配的奇怪许可情况。如果您担心其他人在同一主机上,您可以使用 Dedicated Instance,它更便宜、更容易。
  • 是的......它用于许可用例:)
  • “尝试以下代码但未成功”是什么意思?您是否收到错误消息,或者它只是没有标记主机?

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


【解决方案1】:

最简单的方法是直接传值:

response = client.allocate_hosts(
    AvailabilityZone='us-east-1a',
    InstanceType='c5.large',
    Quantity=10,
    TagSpecifications=[
        {
            'ResourceType': 'dedicated-host',
            'Tags': [
                {
                    'Key': 'Name',
                    'Value': 'Demo'
                }
            ]
        }
    ])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 2022-11-21
    • 2020-09-30
    • 2019-10-19
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多