{"action":"created","issue":{"url":"https://api.github.com/repos/powerupcloud/AWSInventoryLambda/issues/2","repository_url":"https://api.github.com/repos/powerupcloud/AWSInventoryLambda","labels_url":"https://api.github.com/repos/powerupcloud/AWSInventoryLambda/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/powerupcloud/AWSInventoryLambda/issues/2/comments","events_url":"https://api.github.com/repos/powerupcloud/AWSInventoryLambda/issues/2/events","html_url":"https://github.com/powerupcloud/AWSInventoryLambda/issues/2","id":161160512,"number":2,"title":"getting error while executing the script","user":{"login":"amy201988","id":18699313,"avatar_url":"https://avatars.githubusercontent.com/u/18699313?v=3","gravatar_id":"","url":"https://api.github.com/users/amy201988","html_url":"https://github.com/amy201988","followers_url":"https://api.github.com/users/amy201988/followers","following_url":"https://api.github.com/users/amy201988/following{/other_user}","gists_url":"https://api.github.com/users/amy201988/gists{/gist_id}","starred_url":"https://api.github.com/users/amy201988/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amy201988/subscriptions","organizations_url":"https://api.github.com/users/amy201988/orgs","repos_url":"https://api.github.com/users/amy201988/repos","events_url":"https://api.github.com/users/amy201988/events{/privacy}","received_events_url":"https://api.github.com/users/amy201988/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-06-20T10:07:42Z","updated_at":"2016-12-31T11:56:14Z","closed_at":null,"body":"below is the code i modified:\n\nimport boto3  \nimport collections  \nimport datetime  \nimport csv\nfrom time import gmtime, strftime  \nimport smtplib  \nfrom email.MIMEMultipart import MIMEMultipart  \nfrom email.MIMEBase import MIMEBase  \nfrom email.MIMEText import MIMEText  \nfrom email import Encoders  \nimport os\n# EC2 connection beginning\n\nec = boto3.client('ec2')  \n# S3 connection beginning\n\ns3 = boto3.resource('s3')\n# lambda function beginning\n\ndef lambda_handler(event, context):  \n    #get to the curren date\n    date_fmt = strftime(\"%Y_%m_%d\", gmtime())\n    #Give your file path\n    filepath ='/tmp/PUC_AWS_Resources_' + date_fmt + '.csv'\n    #Give your filename\n    filename ='PUC_AWS_Resources_' + date_fmt + '.csv'\n    csv_file = open(filepath,'w+')\n\n```\n#boto3 library ec2 API describe region page\n#http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.describe_regions\nregions = ec.describe_regions().get('Regions',[] )\nfor region in regions:\n    reg=region['RegionName']\n    regname='REGION :' + reg\n    #EC2 connection beginning\n    ec2con = boto3.client('ec2',region_name=reg)\n    #boto3 library ec2 API describe instance page\n    #http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.describe_instances\n    reservations = ec2con.describe_instances().get(\n    'Reservations',[] \n    )\n    instances = sum(\n        [\n            [i for i in r['Instances']]\n            for r in reservations\n        ], [])\n    instanceslist = len(instances)\n    if instanceslist > 0:\n        csv_file.write(\"%s,%s,%s,%s,%s,%s\\n\"%('','','','','',''))\n        csv_file.write(\"%s,%s\\n\"%('EC2 INSTANCE',regname))\n        csv_file.write(\"%s,%s,%s,%s,%s,%s\\n\"%('InstanceID','Instance_State','InstanceName','Instance_Type','LaunchTime','Instance_Placement'))\n        csv_file.flush()\n\n    for instance in instances:\n        state=instance['State']['Name']\n        if state =='running':\n            for tags in instance['Tags']:\n                Instancename= tags['Value']\n                key= tags['Key']\n                if key == 'Name' :\n                    instanceid=instance['InstanceId']\n                    instancetype=instance['InstanceType']\n                    launchtime =instance['LaunchTime']\n                    Placement=instance['Placement']['AvailabilityZone']\n                    csv_file.write(\"%s,%s,%s,%s,%s,%s\\n\"% (instanceid,state,Instancename,instancetype,launchtime,Placement))\n                    csv_file.flush()\n\n    for instance in instances:\n        state=instance['State']['Name']\n        if state =='stopped':\n            for tags in instance['Tags']:\n                Instancename= tags['Value']\n                key= tags['Key']\n                if key == 'Name' :\n                    instanceid=instance['InstanceId']\n                    instancetype=instance['InstanceType']\n                    launchtime =instance['LaunchTime']\n                    Placement=instance['Placement']['AvailabilityZone']\n                    csv_file.write(\"%s,%s,%s,%s,%s,%s\\n\"%(instanceid,state,Instancename,instancetype,launchtime,Placement))\n                    csv_file.flush()\n\n    #boto3 library ec2 API describe volumes page\n    #http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.describe_volumes\n    ec2volumes = ec2con.describe_volumes().get('Volumes',[])\n    volumes = sum(\n        [\n            [i for i in r['Attachments']]\n            for r in ec2volumes\n        ], [])\n    volumeslist = len(volumes)\n    if volumeslist > 0:\n        csv_file.write(\"%s,%s,%s,%s\\n\"%('','','',''))\n        csv_file.write(\"%s,%s\\n\"%('EBS Volume',regname))\n        csv_file.write(\"%s,%s,%s,%s\\n\"%('VolumeId','InstanceId','AttachTime','State'))\n        csv_file.flush()\n\n    for volume in volumes:\n        VolumeId=volume['VolumeId']\n        InstanceId=volume['InstanceId']\n        State=volume['State']\n        AttachTime=volume['AttachTime']\n        csv_file.write(\"%s,%s,%s,%s\\n\" % (VolumeId,InstanceId,AttachTime,State))\n        csv_file.flush()\n\n    #Give your owner ID\n    account_ids='753227428434'    \n    #boto3 library ec2 API describe snapshots page\n    #http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.describe_snapshots\n    ec2snapshot = ec2con.describe_snapshots(OwnerIds=[\n        account_ids,\n    ],).get('Snapshots',[])\n    ec2snapshotlist = len(ec2snapshot)\n    if ec2snapshotlist > 0:\n        csv_file.write(\"%s,%s,%s,%s\\n\" % ('','','',''))\n        csv_file.write(\"%s,%s\\n\"%('EC2 SNAPSHOT',regname))\n        csv_file.write(\"%s,%s,%s,%s\\n\" % ('SnapshotId','VolumeId','StartTime','VolumeSize'))\n        csv_file.flush()\n\n    for snapshots in ec2snapshot:\n        SnapshotId=snapshots['SnapshotId']\n        VolumeId=snapshots['VolumeId']\n        StartTime=snapshots['StartTime']\n        VolumeSize=snapshots['VolumeSize']\n        csv_file.write(\"%s,%s,%s,%s\\n\" % (SnapshotId,VolumeId,StartTime,VolumeSize))\n        csv_file.flush()   \n\n    #boto3 library ec2 API describe addresses page    \n    #http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.describe_addresses\n    addresses = ec2con.describe_addresses().get('Addresses',[] )\n    addresseslist = len(addresses)\n    if addresseslist > 0:\n        csv_file.write(\"%s,%s,%s,%s,%s\\n\"%('','','','',''))\n        csv_file.write(\"%s,%s\\n\"%('EIPS INSTANCE',regname))\n        csv_file.write(\"%s,%s,%s\\n\"%('PublicIp','AllocationId','Domain'))\n        csv_file.flush() \n        for address in addresses:\n            PublicIp=address['PublicIp']\n            AllocationId=address['AllocationId']\n            Domain=address['Domain']\n            csv_file.write(\"%s,%s,%s\\n\"%(PublicIp,AllocationId,Domain))\n            csv_file.flush() \n\n    #RDS Connection beginning    \n    rdscon = boto3.client('rds',region_name=reg)\n\n    #boto3 library RDS API describe db instances page    \n    #http://boto3.readthedocs.org/en/latest/reference/services/rds.html#RDS.Client.describe_db_instances\n    rdb = rdscon.describe_db_instances().get(\n    'DBInstances',[] \n    )\n    rdblist = len(rdb)\n    if rdblist > 0:\n        csv_file.write(\"%s,%s,%s,%s\\n\" %('','','',''))\n        csv_file.write(\"%s,%s\\n\"%('RDS INSTANCE',regname))\n        csv_file.write(\"%s,%s,%s,%s\\n\" %('DBInstanceIdentifier','DBInstanceStatus','DBName','DBInstanceClass'))\n        csv_file.flush()\n\n    for dbinstance in rdb:\n        DBInstanceIdentifier = dbinstance['DBInstanceIdentifier']\n        DBInstanceClass = dbinstance['DBInstanceClass']\n        DBInstanceStatus = dbinstance['DBInstanceStatus']\n        DBName = dbinstance['DBName']\n        csv_file.write(\"%s,%s,%s,%s\\n\" %(DBInstanceIdentifier,DBInstanceStatus,DBName,DBInstanceClass))\n        csv_file.flush()\n\n\nses_user = \"AKIAICP6KWEKRM7X3GTA\"\nses_pwd = \"Ar9WyrfiTUtG376GFndoOGSyTeNIrXm0tPpec+fHWy+J\"\n\ndef mail(fromadd,to, subject, text, attach):\n   msg = MIMEMultipart()\n   msg['From'] = fromadd\n   msg['To'] = to\n   msg['Subject'] = subject\n   msg.attach(MIMEText(text))\n   part = MIMEBase('application', 'octet-stream')\n   part.set_payload(open(attach, 'rb').read())\n   Encoders.encode_base64(part)\n   part.add_header('Content-Disposition','attachment; filename=\"%s\"' % os.path.basename(attach))\n   msg.attach(part)\n   mailServer = smtplib.SMTP(\"email-smtp.us-east-1.amazonaws.com\", 587)\n   mailServer.ehlo()\n   mailServer.starttls()\n   mailServer.ehlo()\n   mailServer.login(ses_user, ses_pwd)\n   mailServer.sendmail(fromadd, to, msg.as_string())\n   # Should be mailServer.quit(), but that crashes...\n   mailServer.close()\n\ndate_fmt = strftime(\"%Y_%m_%d\", gmtime())\n#Give your file path\nfilepath ='/tmp/PUC_AWS_Resources_' + date_fmt + '.csv'\n#Give your filename\nmail(\"Amritha.Sanath@in.pega.com\",\"PegaCloud AWS Resources\",\"PFA for the AWS resource of pegacloud account.\",filepath)\ns3.Object('pega-inventory', filename).put(Body=open(filepath, 'rb'))\n```\n\nand while testing this function i am getting below error:\n\n{\n  \"stackTrace\": [\n    [\n      \"/var/task/lambda_function.py\",\n      56,\n      \"lambda_handler\",\n      \"for tags in instance['Tags']:\"\n    ]\n  ],\n  \"errorType\": \"KeyError\",\n  \"errorMessage\": \"'Tags'\"\n}\n"},"comment":{"url":"https://api.github.com/repos/powerupcloud/AWSInventoryLambda/issues/comments/269861858","html_url":"https://github.com/powerupcloud/AWSInventoryLambda/issues/2#issuecomment-269861858","issue_url":"https://api.github.com/repos/powerupcloud/AWSInventoryLambda/issues/2","id":269861858,"user":{"login":"nestoru","id":393446,"avatar_url":"https://avatars.githubusercontent.com/u/393446?v=3","gravatar_id":"","url":"https://api.github.com/users/nestoru","html_url":"https://github.com/nestoru","followers_url":"https://api.github.com/users/nestoru/followers","following_url":"https://api.github.com/users/nestoru/following{/other_user}","gists_url":"https://api.github.com/users/nestoru/gists{/gist_id}","starred_url":"https://api.github.com/users/nestoru/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nestoru/subscriptions","organizations_url":"https://api.github.com/users/nestoru/orgs","repos_url":"https://api.github.com/users/nestoru/repos","events_url":"https://api.github.com/users/nestoru/events{/privacy}","received_events_url":"https://api.github.com/users/nestoru/received_events","type":"User","site_admin":false},"created_at":"2016-12-31T11:56:13Z","updated_at":"2016-12-31T11:56:13Z","body":"You instance has no tags. While this is a bug I would not fix it because to me at least one Tag should be mandatory (Name). Name your instances and that will take care of it. If you are modifying code it would be great to fork it and show your diff or even better perform a pull request."}}
{"actor":{"display_login":"nestoru"}}