用于创建实例标签的 Boto3 脚本

Boto3 script to create instance tag

我正在尝试为 Amazon EC2 实例创建一个名为 Name 的新标签和值主机名 apphostname

下面是我的代码,它失败并显示此错误消息:

>>> ec2.create_tags(["i-1923943832310"], {"name": "apphostname"})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/site-packages/botocore/client.py", line 157, in _api_call
    "%s() only accepts keyword arguments." % py_operation_name)
TypeError: create_tags() only accepts keyword arguments.
>>>

参见Create Tags。它需要键值参数。 Tags 是字典列表。如果需要,您可以创建多个标签。

ec2.create_tags(Resources=['i-1923943832310'], Tags=[{'Key':'name', 'Value':'apphostname'}])