Guide on AWS Textract set-up. On how to accurately process PDF files... | by Daulet Nurmanbetov | Towards Data Science

How to accurately process PDF files with OCR-as-a-service from AWS
Recently a new paradigm of async API responses has become prominent. It works by returning Job-ID rather than an API response. Then, to check the status, the user would need to submit a second call to the API with the Job ID. Here’s an excellent guide on it.
Async API response is typically used for heavyweight machine learning applications or applications that move large volumes of data, i.e. anything that would take forever to get the results synchronously back to the user.
AWS Textract can detect and analyze the text in multi-page documents that are in PDF format. Textract uses asynchronous responses for its API. Behind the scene, each PDF is separated into a single-page format and sent to the processing engine so that each page can be handled independently of the PDF document and the system can be scaled horizontally. Besides, the async operation allows the user to submit a PDF file with over 1,000 pages to be processed and to return later to check results rather than waiting on request.
Let’s go over the steps required to set up an EC2 machine to call Textract in Python:
1 — Set up an AWS role to access Amazon Textract from EC2 instance.
a) On the Create role page the service that will use this role — Select EC2 and go to Next: Permissions
b) We will need to grant the following 4 permissions to set up Textract — AmazonTextractFullAccessAmazonS3ReadOnlyAccessAmazonSNSFullAccessAmazonSQSFullAccess
(You may want to reduce access permissions once SNS, SQS and Textract configs have been set)
c) Skip Tags and go to Review. Call it something snappy.
d) Go to EC2 page and under Instances select the machine that will be calling Textract and attach the role you have created from a) like in the picture below —
2 — Log in to the EC2 Machine that you’ve added the role to and Install the required AWS SDK for Python — boto3
> pip install boto3
3 — Create an IAM service role to give Amazon Textract access to your Amazon SNS topics. This will allow Textract to notify you when the document is completed analyzing.  See Giving Amazon Textract Access to Your Amazon SNS Topic for detailed access rights. Save the Amazon Resource Name (ARN) of the service role as you will need to supply it to Textract.
Following the above reference, the role you create will indicate the following in the policy statement —
{  "Version": "2012-10-17",  "Statement": [    {      "Effect": "Allow",      "Principal": {        "Service": "ec2.amazonaws.com"      },      "Action": "sts:AssumeRole"    }  ]}
you will need to replace it with —
{  "Version": "2012-10-17",  "Statement": [    {      "Effect": "Allow",      "Principal": {        "Service": "textract.amazonaws.com"      },      "Action": "sts:AssumeRole"    }  ]}
4 — Run the following code in the EC2 instance to analyze PDFs with Textract. The PDFs must be located in your S3 bucket.
We’ve covered how to use AWS Textract on images and PDFs in your S3 via the code provided above.
Setting up Textract is a bit complex, but if you look at the code you will see why that is the case. The code creates both a new SQS Queue and a new SNS Topic and subscribes the queue to the topic we created.
Next, in the code we instruct Textract location of the desired PDF to be processed as well as the SNS Channel to communicate over.