Homeβ€Ί Blogβ€Ί Week 17 - An MCP Server That Answers What My AWS A…
AWS Weekly Lab AWS Terraform

I asked my AWS account what it was doing

Four days earlier, answering that question took eleven API calls and a week of billing I had not noticed. The data was there the whole time β€” what was missing was a way to ask. This week builds one: a Model Context Protocol server over live account state, read-only by design, with a cache that exists because one of the APIs charges a cent every time a model gets curious.

Verified against current vendor documentation on 3 September 2026. Pricing, limits and API behaviour were checked against the official docs on that date. Cloud services change fast — if you are reading this much later, treat the specifics as a starting point and re-check the linked sources.
AWS Platform Engineering Lab Β· Week 17 of 52

Lab Overview

On 30 August I opened a bill I did not expect and asked a simple question: what is running, what is it costing, and did I leave anything behind?

The problem. Answering it took eleven separate lookups across five systems β€” one for what I was charged, one for who changed what, one for what still existed, one for whether anything had alerted, one for what my infrastructure code believed. None of them talk to each other, and each required me to already know it held the answer. Every number had existed for days: a service billing $9.35 a day for a week, a budget alert fired and ignored, anomaly detection flagging eight cents and missing the sixty-one dollars next to it.

The objective. Build an MCP server that lets a model answer the question without knowing which six APIs to call, in what order, with what filters.

The outcome. It works, it costs $0.13, and writing up its own security model caught a claim about it I had published three times and never checked.

DifficultyIntermediate
Lab runtimeNot recorded
Measured cost$0.13 β€” nothing bills while nobody is asking
DeploymentTerraform via HCP Terraform with OIDC
Full lab guideGitHub β€” prerequisites, Terraform, commands, validation, cleanup

Cloud Services and Concepts

ServicePurpose hereWhat it is
AWS Lambda + Function URLThe MCP server itself. No hourly charge, nothing idle to pay for.Lambda runs code with no server to manage; a function URL gives that function its own HTTPS endpoint directly, with no API Gateway in front and no hourly charge for sitting idle.
AWS Cost Explorer APIWhat the account is spending. The only metered read in the build, at $0.01 per request.The programmatic way to read your own spend. Unlike most read calls it is metered, at a per-request charge, which is why answers are worth caching.
AWS CloudTrail / Config / CloudWatchThe other three read paths: who changed what, what still exists, what alerted.CloudTrail records who called what, Config records how resources were configured, and CloudWatch holds the metrics and logs.
Amazon DynamoDBCaches Cost Explorer answers so a chatty model does not bill per question.A managed key-value database with no servers to size. You are billed per read and write rather than per hour.
IAMThe actual security model β€” five statements, read back from the API rather than the console.AWS's permission system. Every call is signed by a principal, and a policy decides whether that principal may make it.

MCP, in one sentence: a protocol that lets a model call tools you define, where each tool has a name, a description and a schema β€” and the model decides when to call it.

A tool description is model input, not documentation. It is not text for humans that the model happens to see; it is the thing the model reads to decide what to do. Anything written there is read on every call.

Architecture and Workflow

MCP client signs with SigV4 aws AWS Cloud Region: us-east-1 1 Function URL AWS_IAM auth four read-only tools unsigned = 403 Cost Explorer $0.01 per request Resource Groups Tagging what runs, and what has no owner CloudWatch alarm state DynamoDB TTL 1 hour A repeated cost question costs $0.00. An LLM decides when to call a tool, so a metered read needs a cache rather than only a budget.
Four read-only tools behind a signed URL, and the one of them that costs money every time it runs.
  1. A client signs an HTTPS request with SigV4 and calls the Lambda Function URL.
  2. Lambda rejects anything unsigned before application code runs β€” the Function URL is set to AWS_IAM auth.
  3. The server exposes four tools, each mapping to exactly one read path into the account.
  4. Cost questions check the DynamoDB cache first, because Cost Explorer is metered per request and the model decides call frequency.
  5. A cache miss calls Cost Explorer, returns the answer and writes it back to the cache.
  6. Everything else β€” CloudTrail, Config, CloudWatch β€” is a free read, so it goes straight through.

Design decisions

IAM scoped per tool, not per server. Each statement maps to exactly one tool, so the policy reads as a list of capabilities rather than a permission surface.

A Function URL rather than API Gateway. It removes every idle charge and the entire Cognito surface. It also means an off-the-shelf MCP client speaking plain HTTP cannot connect β€” it gets a 403 before the protocol starts, because it has no idea how to sign an AWS request. For a personal account where the only caller is me, that trade is obviously right; for a platform team it is the first thing to revisit.

A cache in front of the one metered API. When a model decides how often to call something, a cent per request is an architectural constraint, not a rounding error.

Prerequisites

RequirementDetail
AWS accountPermission for Lambda, IAM, DynamoDB, CloudWatch; and Cost Explorer enabled
Terraform>= 1.10
An MCP clientOne that can SigV4-sign requests β€” a plain HTTP client will get 403
Regionus-east-1 β€” Cost Explorer is a global endpoint served here

Know which reads are metered before you design. Almost every describe/list call in AWS is free. Cost Explorer is $0.01 per request, and each page of results counts. That single fact is why there is a cache in this architecture.

Implementation

One Lambda, one Function URL, one DynamoDB table, one role and one alarm. The lab guide carries the handler and the full Terraform.

week-17-mcp-platform-ops/
|
+-- terraform/
|   +-- environments/dev/   HCP backend, OIDC, wires the module
|   +-- modules/server/     Lambda, Function URL, DynamoDB cache, IAM role
|
+-- src/                    the MCP handler and its four tool definitions
+-- scripts/                signed test calls and cleanup
Only the directories that matter. The full repository is on GitHub.

The role is the security model, and it fits on one screen β€” four read statements, one cache write, one log write:

The complete IAM policy for the MCP server, read back from iam:GetRolePolicy, showing five statements
The server's entire permission set, read back from the API rather than the console. Four read statements, one cache write, one log write.
An unsigned curl returns HTTP 403; the same request signed with SigV4 connects and lists four tools
403 unsigned, connected when signed. Lambda rejects the first one before any of my code runs.

Validation and Evidence

The question that started the week is the test: ask it, and check the answer against what the account actually contained.

TestExpectedActualStatus
Unsigned request to the Function URLRejected before any code runs403 from LambdaPass
Signed request from an allowed principalProtocol handshake succeedsSucceededPass
"What did I leave running?"Matches what exists in the accountMatchedPass
Repeat cost questionServed from cache, no second chargeCache hit; no additional Cost Explorer requestPass
Role holds no write of any kindTrue β€” as published in three filesFalse: dynamodb:PutItem on the cache tableFail β€” the claim, not the build
Asking what was left running with no owner returns 15 untagged resources including two Week 12 test buckets
Fifteen resources with no owner tag. Two of them are Week 12 test buckets a script created outside Terraform, so the destroy run never knew they existed.
The verbatim question, the two tools the model chose to call, and its unedited answer naming a Lightsail instance
The question verbatim, the two tools the model chose on its own, and the answer unedited apart from the account id. No tool was named in the prompt.

Security and Cost

Security

No write on anything the server reports on. The only write in the role is dynamodb:PutItem against its own cache table. A model steered by a poisoned tool description can fill a cache; it cannot change the account. That turns a destruction risk into a disclosure one.

IAM scoped per tool, not per server, and the cache write scoped to one table ARN rather than DynamoDB generally.

AWS_IAM auth on the Function URL. Every request must be SigV4-signed by a principal allowed to invoke it; Lambda rejects the rest before application code runs.

Tool descriptions are treated as model input. They say what the tool answers and what it costs, and nothing else.

Permissions are the security model, not code. It would be easy to claim this server is safe because the code only reads. Code can be changed between reviews; a policy is checked by AWS on every call.

Cost

$0.13 measured, read from Cost Explorer. After teardown, $0.00.

ItemRateThis build
Cost Explorer API$0.01/request, each page countsthe only metered read
Lambdaper request, sub-second callseffectively free at this volume
DynamoDB on-demandper requesteffectively free at this volume
CloudWatch alarm$0.10/alarm/month$0.10/month
Function URLno hourly charge$0 idle
Two identical cost questions: the first shows _cached false and pays a cent, the second shows _cached true and pays nothing
The same question twice. The second one costs nothing, and says so.

Nothing here bills while nobody is asking β€” which is the useful shape for a tool you consult occasionally rather than run continuously.

Troubleshooting and Lessons

I published a claim about my own security model that was not true

Problem. Three files β€” the handler docstring, the Terraform comment and the README β€” said the role holds no write action of any kind.
Root cause. I wrote it while designing. It was true of the design in my head, and I never re-read it against the thing I built. The fourth statement holds dynamodb:PutItem β€” a scoped write to the server's own cache, architecturally fine, but the claim was false.
Resolution. Rendering the policy as a screenshot and reading it. The accurate claim is narrower and still worth making: there is no write on anything the server reports on.
Lesson. The fix was not proofreading β€” it was rendering the artefact as evidence. Describing a policy lets you describe the policy you meant; showing it forces the one that exists.

The console would not show the proof

Problem. The IAM console lists a policy name and hides its body, so the screenshot that would prove the claim did not exist there.
Root cause. Chasing the console was the wrong instinct β€” the authoritative copy is the API's.
Resolution. Read the policy back from the API and render that.
Lesson. When the console will not show a thing, ask whether the console is the right source rather than how to make it cooperate.

One metered API changes the architecture

Problem. A model decides how often to call a tool, and Cost Explorer bills per request.
Root cause. Almost every AWS read is free, so the instinct is that reads are free. One is not.
Resolution. A DynamoDB cache in front of the only metered path.
Lesson. Know which reads are metered before you let something else choose the call frequency.

Cleanup and Key Takeaways

Queue a destroy from HCP, then verify against AWS rather than against the run status. The script checks Lambda, IAM, DynamoDB, alarms and log groups by name prefix, then does a tag search for Week=17.

That second check exists because of a specific failure. Week 12's teardown reported success and left two S3 buckets behind β€” a shell script had created them outside Terraform, so destroy had no idea they existed. A destroy run tells you Terraform removed what it knew about. It tells you nothing about what it never knew.

Key takeaways

  • The bottleneck was never the data. Every number I needed already existed; what was missing was a way to ask without knowing which API held the answer.
  • Know which reads are metered. When a model decides call frequency, one metered API changes the architecture.
  • A tool description is model input, not documentation the model happens to see.
  • Permissions are the security model, not code. Code changes between reviews; a policy is enforced on every call.
  • Render the artefact instead of describing it. Showing the policy caught a false claim I had written three times and read past twice.

References

Comments

How was your experience?
Your feedback helps improve this site.
PoorExcellent