forked from strands-agents/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws_cost_assistant.py
More file actions
67 lines (53 loc) · 2.32 KB
/
aws_cost_assistant.py
File metadata and controls
67 lines (53 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
from mcp import StdioServerParameters, stdio_client
from strands import Agent, tool
from strands.models import BedrockModel
from strands.tools.mcp import MCPClient
from strands_tools import file_write
@tool
def aws_cost_assistant(query: str) -> str:
"""
Process and respond AWS cost related queries.
Args:
query: The user's question
Returns:
A helpful response addressing user query
"""
bedrock_model = BedrockModel(model_id="us.anthropic.claude-3-5-haiku-20241022-v1:0")
response = str()
try:
env = {}
if os.getenv("BEDROCK_LOG_GROUP_NAME") is not None:
env["BEDROCK_LOG_GROUP_NAME"] = os.getenv("BEDROCK_LOG_GROUP_NAME")
cost_mcp_server = MCPClient(
lambda: stdio_client(
StdioServerParameters(
command="uvx",
args=["awslabs.cost-explorer-mcp-server@latest"],
env=env,
)
)
)
with cost_mcp_server:
tools = cost_mcp_server.list_tools_sync() + [file_write]
# Create the research agent with specific capabilities
cost_agent = Agent(
model=bedrock_model,
system_prompt="""You are a AWS account cost analyst. You can do the following tasks:
- Amazon EC2 Spend Analysis: View detailed breakdowns of EC2 spending for the last day
- Amazon Bedrock Spend Analysis: View breakdown by region, users and models over the last 30 days
- Service Spend Reports: Analyze spending across all AWS services for the last 30 days
- Detailed Cost Breakdown: Get granular cost data by day, region, service, and instance type
- Interactive Interface: Use Claude to query your cost data through natural language
""",
tools=tools,
)
response = str(cost_agent(query))
print("\n\n")
if len(response) > 0:
return response
return "I apologize, but I couldn't properly analyze your question. Could you please rephrase or provide more context?"
except Exception as e:
return f"Error processing your query: {str(e)}"
if __name__ == "__main__":
aws_cost_assistant("Get my usage of last 7 days per service")