10

I've encountered an issue with a simple AWS Lambda function written in Python.

When I run my Lambda function, my code is running as expected, the result is correct, but still ends with an error code (exit code): "Process exited before completing request", this is causing the Lambda to run 3 times (async).

Do you have any best practice to manage the exit code for Lambda ?

#!/usr/bin/python

import boto3
import sys
import tweepy
import datetime

session = boto3

# Init s3 client
s3 = session.resource('s3')

def get_data_and_push(s3_bucket, s3_key, user):
    # Retrieve CSV file
    try:
        dest = s3.Object(s3_bucket, s3_key)
        dest.download_file(tmpfile)
    except Exception, e:
        print 'An error occured while trying to download CSV file'
        print 'This exception has been thrown :'
        print e
        sys.exit(1)

    # Authenticate to Twitter
    try:
        auth = tweepy.OAuthHandler(t_consumer_key, t_consumer_secret)
        auth.set_access_token(t_access_token_key, t_access_token_secret)
        api = tweepy.API(auth)
    except Exception, e:
        print 'Cannot authenticate to Twitter.'
        print 'This exception has been thrown :'
        print e
        sys.exit(2)

    data = api.get_user(user)
    print 'User : ' + data.screen_name
    print 'Followers : ' + str(data.followers_count)
    print 'Friends : ' + str(data.friends_count)
    print '-----------'

    # Get today's date
    today = datetime.datetime.now().strftime("%Y-%m-%d")

    # Write result
    try:
        # Write result at the end of the file
        file = open(tmpfile, 'a')
        file.write(today + ',' + str(data.followers_count) + ',' + str(data.friends_count)+ '\n')
        file.close()
    except Exception, e:
        print 'Unable to write in temp file'
        print 'This exception has been thrown :'
        print e
        sys.exit(5)

    # Upload final file
    try:
        # Push file to S3
        dest.upload_file(tmpfile)
    except Exception, e:
        print 'An error occured while trying to upload CSV file'
        print 'This exception has been thrown :'
        print e
        sys.exit(6)

def main(event, context):
    for user in userlist:
        get_data_and_push(bucket, 'export_' + user + '.csv', user)
    sys.exit(0)

Thanks for your help,

1 Answer 1

16

Short

Yes, remove the sys.exit(0) at the end of your code, that should do it :-)


Longer

By doing sys.exit(0) you actually stop the process running your code in the Lambda Function. And this is not expected by the executor.

I assume the handler of a Lambda Function is actually run from within AWS's framework. Hence you already are in a python process, and your handler is called somewhere in AWS's code. So if you exit the process, you actually short-cut AWS's framework, which cannot handle the resolution of the Lambda's execution.

Sign up to request clarification or add additional context in comments.

2 Comments

Oddly enough, AWS's own documentation shows usage of sys.exit, albeit outside of the handler function, when attempting to connect to a db here.
That's correct, and it should be like that. This question is asking about an exit in the handler, which will stop the runtime from taking appropriate steps with the exception. That code is attempting to establish a connection pool before the runtime begins accepting requests. If it can't connect to the database, it should exit early and put the entire lambda in an unhealthy state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.