r2k
r2k

Reputation: 23

How do I create a tag using resource method in boto3 aws lambda

I am trying to create a tag using ec2.resource method NOT ec2.CLIENT... I can create it with client but trying to figure out with resource. I am getting an error "errorMessage": "'dict' object has no attribute 'create_tags'". I have provided my code. I am comparing certain tag which I am using if statement but when I use create_tag method I get an error.

    import boto3
    import collections
    import sys
    ec2 = boto3.resource('ec2',region_name='us-east-2')
    def lambda_handler(event, context):
    vol = ec2.Volume(id='1256')
    for delV in vol.tags:
    delV.create_tags(Tags=[{'Key':'Name', 'Value':'Test1'}])

Upvotes: 1

Views: 339

Answers (1)

Ashaman Kingpin
Ashaman Kingpin

Reputation: 1577

Try this, you don't need to iterate over the existing tags like you are doing;

import boto3
import collections
import sys
ec2 = boto3.resource('ec2',region_name='us-east-2')
def lambda_handler(event, context):
    my_list_of_ids = [1256, 1234, 2300]
    for volid in my_list_of_ids:
        vol = ec2.Volume(id=volid)
        for tag in vol.tags:
          if tag['Name'] == 'Name' and tag['Value'] == Value:
            print("Exists")
        vol.create_tags(Tags=[{'Key':'Name', 'Value':'Test1'}])

Upvotes: 1

Related Questions