Legal Hazard: Building an AI Contract Risk Reader with Textract, Bedrock and Step Functions
Built a serverless tool on AWS that reads contracts and flags risky clauses in plain language, including the API timeout, model switch and Amplify problems I ran into along the way.
Most of us have signed something we didn't fully understand. A rental agreement, an offer letter, a freelance contract. Hiring a lawyer for every document isn't realistic, and reading dense legal text the night before a deadline isn't a great system either.
I built Legal Hazard for the AWS Bharat Builds Tour "First Commit" hackathon to close that gap. You upload a PDF and it tells you which clauses look risky and why, in plain language. It is not legal advice, and the site says so clearly. The goal is to help you understand what you are about to sign, so you know when to ask a professional.
Live site: https://main.d1guinljtydfid.amplifyapp.com
GitHub: https://github.com/ParthMishra20/legal-hazard
What it does
You upload a PDF (up to 10 MB) and watch a short processing animation. Then you get:
- An overall risk verdict: High, Medium or Low
- A summary paragraph explaining the verdict
- Each flagged clause, quoted exactly from your document, with a plain-language explanation of why it matters
Architecture
The whole thing is serverless.
- AWS Amplify Hosting serves the static frontend, written in plain HTML, CSS and JavaScript with no framework.
- Amazon API Gateway (HTTP API) exposes two endpoints: POST /analyze and GET /status.
- An upload Lambda checks that the file is a PDF under 10 MB, saves it to Amazon S3 (public access blocked), and starts a Step Functions execution.
- AWS Step Functions runs two steps in order. First, a Lambda calls Amazon Textract to extract the text. Second, a Lambda calls Amazon Bedrock with Amazon Nova Lite through the Converse API to analyze the text and return structured JSON.
- The result is saved to Amazon DynamoDB.
- A status Lambda checks the Step Functions execution, and the frontend polls it every three seconds until the result is ready.
Why polling? The full pipeline takes 15 to 45 seconds, and API Gateway has a hard 29-second timeout. So the frontend can't send one request and wait. POST /analyze returns immediately with an execution ARN, and the frontend polls /status until it sees SUCCEEDED or FAILED. This one limit shaped the whole design.
Making the output dependable
A tool like this is only useful if its output is consistent, so the analysis step is built around a strict contract:
- The model must return one JSON object with a fixed schema: an overall risk level, a summary, and a list of flagged clauses.
- Every flagged clause must quote an exact passage from the document, which keeps the results tied to what the contract actually says.
- Temperature is set to 0 so the same document gets the same analysis.
- Before anything is saved, the Lambda validates the result: allowed risk levels, required fields, and non-empty clause text and explanations.
- The product never tells the user that a clause is legally valid or invalid. It only explains why a clause may matter.
What fought back
1. The Anthropic model on Bedrock was blocked. I started with Claude 3 Haiku, but the AWS Marketplace subscription failed with an INVALID_PAYMENT_INSTRUMENT error. I switched to Amazon Nova Lite (apac.amazon.nova-lite-v1:0). Its request and response formats differ from Claude's, and it sometimes wrapped its JSON in extra text. I added parsing that strips code fences and takes everything from the first { to the last }.
2. Two Step Functions integration bugs. One state passed a JSON string where the next state expected parsed JSON. Another lost the document_id, which silently overwrote rows in DynamoDB. The Step Functions execution view showed exactly which state was failing, which made both bugs quick to find.
3. The Amplify Gen 1 CLI refused my project. amplify init first failed because the CLI couldn't read my IAM Identity Center profile, which I fixed with a credential_process profile. It then stopped with a message that Gen 1 is in maintenance mode and no longer accepts new customers. For a static site I didn't need the CLI at all, so I deployed through the Amplify Console with a drag-and-drop zip.
4. The overall verdict didn't match its own clauses. On the freelance contract, the overall verdict came back as Medium even though every flagged clause was rated High. A verdict that contradicts its own evidence undermines the whole tool, so I added a check in code: after validation, the overall level is raised if it is lower than the most severe flagged clause. It never lowers a verdict. After redeploying, the same contract shows High.
Testing
I tested three documents against the live site: a one-page rental agreement, a one-page offer letter, and a three-page freelance contract that includes a payment table. The freelance contract was the messiest, and Textract still extracted it cleanly. Nova Lite flagged the no-notice termination, the sweeping IP assignment and the one-sided indemnification, each with a specific explanation and not a generic warning.
Cost and security choices
- Access uses IAM Identity Center, so I never created long-lived access keys.
- Each Lambda has its own IAM role with limited permissions.
- DynamoDB uses on-demand billing, so there is no capacity to plan.
Limitations
- Only the first 18,000 characters of extracted text are analyzed, so very long contracts are cut off.
- All three test documents were digital PDFs. I have not yet tested scanned or photographed documents.
- The analysis comes from a language model, so it can miss clauses or over-flag ones that are normal. That is why the product frames itself as a reading aid and not legal advice.
What I learned
- Design around service limits early. The 29-second API Gateway timeout decided my architecture.
- Treat model output as untrusted input. Validate it, and enforce any guarantee you care about in code.
- A managed pipeline is much easier to debug when you can see which step failed.
- Not every tool needs a CLI. The Amplify Console was the right choice for a static site.
What's next
Support for scanned documents, chunking so long contracts are analyzed in full, and more languages, since contracts in India often mix English with regional languages.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article