1

I am trying to run a python script that is present in AWS Lambda /tmp directory. The scripts require some extra dependencies like boto3 etc to run the file. When AWS Lambda runs the file it gives out the following error:

ModuleNotFoundError: No module named 'boto3'

However when i run this file directly as a lambda function then it runs easily whithout any import errors.

The Lambda Code that is trying to execute the code present in /tmp directory :

import json
import os
import urllib.parse
import boto3
s3 = boto3.client('s3')


def lambda_handler(event, context):
    records = [x for x in event.get('Records', []) if x.get('eventName') == 'ObjectCreated:Put']
    sorted_events = sorted(records, key=lambda e: e.get('eventTime'))
    latest_event = sorted_events[-1] if sorted_events else {}
    info = latest_event.get('s3', {})
    file_key = info.get('object', {}).get('key')
    bucket_name = info.get('bucket', {}).get('name')
    s3 = boto3.resource('s3')
    BUCKET_NAME = bucket_name
    keys = [file_key]
    for KEY in keys:
        local_file_name = '/tmp/'+KEY
        s3.Bucket(BUCKET_NAME).download_file(KEY, local_file_name)
        print("Running Incoming File !! ")
        os.system('python ' + local_file_name)

The /tmp code that is trying to get some data from S3 using boto3 :

import sys
import boto3
import json


def main():
    session = boto3.Session(
                aws_access_key_id='##',
                aws_secret_access_key='##',
                region_name='##')
                
    s3 = session.resource('s3')

    # get a handle on the bucket that holds your file
    bucket = s3.Bucket('##')

    # get a handle on the object you want (i.e. your file)
    obj = bucket.Object(key='8.json') 

    # get the object
    response = obj.get()

    # read the contents of the file
    lines = response['Body'].read().decode()
    data = json.loads(lines)
    transactions = data['dataset']['fields']
    print(str(len(transactions)))
    return str(len(transactions))

main()

So boto3 is imported in both the codes . But its only successful when the lambda code is executing it . However /tmp code cant import boto3 . What can be the reason and how can i resolve it ?

3
  • 1
    Can you upload code that you are trying to execute on /tmp as well as the code which is executing this file from tmp location of lambda? Commented Jan 24, 2021 at 14:22
  • @amitd Uploaded the code . Please check. Commented Jan 25, 2021 at 5:00
  • Why did you put source code in the /tmp folder? Commented Jan 22, 2022 at 15:19

1 Answer 1

1

Executing another python process does not copy Lambda's PYTHONPATH by default:

os.system('python ' + local_file_name)

Rewrite like this:

os.system('PYTHONPATH=/var/runtime python ' + local_file_name)

In order to find out complete PYTHONPATH the current Lambda version is using, add the following to the first script (one executed by Lambda):

import sys
print(sys.path)
Sign up to request clarification or add additional context in comments.

Comments

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.