
Deploying Amazon Bedrock AgentCore Runtime in a VPC: A Step-by-Step Guide
This tutorial demonstrates how to deploy Amazon Bedrock AgentCore Runtime agents in a VPC with secure access to private resources. Using a simple calculator agent example, you'll learn to set up VPC infrastructure with NAT Gateway for internet access, configure network security, and deploy agents that can communicate with both private resources and AWS services like Bedrock and CloudWatch.
Introduction
Amazon Bedrock AgentCore Runtime provides a server-less platform for deploying AI agents at scale. While AgentCore can run in AWS-managed infrastructure, many organizations require their agents to access private resources like databases, internal APIs, or services within their Virtual Private Cloud (VPC). In this post, I'll walk you through deploying an AgentCore Runtime agent in a VPC with full internet access, enabling secure communication with both private resources and public APIs.
Why Deploy AgentCore in a VPC?
Deploying AgentCore Runtime in a VPC offers several key benefits:
- Secure access to private resources: Connect to Amazon RDS databases, ElastiCache clusters, or internal APIs without exposing them to the internet
- Network isolation: Keep your agent's network traffic within your organization's security boundaries
- Compliance requirements: Meet regulatory requirements that mandate private network connectivity
- Cost optimization: Use VPC endpoints to reduce data transfer costs for AWS service calls
Architecture Overview
Our deployment architecture includes:
- VPC : A dedicated Virtual Private Cloud with DNS resolution enabled
- Private Subnets : Two private subnets across different Availability Zones for high availability
- Public Subnet: Hosts the NAT Gateway for outbound internet access
- NAT Gateway : Enables agents in private subnets to access AWS services and the internet
- Internet Gateway : Provides internet connectivity for the NAT Gateway
- Security Group : Controls outbound traffic from the agent (HTTPS to AWS services)
- IAM Execution Role : Grants the agent permissions to invoke Bedrock models and write CloudWatch logs
NAT Gateway vs VPC Endpoints
This tutorial uses a NAT Gateway for simplicity and flexibility. Alternatively, you can use VPC endpoints if your agent only needs AWS service access (no public internet). We'll briefly cover this option later in the post.
Prerequisites
Before you begin, ensure you have:
- AWS CLI installed and configured with appropriate credentials
- Python 3.9 or later
-
pippackage manager
- Sufficient IAM permissions to create VPC resources, IAM roles, and AgentCore runtimes
- The
agentcore-starter-toolkitinstalled:pip install bedrock-agentcore-starter-toolkit
Step 1: Create Your Agent Code
First, let's create a simple calculator agent using the Strands framework . This agent will use the built-in calculator tool to perform mathematical operations.
Create a file
src/agent.py :"""Strands calculator agent with AgentCore Runtime integration."""
import osos.environ["BYPASS_TOOL_CONSENT"] = "true"
from strands import Agentfrom strands_tools import calculatorfrom bedrock_agentcore.runtime import BedrockAgentCoreApp
app = BedrockAgentCoreApp()
@app.entrypointdef agent_invocation(payload, context): """Handler for agent invocation. Args: payload: Request payload containing the prompt context: AgentCore context information Returns: dict: Response containing the agent's result """ # Extract prompt from payload user_message = payload.get("prompt", "No prompt provided") # Create agent with built-in calculator tool agent = Agent(tools=[calculator]) # Process the request result = agent(user_message) # Return structured response return {"result": result.message}
if __name__ == "__main__": app.run()
Create
src/requirements.txt with the necessary dependencies:strands-agentsstrands-agents-tools
Step 2: Set Up VPC Infrastructure
The VPC infrastructure setup is the most critical step. We need to create all networking components that enable secure, private connectivity while maintaining internet access for Bedrock API calls.
Key Infrastructure Components
DNS Configuration: AgentCore requires DNS hostnames and DNS support to be enabled in the VPC for proper service discovery and VPC endpoint resolution.
Subnet Strategy:
- Private subnets (2): Host the AgentCore Runtime network interfaces across different AZs for high availability
- Public subnet (1): Hosts the NAT Gateway
Internet Access Architecture:
- AgentCore in private subnets → NAT Gateway in public subnet → Internet Gateway → AWS Services
- This architecture enables AgentCore to access Bedrock (for models), and CloudWatch (for logs)
Here's the infrastructure setup script (
setup_infrastructure.sh ):#!/bin/bash# One-time infrastructure setup for AgentCore Runtime
set -e
# ConfigurationREGION="${AWS_REGION:-us-east-1}"PROJECT_NAME="${PROJECT_NAME:-strands-calculator}"VPC_CIDR="10.0.0.0/16"PUBLIC_SUBNET_CIDR="10.0.1.0/24"PRIVATE_SUBNET_1_CIDR="10.0.2.0/24"PRIVATE_SUBNET_2_CIDR="10.0.3.0/24"
# Get availability zonesAZ1=$(aws ec2 describe-availability-zones --region $REGION \ --query 'AvailabilityZones[0].ZoneName' --output text)AZ2=$(aws ec2 describe-availability-zones --region $REGION \ --query 'AvailabilityZones[1].ZoneName' --output text)
# Create VPCVPC_ID=$(aws ec2 create-vpc \ --cidr-block $VPC_CIDR \ --region $REGION \ --tag-specifications "ResourceType=vpc,Tags=[{Key=Name,Value=$PROJECT_NAME-vpc}]" \ --query 'Vpc.VpcId' \ --output text)
# Enable DNS (required for VPC endpoint resolution)aws ec2 modify-vpc-attribute --vpc-id $VPC_ID --enable-dns-hostnames --region $REGIONaws ec2 modify-vpc-attribute --vpc-id $VPC_ID --enable-dns-support --region $REGION
# Create Internet GatewayIGW_ID=$(aws ec2 create-internet-gateway \ --region $REGION \ --tag-specifications "ResourceType=internet-gateway,Tags=[{Key=Name,Value=$PROJECT_NAME-igw}]" \ --query 'InternetGateway.InternetGatewayId' \ --output text)aws ec2 attach-internet-gateway --vpc-id $VPC_ID --internet-gateway-id $IGW_ID --region $REGION
# Create SubnetsPUBLIC_SUBNET_ID=$(aws ec2 create-subnet \ --vpc-id $VPC_ID \ --cidr-block $PUBLIC_SUBNET_CIDR \ --availability-zone $AZ1 \ --region $REGION \ --tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=$PROJECT_NAME-public}]" \ --query 'Subnet.SubnetId' \ --output text)
PRIVATE_SUBNET_1_ID=$(aws ec2 create-subnet \ --vpc-id $VPC_ID \ --cidr-block $PRIVATE_SUBNET_1_CIDR \ --availability-zone $AZ1 \ --region $REGION \ --tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=$PROJECT_NAME-private-1}]" \ --query 'Subnet.SubnetId' \ --output text)
PRIVATE_SUBNET_2_ID=$(aws ec2 create-subnet \ --vpc-id $VPC_ID \ --cidr-block $PRIVATE_SUBNET_2_CIDR \ --availability-zone $AZ2 \ --region $REGION \ --tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=$PROJECT_NAME-private-2}]" \ --query 'Subnet.SubnetId' \ --output text)
# Create NAT GatewayEIP_ALLOC_ID=$(aws ec2 allocate-address \ --domain vpc \ --region $REGION \ --query 'AllocationId' \ --output text)
NAT_GW_ID=$(aws ec2 create-nat-gateway \ --subnet-id $PUBLIC_SUBNET_ID \ --allocation-id $EIP_ALLOC_ID \ --region $REGION \ --query 'NatGateway.NatGatewayId' \ --output text)
# Wait for NAT Gateway to become availableaws ec2 wait nat-gateway-available --nat-gateway-ids $NAT_GW_ID --region $REGION
# Create Route TablesPUBLIC_RT_ID=$(aws ec2 create-route-table \ --vpc-id $VPC_ID \ --region $REGION \ --query 'RouteTable.RouteTableId' \ --output text)aws ec2 create-route --route-table-id $PUBLIC_RT_ID \ --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW_ID --region $REGIONaws ec2 associate-route-table --route-table-id $PUBLIC_RT_ID \ --subnet-id $PUBLIC_SUBNET_ID --region $REGION
PRIVATE_RT_ID=$(aws ec2 create-route-table \ --vpc-id $VPC_ID \ --region $REGION \ --query 'RouteTable.RouteTableId' \ --output text)aws ec2 create-route --route-table-id $PRIVATE_RT_ID \ --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT_GW_ID --region $REGIONaws ec2 associate-route-table --route-table-id $PRIVATE_RT_ID \ --subnet-id $PRIVATE_SUBNET_1_ID --region $REGIONaws ec2 associate-route-table --route-table-id $PRIVATE_RT_ID \ --subnet-id $PRIVATE_SUBNET_2_ID --region $REGION
# Create Security GroupSG_ID=$(aws ec2 create-security-group \ --group-name "$PROJECT_NAME-agentcore-sg" \ --description "Security group for AgentCore Runtime" \ --vpc-id $VPC_ID \ --region $REGION \ --query 'GroupId' \ --output text)
# Allow HTTPS outbound (required for Bedrock API calls)aws ec2 authorize-security-group-egress \ --group-id $SG_ID \ --ip-permissions IpProtocol=tcp,FromPort=443,ToPort=443,IpRanges='[{CidrIp=0.0.0.0/0}]' \ --region $REGION
# Create IAM Execution RoleROLE_NAME="$PROJECT_NAME-agentcore-execution-role"
cat > /tmp/trust-policy.json <<EOF{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "bedrock-agentcore.amazonaws.com" }, "Action": "sts:AssumeRole" } ]}EOF
aws iam create-role \ --role-name $ROLE_NAME \ --assume-role-policy-document file:///tmp/trust-policy.json \ --region $REGION
# Attach required policiesaws iam attach-role-policy \ --role-name $ROLE_NAME \ --policy-arn arn:aws:iam::aws:policy/AmazonBedrockFullAccess \ --region $REGION
aws iam attach-role-policy \ --role-name $ROLE_NAME \ --policy-arn arn:aws:iam::aws:policy/CloudWatchLogsFullAccess \ --region $REGION
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)ROLE_ARN="arn:aws:iam::${ACCOUNT_ID}:role/${ROLE_NAME}"
# Wait for IAM role to propagatesleep 10
# Save configuration for deployment scriptcat > infrastructure.env <<EOFexport VPC_ID=$VPC_IDexport PRIVATE_SUBNET_1_ID=$PRIVATE_SUBNET_1_IDexport PRIVATE_SUBNET_2_ID=$PRIVATE_SUBNET_2_IDexport SECURITY_GROUP_ID=$SG_IDexport EXECUTION_ROLE_ARN=$ROLE_ARNexport REGION=$REGIONexport PROJECT_NAME=$PROJECT_NAMEEOF
echo "Infrastructure setup complete!"
Run the script:
chmod +x setup_infrastructure.sh./setup_infrastructure.sh
This creates all necessary infrastructure and saves the configuration to
infrastructure.env for use in the deployment step.Step 3: Deploy the Agent to AgentCore Runtime
Now that our infrastructure is ready, we can deploy the agent using the AgentCore CLI using direct code deployment . The deployment script (
deploy_agent.sh ) handles the entire process:#!/bin/bash# Agent deployment to AgentCore Runtime
set -e
# Load infrastructure configurationsource infrastructure.env
# Install dependenciespip install strands-agents strands-agents-tools bedrock-agentcore-starter-toolkit boto3
# Configure agent with VPC settingsagentcore configure \ --entrypoint src/agent.py \ --name calculator_agent \ --region $REGION \ --execution-role $EXECUTION_ROLE_ARN \ --vpc \ --subnets $PRIVATE_SUBNET_1_ID,$PRIVATE_SUBNET_2_ID \ --security-groups $SECURITY_GROUP_ID \ --disable-memory \ --non-interactive
# Launch agent to AgentCore Runtimeagentcore launch
echo "Agent deployment complete!"
Key deployment parameters:
-
--vpc: Enables VPC mode for the runtime
-
--subnets: Specifies the private subnets where network interfaces will be created
-
--security-groups: Applies the security group to control traffic
-
--disable-memory: Disables AgentCore Memory (optional, reduces costs for simple agents)
-
--execution-role: IAM role for Bedrock and CloudWatch access
Run the deployment:
chmod +x deploy_agent.sh./deploy_agent.sh
The deployment process takes 5-10 minutes as AWS provisions the container infrastructure, creates network interfaces in your VPC, and deploys your agent code.
Step 4: Test Your Agent
Once deployed, test your agent using the AgentCore CLI:
agentcore invoke '{"prompt": "What is 25 plus 17?"}'
Expected response:
{ "result": "25 plus 17 equals 42."}
You can also test more complex calculations:
agentcore invoke '{"prompt": "Calculate the square root of 144 and multiply it by 5"}'
Step 5: Monitor Your Agent
View real-time logs using CloudWatch:
aws logs tail /aws/bedrock/agentcore/runtime/src_agent --follow --region us-east-1
The logs show:
- Agent invocations
- Tool usage (calculator calls)
- Execution context
- Any errors or warnings
Understanding VPC Connectivity
How AgentCore Accesses Resources
When you deploy AgentCore in a VPC:
- Network Interfaces: AWS creates Elastic Network Interfaces (ENIs) in your specified private subnets
- Private IP Assignment: Each ENI receives a private IP address from your subnet CIDR range
- Outbound Traffic Flow:
- Agent → Private Subnet → NAT Gateway → Internet Gateway → Bedrock APIs
- Security Group Control: The security group acts as a firewall, controlling what the agent can access
Important Considerations
Public Subnets Don't Provide Internet Access: A common misconception is that placing AgentCore in a public subnet provides internet access. It doesn't. Always use private subnets with a NAT Gateway for internet connectivity.
DNS Resolution is Required: Enable DNS hostnames and DNS support in your VPC. AgentCore needs this for service discovery and VPC endpoint resolution.
Availability Zone Support: AgentCore only supports specific Availability Zones in each region. For us-east-1, use use1-az1, use1-az2, or use1-az4. Check the AgentCore VPC documentation for your region's supported AZs.
Connecting to Private Resources
To connect your agent to private resources like RDS databases:
1. Update Security Groups
Agent Security Group (outbound rule):
aws ec2 authorize-security-group-egress \ --group-id $SECURITY_GROUP_ID \ --ip-permissions IpProtocol=tcp,FromPort=3306,ToPort=3306,IpRanges='[{CidrIp=10.0.0.0/16}]'
RDS Security Group (inbound rule):
aws ec2 authorize-security-group-ingress \ --group-id $RDS_SECURITY_GROUP_ID \ --protocol tcp \ --port 3306 \ --source-group $SECURITY_GROUP_ID
2. Update Agent Code
import pymysql
@app.entrypointdef agent_invocation(payload, context): # Connect to private RDS instance connection = pymysql.connect( host='mydb.cluster-abc123.us-east-1.rds.amazonaws.com', user='admin', password=os.environ['DB_PASSWORD'], database='myapp' ) # Use the connection in your agent logic # ...
Alternative: Using VPC Endpoints Instead of NAT Gateway
Alternatively, if your agent doesn't need public internet access, you can replace the NAT Gateway with VPC endpoints. AgentCore needs to access ECR, S3, Bedrock, CloudWatch Logs, and the AgentCore API. You can create interface endpoints for these services (except S3, which uses a free gateway endpoint). This approach eliminates NAT Gateway costs but requires creating and managing multiple endpoints. VPC endpoints are ideal for production workloads with high traffic volumes where the lower data processing fees offset the endpoint costs. For details, see the AgentCore VPC endpoints documentation .
Cleanup
When you're done testing, clean up all resources to avoid ongoing charges:
#!/bin/bash# cleanup.sh
source infrastructure.env
# Delete AgentCore Runtimeagentcore delete --non-interactive
# Delete NAT Gatewayaws ec2 delete-nat-gateway --nat-gateway-id $NAT_GATEWAY_ID --region $REGIONaws ec2 wait nat-gateway-deleted --nat-gateway-ids $NAT_GATEWAY_ID --region $REGION
# Release Elastic IPEIP_ALLOC_ID=$(aws ec2 describe-addresses --region $REGION \ --filters "Name=tag:Project,Values=$PROJECT_NAME" \ --query 'Addresses[0].AllocationId' --output text)aws ec2 release-address --allocation-id $EIP_ALLOC_ID --region $REGION
# Delete remaining resourcesaws ec2 detach-internet-gateway --internet-gateway-id $IGW_ID --vpc-id $VPC_ID --region $REGIONaws ec2 delete-internet-gateway --internet-gateway-id $IGW_ID --region $REGIONaws ec2 delete-subnet --subnet-id $PUBLIC_SUBNET_ID --region $REGIONaws ec2 delete-subnet --subnet-id $PRIVATE_SUBNET_1_ID --region $REGIONaws ec2 delete-subnet --subnet-id $PRIVATE_SUBNET_2_ID --region $REGIONaws ec2 delete-security-group --group-id $SECURITY_GROUP_ID --region $REGIONaws ec2 delete-vpc --vpc-id $VPC_ID --region $REGION
# Delete IAM roleROLE_NAME="$PROJECT_NAME-agentcore-execution-role"aws iam detach-role-policy --role-name $ROLE_NAME \ --policy-arn arn:aws:iam::aws:policy/AmazonBedrockFullAccessaws iam detach-role-policy --role-name $ROLE_NAME \ --policy-arn arn:aws:iam::aws:policy/CloudWatchLogsFullAccessaws iam delete-role --role-name $ROLE_NAME --region $REGION
rm -f infrastructure.env
Run cleanup:
chmod +x cleanup.sh./cleanup.sh
Conclusion
Deploying Amazon Bedrock AgentCore Runtime in a VPC enables secure access to private resources while maintaining the serverless benefits of AgentCore. The architecture we've built provides:
- Security: Agents run in private subnets with controlled network access
- Reliability: Multi-AZ deployment ensures high availability
- Flexibility: Can access both private resources and public APIs
- Observability: CloudWatch integration for monitoring and debugging
Additional Resources
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article