Home > Software design >  cannot upload data to s3 through lambda
cannot upload data to s3 through lambda

Time:10-21

Im trying to extract trustd advisor data to s3 using lambda function. I have used Py3.8 ver. Below is the snippet of my code:

def s3_upload():
    try:
        s3 = boto3.resource('s3')
        s3.Bucket('BUCKET_NAME').put_object('/tmp/*.csv', file_name)
        return True
    except:
        print('Function failed to upload')
        return False
        traceback.print_exc()

def lambda_handler(event, context):
    try:
        support_client = boto3.client('support', region_name='us-west-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'])
            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'])])
            except:
                print('Failed to get check: '   checks['id']   ' --- '   checks['name'])
                traceback.print_exc()
    except:
        print('Failed! Debug further.')
        traceback.print_exc()
    
if __name__ == '__main__':
    lambda_handler

Permissions have also been setup properly for lambda. Along with basic execution role and support access role, Below is custom lambda permission:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:PutObjectTagging"
            ],
            "Resource": "arn:aws:s3:::test-ta-reports/*"
        },
        {
            "Sid": "VisualEditor2",
            "Effect": "Allow",
            "Action": [
                "trustedadvisor:DescribeCheckSummaries",
                "s3:CreateJob",
                "trustedadvisor:DescribeChecks",
                "trustedadvisor:DescribeCheckItems"
            ],
            "Resource": "*"
        }
    ]
}

Im new to Py and lambda. The function is executing without any errors but the data is not uploaded to s3. Am i missing anything ?

CodePudding user response:

You are not calling the s3_upload function anywhere, also the code is invalid since it has file_name variable in it which is not initialized.

CodePudding user response:

I've observed your script-

  • traceback.print_exc() This should be executed before the return statement so that the python compiler can identify the obstacles/errors

  • if __name__ == '__main__': lambda_handler This will work only if is used to execute some code only if the file was run directly, and not imported.

  • According to the documentation the first three parameters of the put_object method, def put_object(self, bucket_name, object_name, data, length, Fix your parameters of put_object.

  • you're not using s3_upload in your lambda.

  • Related