AWS Logo
Menu
Connecting Farm APIs to Bedrock Agents with OpenAPI Schemas: Soil pH Case

Connecting Farm APIs to Bedrock Agents with OpenAPI Schemas: Soil pH Case

Building a Soil pH Analysis Demo with Bedrock Agents using OpenAPI schemas

Nitin Eusebius
Amazon Employee
Published Jan 24, 2025
In my previous post, we explored building a smart farming system using Amazon Bedrock's multi-agent collaboration. Now let's continue by examining OpenAPI schemas for your agent's action groups.
When you create an action group in Amazon Bedrock, you must define the parameters that the agent needs to invoke from the user. You can also define API operations that the agent can invoke using these parameters. To define the API operations, create an OpenAPI schema in JSON or YAML format. You can create OpenAPI schema files and upload them to Amazon Simple Storage Service (Amazon S3). Alternatively, you can use the OpenAPI text editor in the console, which will validate your schema. After you create an agent, you can use the text editor when you add an action group to the agent or edit an existing action group. For more information, see Modify an agent.
The agent uses the schema to determine the API operation that it needs to invoke and the parameters that are required to make the API request. These details are then sent through a Lambda function that you define to carry out the action or returned in the response of the agent invocation.
Lets deep dive into a demonstration showcasing Bedrock agents' capabilities through a focused soil pH analysis system. This example illustrates how to structure OpenAPI schemas, Lambda functions, and agent configurations for API integration.
Note: This demo uses simplified data and architecture. Production implementations would include:
  • Database integration
  • Authentication/Authorization
  • Input validation
  • Error handling
  • Logging/Monitoring
  • Additional soil metrics
  • Data persistence
Code and configuration provided serve as a starting point for building more comprehensive agriculture solutions with Bedrock agents. Lets get started

Prerequisites

To follow along with this tutorial, you'll need:

Step 1 :

Execute the following cloudformation script
Save the below yaml as agents_api_gateway.yaml
Deploy CloudFormation:
Get API URL:
Verify the 2 lambda functions created
getSoilPH : This lambda function is called via api gateway
  • Main Lambda function that stores and serves pH values for fields in a hardcoded dictionary for demo. In real world you will have this information coming from a database for instance.
  • Returns pH value when given a field ID via API Gateway, with proper HTTP status codes and error handling
soilPHActionGroup : This lambda function is where OpenAPI schema details are then sent through a Lambda function that you define to carry out the action or returned in the response of the agent invocation
  • Bedrock agent's Lambda that receives parameters from the agent and forwards requests to the main API
  • Handles URL construction, makes HTTP calls, and formats responses in the structure required by Bedrock agents

Step 2 :

Now lets create our Agent
  • Go to AWS Console → Bedrock → Agents
  • Click "Create Agent"
  • Name: "SoilPHAnalyzer"
  • Model: Claude 3.5 Haiku
  • Instructions:
Or you can use this
Add Action Group:
  • Name: "SoilActions"
  • Description: "Soil pH analysis operations"
Under Action group type select Define with API schemas
Under Action group invocation select Select an existing Lambda function and select soilPHActionGroup lambda function
Select Define via in-line schema editor and paste the following schema
OpenAPI schema
Click Create --> Save & Exit and then Prepare

Step 3 :

What's the pH level for field1?
Is field2 acidic or alkaline?
Tell me about field3's soil pH
Lets also test it with some ambiguous question, as you can see it prompts you to provide the field id
I want to know if this soil is acidic?
Best practices for soybeans
In this demonstration, we built a focused Bedrock agent that retrieves and interprets soil pH data through OpenAPI schemas and Lambda functions. The example shows how Bedrock agents can orchestrate API interactions while maintaining strict boundaries around their capabilities.
Key takeaways:
  • Agents excel at focused, well-defined tasks
  • OpenAPI schemas define clear parameter structures
  • Lambda integration enables flexible backend processing
  • Clear agent instructions prevent scope creep
This foundation can be expanded for production agriculture systems incorporating databases, security, and additional soil metrics. Happy Building !
 

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

Comments