Amazon Textract
Developer Guide

Creating an AWS Lambda Function

You can call Amazon Textract API operations from within an AWS Lambda function. The following instructions show how to create a Lambda function in Python that calls DetectDocumentText. It returns a list of Block objects. To run this example, you need an Amazon S3 bucket that contains a document in PNG or JPEG format.

For an example that uses Lambda functions to process documents at a large scale, see Large scale document processing with Amazon Textract.

Step 1: Create an AWS Lambda deployment package

  1. Open a command window.

  2. Enter the following commands to create a deployment package with the most recent version of the AWS SDK.

    pip install boto3 --target python/. zip boto3-layer.zip -r python/

Step 2: Create a AWS Lambda function (console)

  1. Sign in to the AWS Management Console and open the AWS Lambda console at https://console.aws.amazon.com/lambda/.

  2. Select Create function. For more information, see Create a Lambda Function with the Console.

  3. Choose the following options.

    • Choose Author from scratch

    • Enter a value for Function name.

    • For Runtime choose Python 3.7 or Python 3.6.

    • For Choose or create an execution role, choose Create a new role with basic Lambda permissions.

  4. Choose Create function to create the AWS Lambda function.

  5. Open the IAM console at https://console.aws.amazon.com/iam/.

  6. From the navigation pane choose Roles.

  7. From the resources list, choose the IAM role that AWS Lambda created for you. The role name is prepended with the name of your Lambda function.

  8. In the Permissions tab, choose Attach policies.

  9. Add the AmazonTextractFullAccess and AmazonS3ReadOnlyAccess Policies.

  10. Select Attach Policy.

Step 3: Create and add a layer (console)

  1. Open the AWS Lambda console at https://console.aws.amazon.com/lambda/.

  2. In the navigation pane, choose Layers.

  3. Choose Create layer.

  4. Enter values for Name and Description.

  5. For Code entry type, choose Upload .zip file and select Upload.

  6. In the dialog box, select the zip file (boto3-layer.zip) that you created in Step 1: Create an AWS Lambda deployment package.

  7. For compatible runtimes, choose the runtime that you chose in Step 2: Create a AWS Lambda function (console).

  8. Choose Create to create the layer.

  9. Choose the navigation pane menu icon.

  10. In the navigation pane, choose Functions.

  11. In the resources list, select the function you created in Step 2: Create a AWS Lambda function (console).

  12. In the Designer section of the Configuration tab, choose Layers (under your Lambda function name).

  13. In the Layers section, choose Add a layer.

  14. Choose Select from list of runtime compatible layers.

  15. In Compatible layers, select the Name and Version of the layer name and version you created in step 3.

  16. Choose Add.

Step 4: Add python code (console)

  1. In Designer choose your function name.

  2. In the function code editor, add the the following to the file lambda_function.py. Change the values of bucket and document to your bucket and document.

    import json import boto3 def lambda_handler(event, context): bucket="bucket" document="document" client = boto3.client('textract') #process using S3 object response = client.detect_document_text( Document={'S3Object': {'Bucket': bucket, 'Name': document}}) #Get the text blocks blocks=response['Blocks'] return { 'statusCode': 200, 'body': json.dumps(blocks) }
  3. Choose Save to save your Lambda function.

Step 5: Test your Lambda function (console)

  1. Select Test.

  2. Enter a value for Event name.

  3. Choose Create.

  4. Choose Test. The lambda function is invoked. The output is displayed in the Execution results pane of the code editor. The output is a list of Block objects.

If the AWS Lambda function returns a timeout error, a call to an Amazon Textract API operation might be the cause. For information about extending the timout period for an AWS Lambda function, see AWS Lambda Function Configuration.

For information about invoking a Lambda function from your code, see Invoking AWS Lambda Functions.