docs/sf/providers/aws/guide/agents/dev.md
Run your AI agent locally with automatic AWS credential management, hot reload, and an interactive chat interface.
serverless dev
Dev mode runs your agent on your local machine while using the deployed IAM role for AWS permissions. This lets you iterate on agent code without redeploying, while still accessing all deployed AWS resources (gateway tools, memory, Bedrock models, etc.).
1. Deploy your agent first (creates the IAM role and cloud resources):
serverless deploy
2. Start dev mode:
serverless dev
3. Chat with your agent -- once the agent is ready, you'll see:
Dev mode running on http://localhost:8080
Session ID: a1b2c3d4-...
Type your message and press Enter to chat with the agent.
Press Ctrl+C to stop.
You: What can you help me with?
Agent:
I'm an AI assistant that can help you with...
You:
Edit your agent code, save the file, and dev mode automatically rebuilds and restarts.
When you run serverless dev, the framework:
serverless dev
│
├── Read CloudFormation stack outputs (Role ARN, Gateway URL, Memory ID)
├── Update IAM trust policy for local AssumeRole
├── Get STS temporary credentials (60 min)
│
├── [Docker Mode] Build image → Run container on port 8080
│ OR
├── [Code Mode] Spawn Python process on PORT
│
├── Start file watcher
└── Start interactive chat CLI
│
├── User types message
├── HTTP POST http://localhost:8080/invocations
├── Agent responds (SSE stream or JSON)
└── Display response
Your agent runs locally and calls AWS services directly using the injected temporary credentials -- there is no tunnel or cloud proxy.
# Auto-detects the first runtime agent
serverless dev
# Specify which agent to run (required when multiple agents are defined)
serverless dev --agent myAgent
# Use a custom port (default: 8080)
serverless dev --port 9000
# Force agents dev mode (see note below)
serverless dev --agents
--agents flagWhen your serverless.yml defines both Lambda functions and agents, serverless dev defaults to Lambda functions
dev mode. Use --agents to explicitly select agents dev mode:
# This runs Lambda dev mode (default when functions exist)
serverless dev
# This runs agents dev mode
serverless dev --agents
If your configuration only has agents (no functions), agents dev mode is selected automatically.
Dev mode supports two execution modes, detected automatically based on your project configuration.
| Priority | Condition | Mode |
|---|---|---|
| 1 | artifact.image is configured | Docker |
| 2 | handler is set (no image config) | Code (Python only) |
| 3 | Dockerfile exists in project root | Docker |
| 4 | Default (image auto-creation) | Docker |
Builds a Docker image locally and runs it in a container. This is the default mode for most projects.
ai:
agents:
myAgent: {} # Dockerfile auto-detected
<service>-<agent>:localRuns the Python process directly without Docker. Faster startup and iteration, ideal for rapid development.
ai:
agents:
myAgent:
handler: agent.py
runtime: python3.13
.py files for changesImportant: Your handler must read the PORT environment variable:
if __name__ == "__main__":
port = int(os.getenv('PORT', 8080))
app.run(port=port, host='0.0.0.0')
Dev mode automatically discovers deployed cloud resources from your CloudFormation stack and injects them as environment variables. This means your local agent connects to the same gateway tools, memory, and other resources as the deployed version.
| Resource | Environment Variable | When Injected |
|---|---|---|
| Gateway URL | BEDROCK_AGENTCORE_GATEWAY_URL | If gateway/tools are deployed |
| Memory ID | BEDROCK_AGENTCORE_MEMORY_ID | If memory is configured on the agent |
This happens automatically -- no manual configuration needed. When you run serverless dev, the framework reads the
CloudFormation stack outputs and injects the values.
Dev mode manages AWS credentials automatically through the deployed IAM role.
Trust policy setup -- Dev mode adds a ServerlessAgentCoreLocalDevPolicy statement to the agent's IAM role trust
policy, allowing your local AWS identity to assume the role. This handles SSO sessions, assumed roles, and regular
IAM users.
STS AssumeRole -- Obtains temporary credentials (AccessKeyId, SecretAccessKey, SessionToken) with 60-minute expiration.
Auto-refresh -- When credentials have less than 10 minutes remaining and a file change triggers a rebuild, credentials are automatically refreshed.
Retry logic -- Credential acquisition retries up to 10 times with exponential backoff, handling IAM propagation delays.
For Python code mode, a virtual environment is strongly recommended:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
serverless dev
This prevents boto3 from reading your system ~/.aws/config or SSO cache, ensuring the agent uses only the injected
temporary credentials. Dev mode detects the virtual environment automatically via the VIRTUAL_ENV environment
variable.
Dev mode injects the following environment variables into your agent process/container:
| Variable | Description |
|---|---|
AWS_ACCESS_KEY_ID | Temporary STS credentials |
AWS_SECRET_ACCESS_KEY | Temporary STS credentials |
AWS_SESSION_TOKEN | Temporary STS credentials |
AWS_REGION | From provider configuration |
AWS_DEFAULT_REGION | Same as AWS_REGION |
AGENTCORE_DEV_MODE | Always 'true' -- use to detect dev mode in your agent code |
PYTHONUNBUFFERED | Always '1' -- ensures real-time log output |
SLS_SERVICE | Service name from serverless.yml |
SLS_STAGE | Current stage |
SLS_AGENT | Agent name |
| Variable | Mode | Description |
|---|---|---|
PORT | Code only | Port number your handler should listen on |
| Variable | Description |
|---|---|
BEDROCK_AGENTCORE_GATEWAY_URL | Gateway endpoint URL |
BEDROCK_AGENTCORE_MEMORY_ID | Memory resource ID |
Any variables defined in environment in your serverless.yml are also injected:
ai:
agents:
myAgent:
environment:
MODEL_ID: us.anthropic.claude-sonnet-4-20250514-v1:0
MY_API_KEY: ${ssm:/my/api/key}
Dev mode provides a built-in CLI for chatting with your local agent.
You: promptCtrl+C for graceful shutdown (stops container/process, cleans up)The chat CLI sends HTTP POST requests to your local agent:
POST http://localhost:<port>/invocations
Content-Type: application/json
Accept: text/event-stream
X-Amzn-Bedrock-AgentCore-Runtime-Session-Id: <session-uuid>
{ "prompt": "Your message here" }
Your agent can respond with SSE streams or plain JSON.
Dev mode monitors your source files and automatically rebuilds/restarts on changes.
.py files in the projectThe following are always excluded from file watching:
node_modules/, .git/, .serverless/venv/, .venv/__pycache__/, *.pyc.pytest_cache/, .mypy_cache/, coverage/*_test.py, *.test.py, *.test.js, *.spec.jsDev mode converts the runtime configuration to a Python command:
| Runtime Config | Python Command |
|---|---|
python3.13 | python3.13 |
python3.12 | python3.12 |
If the installed Python version doesn't match the configured runtime, dev mode logs a warning.
On Windows, python.exe is used regardless of the runtime configuration.
A virtual environment is strongly recommended for code mode:
python3 -m venv venv
source venv/bin/activate # Linux/macOS
# or: venv\Scripts\activate # Windows
pip install -r requirements.txt
Dev mode detects the virtual environment via the VIRTUAL_ENV environment variable and:
bin/ directory to PATHVIRTUAL_ENV and VIRTUAL_ENV_PROMPT to the Python processmy-agent/
├── agent.py # Entry point (handler)
├── requirements.txt # Dependencies
├── serverless.yml # Configuration
└── venv/ # Virtual environment (recommended)
You must deploy the agent before using dev mode. The IAM role and cloud resources must exist:
serverless deploy
The agent is still starting up. Wait a few seconds for the container/process to initialize and begin listening on the port.
Dev mode detected a different Python version than configured. Install the correct version or update the runtime in
serverless.yml:
# Check installed version
python3.13 --version
# Or update serverless.yml
ai:
agents:
myAgent:
runtime: python3.12 # Match your installed version
IAM trust policy changes take a few seconds to propagate. Dev mode waits 10 seconds automatically, but in rare cases you may need to retry. Dev mode retries up to 10 times with exponential backoff.
If your agent uses system-level AWS credentials instead of the injected ones, activate a virtual environment:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
serverless dev
This isolates boto3 from ~/.aws/config and SSO session caches.
Dev mode does not auto-restart after crashes. Fix the issue in your code, and dev mode will rebuild automatically when you save the file.