Skip to content

This repository demonstrates how to build and deploy an AI agent using Amazon Bedrock and AWS Lambda, with infrastructure managed by AWS CDK in TypeScript. The AI agent is designed to interact with the National Park Service (NPS) API to fetch information about national parks based on user input, such as a state code.

License

Notifications You must be signed in to change notification settings

cebert/examples-ai-bedrock-agent-national-parks

Repository files navigation

AWS Bedrock Agent National Parks Service Example

This repository showcases how to use Amazon Bedrock to create AI agents that perform tasks on behalf of users. The agent enables users to ask natural language questions about national parks, processes these queries, and retrieves relevant information (when available) to provide accurate answers.

This project utilizes Amazon Bedrock, Bedrock Action Groups, Amazon CDK, and AWS Lambda while integrating with the U.S. National Parks Service API to deliver a realistic example. The AI agent demonstrates a simple task: making a single API call using Amazon Bedrock Actions. It does not employ advanced AI patterns such as routing, chaining, or parallelization.

AWS Bedrock support model support varies across regions. This project was tested deploying to the us-east-1 region, which currently has the most robust Bedrock Foundation Model (FM) support. See Model support by AWS Region in Amazon Bedrock.

If you plan to deploy this project yourself, I assume you have some basic knowledge of AWS Bedrock, AWS CDK, and AWS Lambda. If you don't have experience with these topics reading the additional resources included later on this page would be helpful.

Overview

This project showcases:

  • An AWS Bedrock Agent integration with an external API, such as the U.S. National Parks Service API
  • Using AWS CDK infrastructure as code (IaC) to deploy a Bedrock agent using the (currently) experimental AWS Generative AI Constructs Library
  • TypeScript Lambda function development

Features

The Bedrock Agent can:

  • Search for parks by state
  • Get detailed information about specific parks
  • Provide visitor information like operating hours and entrance fees

National Parks Service API

This project uses the National Parks Service API. Key endpoints used:

  • /parks - Get the list of parks
  • /parks/{parkCode} - Get specific park details

Please review the NPS API Terms of Service. You must request a free NPS API key to use this project in your AWS account.

Project Structure

├── bin/ # CDK entry point
│ ├── national-parks-bedrock-agent-example
├── lib/ # CDK IaC
| ├── schema # Contains an Open API 3.0.1 spec we share with the Action Group
│ ├── stacks/ # CDK stacks
│  ├── bedrock-agent-stack.ts # Deploys a Bedrock Agent, Action Group, and sets up IAM
│  ├── national-parks-api-stack.ts # Deploys Lambda that interacts with NPS API
├── src/
│ ├── clients/ # API clients
│ │ └── nps/ # National Parks Service API client directory
│ └── functions/ # Lambda functions
│ │ └── parks/ # Parks info Lambda
└── test/ # Tests (currently not used)

Using the Bedrock Agent

Once deployed, you can interact with your National Parks Agent through:

  1. AWS Console

    • Navigate to Amazon Bedrock in your AWS Console
    • Select "Agents" from the left sidebar
    • Find your agent named "national-parks-info-agent"
    • Click "Test" to open the playground
    • Type your questions in natural language Example test query:
  2. AWS SDK

    • Use the BedrockAgentRuntime client
    • Call the invoke operation
    • See AWS SDK documentation for language-specific examples

Example Queries

Try asking the agent questions like:

  • "What national parks are in Michigan?"
  • "Tell me about Keweenaw National Historical Park?"
  • "List the names of every park in Ohio"

Understanding Agent Capabilities

The agent can:

  • Search for parks in a specific state
  • Provide detailed information about individual parks including:
    • Description and location
    • Entrance fees
    • Operating hours
    • Directions
    • Weather information

System Flow

The following diagram illustrates how information flows through the system when a user interacts with the National Parks Agent:

flowchart LR
    User[User] -->|1. Ask about parks| Agent[Bedrock Agent]
    Agent -->|2. Process request| Lambda[Lambda Function]
    Lambda -->|3. Fetch data| NPS[National Parks API]
    NPS -->|4. Park info| Lambda
    Lambda -->|5. Format response| Agent
    Agent -->|6. Natural response| User
    %% Styling
    classDef aws fill:#FF9900,stroke:#232F3E,stroke-width:2px,color:white;
    classDef external fill:#85B09A,stroke:#232F3E,stroke-width:2px,color:white;
    classDef user fill:#3F8624,stroke:#232F3E,stroke-width:2px,color:white;
    class Agent,Lambda aws;
    class NPS external;
    class User user;
Loading

Costs

Remember you are charged for

  1. Bedrock Model invocations
  2. Lambda invocations
  3. CloudWatch logs

Deployment

This project leverages the experimental generative-ai-cdk-constructs library to easily configure and deploy AWS Bedrock and Action Groups. Several constructs in this library help make provisioning and deploying generative AI projects. Since this is an experimental library and this space is evolving, rapidly expect this library to change frequently.

Setup Steps

Prerequisites

  • Node.js (v22+)
  • National Parks Service API key
  • AWS Account with Bedrock access
    • Note: AWS accounts don't have access to FMs by default. You must request access to the model(s) you wish to use and experiment with. The process is easy. In personal accounts, I recommend requesting access to all available FMs at once. As of January 2025, the us-east-1 supports the most FMs. It's recommended that you run out of that region.
    • This project is currently using ANTHROPIC_CLAUDE_SONNET_V1_0 with on-demand pricing. You can feel free to experiment with different FMs.

Initial Setup

The following steps only need to be performed once for initial setup.

  1. Clone the repository:

    git clone https://github.com/cebert/examples-ai-bedrock-agent-national-parks.git
    cd examples-ai-bedrock-agent-national-parks
  2. Install dependencies:

    npm install
  3. Create a .env file or rename the file .env.example to .env and replace the placeholder environmental variable values with values specific to your AWS account. The .env file is excluded from git to prevent unintended secret disclosure:

    NPS_API_KEY=your_api_key_here
    AWS_ACCOUNT=your_aws_account_number
    AWS_REGION=us-east-1
    
  4. Using the AWS CLI, configure and authenticate with AWS using your SSO credentials:

    aws configure sso
  5. Follow the prompts to set up your SSO profile (e.g., bedrock-agent).

  6. The first time you deploy this stack, you'll need to run the following bootstrap command to prepare your AWS environment for CDK use:

    cdk bootstrap aws://<AWS_ACCOUNT_ID>/us-east-1 --profile <SSO_PROFILE_NAME>

Replace <AWS_ACCOUNT_ID> with your AWS account ID and <SSO_PROFILE_NAME> with your SSO profile name (e.g., bedrock-agent).

Building and Deploying

  1. Build the Project:

    npm run build
  2. Generate the CloudFormation template for your stack:

    npm run synth
  3. Deploy the CDK application to AWS:

       npm run deploy -- --profile <SSO_PROFILE_NAME> --all

Additional Resources

AWS Bedrock & Agents

Development Tools & SDKs

API Documentation

Model Information

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Feel free to fork this repository or use any of the code. I don't plan to actively maintain this example as an Open Source project.

About

This repository demonstrates how to build and deploy an AI agent using Amazon Bedrock and AWS Lambda, with infrastructure managed by AWS CDK in TypeScript. The AI agent is designed to interact with the National Park Service (NPS) API to fetch information about national parks based on user input, such as a state code.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published