Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto3 script to create instance tag

I am trying to create a new tag called Name and value hostname apphostname for an Amazon EC2 instance.

Below is my code, and it is failing with this error message:

>>> 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.
>>>
like image 488
bobby Avatar asked Jan 03 '17 18:01

bobby


People also ask

How do you add a tag to EC2 instance boto3?

Step 1: Import boto3 and botocore exceptions to handle exceptions. Step 2: secret_location and tags_dict are the required parameters in this function. tags_dict should be as {“key”:”value”,..} Step 3: Create an AWS session using boto3 lib.

How do I tag an instance?

In the navigation pane, select a resource type (for example, Instances). Select the resource from the resource list and choose the Tags tab. Choose Manage tags, Add tag. Enter the key and value for the tag.


1 Answers

See Create Tags. It expects key value arguments. Tags is a list of dictionaries. You can create more than one tag if you want.

ec2.create_tags(Resources=['i-1923943832310'], Tags=[{'Key':'name', 'Value':'apphostname'}])
like image 174
helloV Avatar answered Sep 23 '22 11:09

helloV