# from pprint import pprint
from boto3.session import Session
import boto3
import time

separator = ';'

accounts = [
    '1234566789',
    '9886542323']

for accountid in accounts:

    # print('######## '+ accountid +' ########')

    client = boto3.client('sts')
    response = client.assume_role(
        RoleArn='arn:aws:iam::' + accountid + ':role/fullstop',
        RoleSessionName='fullstop',
        DurationSeconds=900
    )

    # pprint(response)

    # print(response['Credentials']['AccessKeyId'])
    # print(response['Credentials']['SecretAccessKey'])
    # print(response['Credentials']['SessionToken'])

    session = Session(aws_access_key_id=response['Credentials']['AccessKeyId'],
                      aws_secret_access_key=response['Credentials']['SecretAccessKey'],
                      aws_session_token=response['Credentials']['SessionToken']
                      )

    ec2 = session.resource('ec2', region_name='eu-west-1')
    ec2_eu_central_1 = session.resource('ec2', region_name='eu-central-1')

    # Boto 3
    # Use the filter() method of the instances collection to retrieve
    # all running EC2 instances.
    # print('######## eu-west-1 ########')
    instances = ec2.instances.filter(
        Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
    for instance in instances:
        image = ec2.Image(instance.image_id)
        if image and hasattr(image, 'name'): # if image is not None or not image or not image.name
            print(accountid + separator +
                  'eu-west-1' + separator +
                  instance.id + separator +
                  instance.image_id + separator +
                  image.name)
        else:
            print(accountid + separator +
                  'eu-west-1' + separator +
                  instance.id + separator +
                  instance.image_id)
        # break # remove this to list all instances

    time.sleep(5)

    # print('######## eu-central-1 ########')
    instances = ec2_eu_central_1.instances.filter(
        Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
    for instance in instances:
        image = ec2_eu_central_1.Image(instance.image_id)
        if image and hasattr(image, 'name'): # if image is not None or not image or not image.name
            print(accountid + separator +
                  'eu-central-1' + separator +
                  instance.id + separator +
                  instance.image_id + separator +
                  image.name)
        else:
            print(accountid + separator +
                  'eu-central-1' + separator +
                  instance.id + separator +
                  instance.image_id)
    # break

    time.sleep(5)

@@ -1,10 +1,8 @@
-# 
 from ppr
@@ -1315,32 +1315,537 @@
 e in instances:%0A
+        name = ''%0A        stack_name = ''%0A        stack_version = ''%0A        # pprint(instance.tags)%0A        if instance and hasattr(instance, 'tags') and hasattr(instance.tags, '__iter__'):%0A            for tag in instance.tags:%0A                if tag%5B'Key'%5D == 'Name':%0A                    name = tag%5B'Value'%5D%0A                elif tag%5B'Key'%5D == 'StackName':%0A                    stack_name = tag%5B'Value'%5D%0A                elif tag%5B'Key'%5D == 'StackVersion':%0A                    stack_version = tag%5B'Value'%5D%0A%0A
         image = 
@@ -1909,32 +1909,33 @@
 (image, 'name'):
+ 
  # if image is n
@@ -2051,32 +2051,158 @@
 ' + separator +%0A
+                  name + separator +%0A                  stack_name + separator +%0A                  stack_version + separator +%0A
                 
@@ -2401,32 +2401,158 @@
 ' + separator +%0A
+                  name + separator +%0A                  stack_name + separator +%0A                  stack_version + separator +%0A
                 
@@ -2620,24 +2620,28 @@
 id)%0A        
+    
 # break # re
@@ -2886,32 +2886,505 @@
 e in instances:%0A
+        name = ''%0A        stack_name = ''%0A        stack_version = ''%0A        if instance and hasattr(instance, 'tags') and hasattr(instance.tags, '__iter__'):%0A            for tag in instance.tags:%0A                if tag%5B'Key'%5D == 'Name':%0A                    name = tag%5B'Value'%5D%0A                elif tag%5B'Key'%5D == 'StackName':%0A                    stack_name = tag%5B'Value'%5D%0A                elif tag%5B'Key'%5D == 'StackVersion':%0A                    stack_version = tag%5B'Value'%5D%0A%0A
         image = 
@@ -3469,16 +3469,17 @@
 'name'):
+ 
  # if im
@@ -3606,32 +3606,158 @@
 ' + separator +%0A
+                  name + separator +%0A                  stack_name + separator +%0A                  stack_version + separator +%0A
                 
@@ -3959,32 +3959,158 @@
 ' + separator +%0A
+                  name + separator +%0A                  stack_name + separator +%0A                  stack_version + separator +%0A
                 

update script