Home > Software engineering >  How to fix aws lambda function logs error?
How to fix aws lambda function logs error?

Time:10-26

I'm trying to extract aws trust advisor data through lambda function(trigger by event scheduler) and upload to s3. However, some part of the function throws error. below is my code

##libraries
import boto3
import os
import csv
from csv import DictWriter
import time
import traceback

## bucket_name is set as env variable
bucket_name = "test-ta-reports"
fail_msg = 'Pulling Trusted Advisor data failed'
Filename = "/tmp/checks_list.csv"
obj_name = time.strftime("%Y-%m-%d-%H-%M-%S")   '/'   '.csv'

##upload to s3
def s3_upload(bucket_name, Filename, obj_name):
    
    if obj_name is None:
        obj_name = os.path.basename(Filename)
    try:
        s3 = boto3.client("s3", region_name="eu-west-1")
        response = s3.upload_file(Filename, bucket_name, obj_name)
        return True
    except:
        print('Data failed to upload to bucket')
        traceback.print_exc()
        return False
    
def lambda_handler(event, context):
    try:
        support_client = boto3.client('support', region_name='us-east-1')
        ta_checks = support_client.describe_trusted_advisor_checks(language='en')
        checks_list = {ctgs: [] for ctgs in list(set([checks['category'] for checks in ta_checks['checks']]))}
        
        for checks in ta_checks['checks']:
            print('Getting check:'   checks['name']   checks['category'])
            try:
                check_summary = support_client.describe_trusted_advisor_check_summaries(
                                checkIds=[checks['id']])['summaries'][0]
                if check_summary['status'] != 'not_available':
                    checks_list[checks['category']].append(
                       [checks['name'], check_summary['status'],
                        str(check_summary['resourcesSummary']['resourcesProcessed']),
                        str(check_summary['resourcesSummary']['resourcesFlagged']),
                        str(check_summary['resourcesSummary']['resourcesSuppressed']),
                        str(check_summary['resourcesSummary']['resourcesIgnored'])
                        ])
                else:
                    print("unable to append checks")
                       
            except:
                print('Failed to get check: '    checks['name'])
                traceback.print_exc()
    except:
        print('Failed! Debug further.')
        traceback.print_exc()
    
    ##rewrite dict to csv
    with open('/tmp/checks_list.csv', 'w', newline='') as csvfile:
        csv_writer = DictWriter(csvfile, fieldnames=['status','hasFlaggedResources','timestamp','resourcesSummary','categorySpecificSummary', 'checkId'])
        csv_writer.writeheader()
        csv_writer.writerow(check_summary)
        return checks_list
        
    if s3_upload(bucket_name, Filename, obj_name):
        print("Successfully uploaded")

if __name__ == '__main__':
    lambda_handler(event, context)

The error logs

unable to append checks

I'm new to Python. So, unsure of how to check for trackback stacks under else: statement. Is there any way to modify this code for getting traceback logs for the append block. Also, have i made any error in the above code. I'm unable to figure out any. PLz help

CodePudding user response:

response = client.describe_trusted_advisor_check_summaries(
    checkIds=[
        'string',
    ]
)

describe_trusted_advisor_check_summaries() returns summarized results for one or more Trusted advisors. Here you are checking for the check_summary['status'] is not equal to not_avaialble i.e. alert status of the check is either "ok" (green), "warning" (yellow), "error" (red), and in that case, you are appending resourcesProcessed, resourcesFlagged, resourcesSuppressed, and resourcesIgnored to checks_list for further processing.

it's printing

unable to append checks

just because the status of the check is not_available. It is not an error log. Just deal with the case if the check status is not_available, what you should be doing?

See the documentation of describe_trusted_advisor_check_summaries. https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/support.html#Support.Client.describe_trusted_advisor_check_summaries

  • Related