0

Here is my code and the error when i try to share AMIs with another account. Any thoughts would be greatly appreciated.

import boto3
from datetime import datetime, timedelta

target_account_id = '000123456789'

def lambda_handler(event, context):
    # boto3 client
    ec2 = boto3.resource("ec2")


    """
    This function reurns the list of filtered AMI 
    """
    my_images = ec2.images.all()
    share_images = set()
    for image in my_images:
        created_at = datetime.strptime(
            image.creation_date,
            "%Y-%m-%dT%H:%M:%S.000Z",
        )
        if created_at > datetime.now() - timedelta(1):
            share_images.add(image.id)
            
    for image in share_images:
        image.modify_attribute(
        Attribute = 'launchPermission',
        OperationType = 'add',
        LaunchPermission = {
            'Add' : [{ 'UserId': target_account_id }]
        }
    )
    return images

----------------ERROR message----------------------

Function Logs
START RequestId: 0xxxxx-xxxx-xxxxxxxxxx Version: $LATEST
[ERROR] AttributeError: 'str' object has no attribute 'modify_attribute'
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 28, in lambda_handler
    image.modify_attribute(END RequestId: xxxxx-xxxx-xxxxxxxxxx
REPORT RequestId: xxxxx-xxxx-xxxxxxxxxx Duration: 29724.36 ms   Billed Duration: 29725 ms   Memory Size: 1000 MB    Max Memory Used: 790 MB Init Duration: 197.65 ms
Jonas
  • 121,568
  • 97
  • 310
  • 388
sniphar
  • 1
  • 1

1 Answers1

0

Instead of share_images.add(image.id), it should be share_images.add(image).

Marcin
  • 215,873
  • 14
  • 235
  • 294