1
0
mirror of synced 2026-05-22 14:43:35 +00:00
Files
amazon-bedrock-agentcore-sa…/04-infrastructure-as-code/README.md
T
David Kaleko 89891fcba5 Add AWS CDK implementation for existing CFN examples (#536)
* feat: Add AWS CDK implementation for basic AgentCore runtime deployment

This commit introduces a comprehensive CDK alternative to the existing CloudFormation
basic-runtime sample, providing a cleaner and more maintainable Infrastructure as Code
approach for deploying Amazon Bedrock AgentCore resources.

- **Complete CDK stack** (`basic_runtime_stack.py`) with proper construct separation
- **Dedicated IAM role construct** (`infra-utils/agentcore_role.py`) for reusability
- **Custom Lambda function** (`infra-utils/build_trigger_lambda.py`) for CodeBuild automation
- **S3 asset-based source packaging** eliminating Docker dependency for users
- **ARM64 CodeBuild integration** with automated container image building
- **Comprehensive documentation** matching CloudFormation sample structure

- Uses S3 assets instead of inline code for better maintainability
- Separates infrastructure utilities into dedicated `infra-utils/` directory
- Implements proper CDK patterns with construct separation
- Provides cleaner deployment experience (~5-10 min vs ~10-15 min)

- **Basic Strands agent** (`agent-code/basic_agent.py`) with simple Q&A functionality
- **ARM64 Dockerfile** optimized for AgentCore runtime requirements
- **Proper dependency management** with isolated requirements

- Updated title to reflect both CloudFormation and CDK options
- Added comprehensive CDK section with architecture highlights
- Included CDK prerequisites with version requirements (CDK 2.218.0+)
- Updated repository structure to show new CDK directory layout
- Added installation commands for required CDK dependencies

- Complete documentation following CloudFormation sample structure
- Detailed prerequisites, deployment, testing, and cleanup instructions
- Sample queries and troubleshooting sections
- Architecture explanation and use case descriptions

- **CDK 2.218.0+** required for BedrockAgentCore construct support
- **Python 3.8+** and **constructs>=10.0.79** for proper CDK functionality
- **S3 assets** for source code packaging without size limitations

- ECR repository for container image storage
- CodeBuild project with ARM64 support for automated builds
- Lambda function for build orchestration and completion waiting
- AgentCore Runtime with proper IAM permissions and networking
- Custom resource for deployment automation

-  Successfully deployed and tested in AWS environment
-  Verified agent functionality with sample queries
-  Confirmed clean resource cleanup with `cdk destroy`

- Added David Kaleko to CONTRIBUTORS.md

This implementation provides a modern, maintainable alternative to CloudFormation
while maintaining feature parity and following AWS CDK best practices.

* fix: Resolve CDK Lambda import issues and reorganize infrastructure utilities

This commit fixes critical Lambda function import errors that were preventing
the CDK stack deployment from completing, and reorganizes the infrastructure
utilities for better Python module compatibility.

- **Root cause**: `cfnresponse` module is only available for inline CloudFormation
  Lambda code, not when using CDK's `Code.from_asset()` approach
- **Solution**: Embedded the standard AWS-provided cfnresponse functionality
  directly into the Lambda function to eliminate import dependencies
- **Impact**: Custom resource now properly signals CloudFormation completion/failure

- **Renamed**: `infra-utils/` → `infra_utils/` for proper Python module imports
- **Fixed**: Lambda handler path to use correct Python module notation
- **Updated**: Import statements to use underscore-based directory name

- Embedded cfnresponse class with SUCCESS/FAILED constants and send() method
- Added comprehensive comments explaining why local cfnresponse is necessary
- Maintains full compatibility with CloudFormation custom resource protocol
- Proper error handling and CloudWatch logging integration

- Updated Lambda handler path: `infra_utils.build_trigger_lambda.handler`
- Fixed import statements for renamed directory structure
- Removed conditional BedrockAgentCore imports (always available in CDK 2.218.0+)

- Moved infrastructure utilities to properly named Python package
- Added package `__init__.py` for proper module structure
- Maintained clean separation between infrastructure and agent code

-  Resolves hanging CloudFormation deployments
-  Custom resource now properly waits for CodeBuild completion
-  Stack deployment completes successfully end-to-end
-  Maintains compatibility with existing CloudFormation approach

- Verified Lambda function executes without import errors
- Confirmed CodeBuild triggering and monitoring functionality
- Validated complete stack deployment cycle

This fix ensures the CDK implementation works reliably and follows Python
packaging best practices while maintaining the same deployment behavior
as the CloudFormation equivalent.

* Minor README update

* Dockerfile updates including a health check to fix all ASH security scan warnings

* Readme updates in accordance with PR feedback

* feat: Add CDK implementation for end-to-end weather agent

- Complete CDK stack for weather-based activity planning agent
- Includes browser tool, code interpreter, memory, and S3 storage
- Fixed IAM permissions for bedrock-agentcore services
- Added proper CloudFormation response handling for custom resources
- Comprehensive documentation with deployment and testing instructions
- Production-ready infrastructure with monitoring and best practices

* Add CDK implementation for multi-agent runtime

Convert CloudFormation multi-agent-runtime example to CDK with:
- Dual agent architecture (orchestrator + specialist)
- Agent-to-agent communication via bedrock-agentcore:InvokeAgentRuntime
- Separate ECR repos and CodeBuild projects for each agent
- IAM roles with proper cross-agent invocation permissions
- Custom resource Lambda for build triggering
- Comprehensive documentation and test script

Tested and validated: orchestrator correctly delegates complex queries to specialist agent while handling simple queries directly.

* README updates to make sure CDK readmes parallel that of cloudformation, copied architecture diagrams over because they're the same

* Add CDK implementation for MCP server AgentCore runtime

Convert CloudFormation mcp-server-agentcore-runtime example to CDK with:
- MCP server with FastMCP and three tools (add_numbers, multiply_numbers, greet_user)
- Cognito JWT authentication with pre-created test user
- ECR repository and CodeBuild project for ARM64 Docker image
- IAM roles with proper permissions for MCP protocol
- Custom Lambda functions for build triggering and password setting
- Architecture diagram and comprehensive documentation
- Test scripts for authentication and MCP tool validation

Tested and validated: MCP server successfully deployed with JWT auth, all three tools working correctly via MCP client.

* Reorganizing READMEs to avoid duplication, top level IaC README describes each example only once then links to both CFN and CDK versions of each

* Python linting fixes

---------

Signed-off-by: David Kaleko <5712203+kaleko@users.noreply.github.com>
2025-11-06 14:28:30 -03:00

4.4 KiB

Infrastructure as Code Samples for Amazon Bedrock AgentCore

Deploy Amazon Bedrock AgentCore resources using CloudFormation templates or AWS CDK.

Overview

These Infrastructure as Code samples enable you to:

  • Deploy AgentCore resources consistently across environments
  • Automate infrastructure provisioning with Infrastructure as Code
  • Maintain version control of your infrastructure
  • Implement AWS best practices for security and monitoring

Choose your preferred approach:

  • CloudFormation - YAML/JSON templates for declarative infrastructure
  • CDK - Python code for programmatic infrastructure

Samples

1. Basic Agent Runtime

Deploy a simple AgentCore Runtime with a basic Strands agent - no additional tools or memory.

What it deploys:

  • AgentCore Runtime with simple agent
  • ECR Repository and automated Docker builds
  • IAM roles with least-privilege policies

Use case: Learning AgentCore basics without complexity
Deployment time: ~5-15 minutes
Estimated cost: ~$50-100/month

Implementation: CloudFormation | CDK

2. MCP Server on AgentCore Runtime

Deploy a complete MCP (Model Context Protocol) server with automated Docker building and JWT authentication.

What it deploys:

  • AgentCore Runtime hosting MCP server
  • Amazon Cognito for JWT authentication
  • Automated ARM64 Docker builds

Sample MCP Tools: add_numbers, multiply_numbers, greet_user
Deployment time: ~10-15 minutes
Estimated cost: ~$50-100/month

Implementation: CloudFormation | CDK

3. Multi-Agent Runtime

Deploy a multi-agent system where Agent1 (orchestrator) can invoke Agent2 (specialist) for complex tasks.

What it deploys:

  • Two AgentCore Runtimes with agent-to-agent communication
  • IAM roles with agent-to-agent invocation permissions
  • Separate ECR repositories for each agent

Architecture: Agent1 routes requests and delegates to Agent2 for detailed analysis
Deployment time: ~15-20 minutes
Estimated cost: ~$100-200/month

Implementation: CloudFormation | CDK

4. End-to-End Weather Agent with Tools and Memory

Deploy a complete weather-based activity planning agent with browser automation, code interpreter, and memory.

What it deploys:

  • AgentCore Runtime with Strands agent
  • Browser Tool for web scraping weather data
  • Code Interpreter Tool for weather analysis
  • Memory for storing user preferences
  • S3 bucket for results storage

Features: Scrapes weather.gov, analyzes conditions, stores preferences, generates recommendations
Deployment time: ~15-20 minutes
Estimated cost: ~$100-150/month

Implementation: CloudFormation | CDK

Prerequisites

Before deploying any sample, ensure you have:

  1. AWS Account with appropriate permissions
  2. AWS CLI installed and configured
  3. Access to Amazon Bedrock AgentCore (preview)
  4. IAM Permissions to create:
    • CloudFormation stacks (for CloudFormation samples)
    • IAM roles and policies
    • ECR repositories
    • Lambda functions
    • AgentCore resources
    • S3 buckets (for weather agent)

For CDK samples, also install:

  • Python 3.8+
  • AWS CDK v2.218.0 or later

Repository Structure

04-infrastructure-as-code/
├── README.md                          # This file
├── cloudformation/                    # CloudFormation samples
│   ├── README.md                      # CloudFormation-specific guide
│   ├── basic-runtime/
│   ├── mcp-server-agentcore-runtime/
│   ├── multi-agent-runtime/
│   └── end-to-end-weather-agent/
└── cdk/                              # CDK samples
    ├── README.md                     # CDK-specific guide
    ├── basic-runtime/
    ├── mcp-server-agentcore-runtime/
    ├── multi-agent-runtime/
    └── end-to-end-weather-agent/

Additional Resources