0

Anyone have any idea about this error. I am using AWS translate, boto3, and Zappa and made a script to convert the language which works fine in local and throw this error on AWS lambda. I seriously don't know why? Anyone face this error before?

Here is my code:

from flask import Flask, jsonify, Blueprint, request, current_app
import os
import boto3

app = Flask(__name__)

translate = boto3.client(service_name='translate')
s3 = boto3.resource('s3')
s3_data = boto3.client('s3')

def check_file(file_name):

    s3 = boto3.resource('s3')
    my_bucket = s3.Bucket('output-file1')
    for s3_filename in my_bucket.objects.all():
        if s3_filename.key ==  file_name:
            return True
        else:
            return False

def convert_data():
    BUCKET_NAME = 'output-file1'
    my_bucket = s3.Bucket('input-file1')
    for s3_object in my_bucket.objects.all():
        data = check_file(s3_object.key)
        if data == False or data is None:
            body = s3_object.get()['Body'].read()
            file_data = bytearray(body)
            response = translate.translate_text(Text=str(file_data), SourceLanguageCode="en", TargetLanguageCode="fr") 
            # response = translate.translate_text(Text=str(file_data), TerminologyNames=["testing"],\
            #             SourceLanguageCode="en", TargetLanguageCode="fr")
            print("Translated text: " + response.get('TranslatedText'))
            with open('/tmp/' + s3_object.key, 'w') as txtfile:
                txtfile.write(str(response.get('TranslatedText')))
                txtfile.close()
                s3_data.upload_file(Bucket='output-file1',\
                        Key=s3_object.key, Filename='/tmp/' + s3_object.key)
                print('========Upload The File===========')

# convert_data()

def convert_langauge():

    with app.app_context():
        convert_data()
    return True


if __name__ == "__main__":
    app.run()
Chirag Kalal
  • 638
  • 1
  • 7
  • 21

1 Answers1

2

If the script runs on local and isn't working on lambda the likely cause is that your boto3 library versions are different on local and lambda. AWS doesn't update their boto3 often and I ran into a similar issue with cognito object lock.

To fix this, you can download (export) the lambda function to your local machine. Next get the boto3 version you have running locally by using this:

pip install boto3==<your_local_version> -t lib/

where lib/ is name of the directory where these files will be installed.

Next, copy all the files inside the lib folder (do not copy the folder iteself) and paste them inside the zip that you downloaded (exported). DO NOT extract the zip and repackage it, just open it in winzip or winrar and paste the files/folders inside the lib folder to your lambda function zip. Next you go to your lambda in console and upload the zip again. It will replace your lambda created by zappa and will not change your api path. You will also have the correct boto3 version installed.

Ninad Gaikwad
  • 4,272
  • 2
  • 13
  • 23