Skip to content

Commit 5d7fb87

Browse files
feat(lambda): update lambda documentation regarding mcp and lambda layers (#472)
1 parent bdc2c4a commit 5d7fb87

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

docs/user-guide/deploy/deploy_to_aws_lambda.md

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,39 @@ def handler(event: Dict[str, Any], _context) -> str:
6363

6464
## Infrastructure
6565

66-
To deploy the above agent to Lambda using the TypeScript CDK, prepare your code for deployment by creating the Lambda definition and an associated Lambda layer ([`AgentLambdaStack.ts`][AgentLambdaStack]):
66+
To deploy the above agent to Lambda using the TypeScript CDK, prepare your code for deployment by creating the Lambda definition. You can use the official Strands Agents Lambda layer for quick setup, or create a custom layer if you need additional dependencies.
67+
68+
### Using the Strands Agents Lambda Layer
69+
70+
The fastest way to get started is to use the official Lambda layer, which includes the base `strands-agents` package:
71+
72+
```
73+
arn:aws:lambda:{region}:856699698935:layer:strands-agents-py{python_version}-{architecture}:{layer_version}
74+
```
75+
76+
**Example:**
77+
78+
```
79+
arn:aws:lambda:us-east-1:856699698935:layer:strands-agents-py3_12-x86_64:1
80+
```
81+
82+
| Component | Options |
83+
|-----------|---------|
84+
| **Python Versions** | `3.10`, `3.11`, `3.12`, `3.13` |
85+
| **Architectures** | `x86_64`, `aarch64` |
86+
| **Regions** | `us-east-1`, `us-east-2`, `us-west-1`, `us-west-2`, `eu-west-1`, `eu-west-2`, `eu-west-3`, `eu-central-1`, `eu-north-1`, `ap-southeast-1`, `ap-southeast-2`, `ap-northeast-1`, `ap-northeast-2`, `ap-northeast-3`, `ap-south-1`, `sa-east-1`, `ca-central-1` |
87+
88+
To check the size and details of a layer version:
89+
90+
```bash
91+
aws lambda get-layer-version \
92+
--layer-name arn:aws:lambda:{region}:856699698935:layer:strands-agents-py{python_version}-{architecture} \
93+
--version-number {layer_version}
94+
```
95+
96+
### Using a Custom Dependencies Layer
97+
98+
If you need packages beyond the base `strands-agents` SDK (such as `strands-agents-tools`), create a custom layer ([`AgentLambdaStack.ts`][AgentLambdaStack]):
6799

68100
```typescript
69101
const packagingDirectory = path.join(__dirname, "../packaging");
@@ -177,14 +209,67 @@ aws lambda invoke --function-name AgentFunction \
177209
jq -r '.' ./output.json
178210
```
179211

212+
## Using MCP Tools on Lambda
213+
214+
When using [Model Context Protocol (MCP)](../concepts/tools/mcp-tools.md) tools with Lambda, there are important considerations for connection lifecycle management.
215+
216+
### MCP Connection Lifecycle
217+
218+
**Establish a new MCP connection for each Lambda invocation.** Creating the `MCPClient` object itself is inexpensive - the costly operation is establishing the actual connection to the server. Use context managers to ensure connections are properly opened and closed:
219+
220+
```python
221+
from mcp.client.streamable_http import streamablehttp_client
222+
from strands import Agent
223+
from strands.tools.mcp import MCPClient
224+
225+
def handler(event, context):
226+
mcp_client = MCPClient(
227+
lambda: streamablehttp_client("https://your-mcp-server.example.com/mcp")
228+
)
229+
230+
# Context manager ensures connection is opened and closed safely
231+
with mcp_client:
232+
tools = mcp_client.list_tools_sync()
233+
agent = Agent(tools=tools)
234+
response = agent(event.get("prompt"))
235+
236+
return str(response)
237+
```
238+
239+
**Advanced: Reusing connections across invocations**
240+
241+
For optimization, you can establish the connection at module level using `start()` to reuse it across Lambda warm invocations:
242+
243+
```python
244+
from mcp.client.streamable_http import streamablehttp_client
245+
from strands import Agent
246+
from strands.tools.mcp import MCPClient
247+
248+
# Create and start connection at module level (reused across warm invocations)
249+
mcp_client = MCPClient(
250+
lambda: streamablehttp_client("https://your-mcp-server.example.com/mcp")
251+
)
252+
mcp_client.start()
253+
254+
def handler(event, context):
255+
tools = mcp_client.list_tools_sync()
256+
agent = Agent(tools=tools)
257+
response = agent(event.get("prompt"))
258+
return str(response)
259+
```
260+
261+
!!! warning "Multi-tenancy Considerations"
262+
MCP connections are typically stateful to a particular conversation. Reusing a connection across invocations can lead to state leakage between different users or conversations. **Start with the context manager approach** and only optimize to connection reuse if needed, with careful consideration of your tenancy model.
263+
180264
## Summary
181265

182266
The above steps covered:
183267

184268
- Creating a Python handler that Lambda invokes to trigger an agent
185-
- Creating the CDK infrastructure to deploy to Lambda
186-
- Packaging up the Lambda handler and dependencies
269+
- Infrastructure options: official Lambda layer or custom dependencies layer
270+
- Packaging up the Lambda handler and dependencies
187271
- Deploying the agent and infrastructure to an AWS account
272+
- Using MCP tools with HTTP-based transports on Lambda
188273
- Manually testing the Lambda function
189274

190275
Possible follow-up tasks would be to:

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ nav:
189189
- Overview: user-guide/deploy/deploy_to_bedrock_agentcore/index.md
190190
- Python: user-guide/deploy/deploy_to_bedrock_agentcore/python.md
191191
- TypeScript: user-guide/deploy/deploy_to_bedrock_agentcore/typescript.md
192-
- AWS Lambda: user-guide/deploy/deploy_to_aws_lambda.md
192+
- AWS Lambda<sup> new</sup>: user-guide/deploy/deploy_to_aws_lambda.md
193193
- AWS Fargate: user-guide/deploy/deploy_to_aws_fargate.md
194194
- Amazon EKS: user-guide/deploy/deploy_to_amazon_eks.md
195195
- Amazon EC2: user-guide/deploy/deploy_to_amazon_ec2.md

0 commit comments

Comments
 (0)