How to Deploy an AI Agent with Amazon Bedrock AgentCore
Amazon Bedrock AgentCore is a managed service that makes it easier to build, deploy, and operate AI agents securely at scale on AWS. It works seamlessly with frameworks like Strands Agents, LangGraph, CrewAI, and LlamaIndex, while taking care of the complex tasks such as runtime management, IAM role configuration, and observability. In this guide, you’ll set up your environment, create and test a simple AI agent locally, deploy it with the AgentCore starter toolkit, and invoke it through the AWS SDK. Prerequisites Step 1: Set Up AWS CLI Step 2: Install and Create Your Agent Create a requirements.txt file Breaking Down the Code Step 3: Test the Agent Locally Step 4: Deploy to AgentCore Runtime Step 5: Invoke the Agent with AWS SDK Step 6: Clean Up Common Issues Conclusion Before you start, make sure you have: An AWS account with credentials configured. AWS CLI installed and working. Python 3.10 or later installed. Boto3 installed. Model access enabled in the Amazon Bedrock console (for example, Anthropic Claude Sonnet 4.0). First, install the AWS CLI if you do not already have it. On Linux or macOS: AWS CLI setup guide. Next, configure a profile with AWS SSO: You’ll be prompted to enter details such as: SSO start URL– the URL for your AWS organization’s IAM Identity Center portal. SSO region– the AWS region where IAM Identity Center is configured. Account ID– the AWS account you want to access. Role name– the IAM role you want to assume within that account. Default region– the region that will be used when making requests. Default output format– for example, This creates a new profile called Next, you have to verify your identity. Once your profile is configured, confirm that the CLI is correctly authenticating with AWS by running: This command returns details about your identity, including: Account– the AWS account ID you’re authenticated against. UserId– the unique identifier of your IAM role or user. Arn– the full Amazon Resource Name (ARN) of your identity. If the command succeeds and shows your account information, it means your profile is properly set up and ready to use with AWS SDKs, the AWS CLI, or services like Bedrock AgentCore. First, you need to set up Python virtual environment. This prevents dependency conflicts with other projects on your machine. Let’s create and activate a virtual environment: On macOS/Linux: On Windows (PowerShell or CMD): Once activated, your terminal prompt will show (.venv) at the beginning. To deactivate: List the dependencies your project needs by creating a file named This makes it easy to install everything at once with: Create a file called The The agent looks for a If no prompt is provided, it defaults to The Run the agent: Open another terminal and send a request: If successful, you will see: You can stop the agent with Ctrl+C. Now you are ready to deploy your agent to AWS. Configure the agent: This creates a configuration file called You can launch the deployment with this command: The output will include: The Amazon Resource Name (ARN) of your agent. The location of logs in Amazon CloudWatch. Test your deployed agent: If you get a joke back, your agent is running successfully. You can call your agent programmatically using Boto3. Create a file called Run the script: You should see the AI agent’s response. If you no longer want to run the agent, delete the runtime: Permission denied: Check your AWS credentials and IAM policies. Docker warning: Ignore this unless you use — local or — local-build. Model access denied: Enable model access (such as Claude Sonnet 4.0) in the Bedrock console. Build errors: Check CloudWatch build logs and IAM policies. Amazon Bedrock AgentCore makes it easy to create and deploy AI agents without dealing with complex container setups or infrastructure. You can test locally, launch to the cloud with one command, and monitor everything through CloudWatch. This workflow is ideal for developers who want to move from prototype to production quickly while staying inside the AWS ecosystem. Resources: https://strandsagents.com/latest/ https://aws.amazon.com/bedrock/agentcore/Prerequisites
Step 1: Set Up AWS CLI
aws configure sso --profile my-profilejson, yaml, or table.my-profilein your AWS CLI configuration, allowing you to use that identity to interact with AWS services.aws sts get-caller-identity --profile my-profileStep 2: Install and Create Your Agent
python3 -m venv .venvsource .venv/bin/activatepython -m venv .venv.venv\Scripts\activatepython -m venv .venv→ creates a virtual environment named .venvin your project folder..venv\Scripts\activate→ activates the environment.deactivateCreate a
requirements.txtfilerequirements.txtin the project root:bedrock-agentcorestrands-agentspip install -r requirements.txtmy_agent.pyand add the following code:from bedrock_agentcore import BedrockAgentCoreAppfrom strands import Agentapp = BedrockAgentCoreApp()# Create an agent with default settingsagent = Agent()@app.entrypointdef invoke(payload): """Your AI agent function""" user_message = payload.get("prompt", "Hello! How can I help you today?") result = agent(user_message) return { "result": result.message}if __name__ == "__main__": app.run()Breaking Down the Code
BedrockAgentCoreApp– the core runtime wrapper that handles configuration, execution, and integration with AWS services.Agent– a basic agent object from the Strands library that can process and respond to prompts.BedrockAgentCoreApp()creates the container application that manages your agent’s lifecycle.Agent()initializes a simple Strands agent with default settings. In a real-world case, you can customize this with specific tools, memory, or reasoning logic.@app.entrypointdecorator marks this function as the callable entry point for your agent. Whenever a request is sent to the agent (via the AWS SDK, CLI, or local test), this function is invoked."prompt"in the incoming payload."Hello! How can I help you today?".Agentobject then processes this input and generates a response.Step 3: Test the Agent Locally
python3 -u my_agent.pycurl -X POST http://localhost:8080/invocations \ -H "Content-Type: application/json" \ -d '{ "prompt": "Hello!"}'{ "result": "Hello! I'm here to help..."}Step 4: Deploy to AgentCore Runtime
agentcore configure -e my_agent.pybedrock_agentcore.yaml.agentcore launchagentcore invoke '{ "prompt": "tell me a joke"}'Step 5: Invoke the Agent with AWS SDK
invoke_agent.py:import jsonimport boto3agent_arn = "YOUR_AGENT_ARN"prompt = "Tell me a joke"agent_core_client = boto3.client("bedrock-agentcore")payload = json.dumps({ "prompt": prompt}).encode()response = agent_core_client.invoke_agent_runtime( agentRuntimeArn=agent_arn, payload=payload)content = []for chunk in response.get("response", []): content.append(chunk.decode("utf-8"))print(json.loads("".join(content)))python invoke_agent.pyStep 6: Clean Up
aws bedrock-agentcore delete-agent-runtime --agent-runtime-arn <your_arn>Common Issues
Conclusion
相关推荐
-
Pareto Chart Generator
-
How to Build CRUD Operations with .NET Core – A Todo API Handbook
-
How to Learn to Code and Get a Developer Job [Full Book]
-
How to Get Started with NodeJS – a Handbook for Beginners
-
Product Experimentation for Collaborative AI Features: Cluster Randomization for LLM
-
The Docker Handbook – Learn Docker for Beginners
- 最近发表
-
- How to Learn Python for JavaScript Developers [Full Handbook]
- How to Use Classes in JavaScript – A Handbook for Beginners
- Learn React – A Handbook for Beginners
- Learn React – A Handbook for Beginners
- How to Preprocess Medical Images for Machine Learning – A Guide Using Chest X
- The Golang Handbook – A Beginner's Guide to Learning Go
- The Docker Handbook – Learn Docker for Beginners
- How to Get Started with NodeJS – a Handbook for Beginners
- How to Become a Full
- The Python Handbook – Learn Python for Beginners
- 随机阅读
-
- Bansidhar Kadiya
- How to Build CRUD Operations with .NET Core – A Todo API Handbook
- Python Code Example Handbook – Sample Script Coding Tutorial for Beginners
- Learn React – A Handbook for Beginners
- Canvas API Tutorial and Examples
- Learn Clustering in Python – A Machine Learning Engineering Handbook
- How to Learn to Code and Get a Developer Job [Full Book]
- The Software Architecture Handbook
- How to Start your Career in Tech with freeCodeCamp
- The Java Handbook – Learn Java Programming for Beginners
- How to Use Classes in JavaScript – A Handbook for Beginners
- How to Get Started with NodeJS – a Handbook for Beginners
- Command Line for Beginners – How to Use the Terminal Like a Pro [Full Handbook]
- The Golang Handbook – A Beginner's Guide to Learning Go
- The Docker Handbook – Learn Docker for Beginners
- The Software Architecture Handbook
- software architecture
- Python Code Example Handbook – Sample Script Coding Tutorial for Beginners
- The Golang Handbook – A Beginner's Guide to Learning Go
- What is Programming? A Handbook for Beginners
- 搜索
-