TERRAFORM · cost-engineering
Terraform: Bedrock cost attribution by IAM principal
Federated developer role with session-tag passthrough, principal tags as fallback, cost allocation tag activation, and the Athena queries that read spend by person from CUR 2.0.
✓ verified 2026-07-08 · stack: terraform, aws, bedrock, claude-code · source
The identity side of Bedrock cost attribution as one module: a developer role that federated identities assume with session tags flowing through, principal tags as the static fallback, and the cost allocation tag activation that makes both visible in Cost Explorer and CUR 2.0. One step stays in the console by design (creating the CUR 2.0 export itself) and is documented below.
terraform { required_providers { aws = { source = "hashicorp/aws" version = ">= 5.0" } }}
variable "name" { description = "Prefix for created resources" type = string default = "claude-code"}
variable "oidc_provider_arn" { description = "IAM OIDC provider for your IdP (Identity Center, Okta, Entra)" type = string}
variable "oidc_provider_host" { description = "The provider hostname as it appears in condition keys, e.g. oidc.example.okta.com" type = string}
variable "oidc_audience" { description = "Expected aud claim in the ID token" type = string}
variable "region" { type = string default = "eu-west-2"}
variable "model_name" { description = "Claude model name as it appears in Bedrock ARNs" type = string default = "anthropic.claude-sonnet-4-6"}
variable "team" { type = string}
variable "cost_center" { type = string}
data "aws_caller_identity" "current" {}
locals { account_id = data.aws_caller_identity.current.account_id}
# Trust: the IdP assumes the role AND may attach session tags. Without# sts:TagSession here, the tags claim in the ID token is rejected and# every developer collapses into one anonymous role in billing.data "aws_iam_policy_document" "developer_trust" { statement { actions = [ "sts:AssumeRoleWithWebIdentity", "sts:TagSession", ] principals { type = "Federated" identifiers = [var.oidc_provider_arn] } condition { test = "StringEquals" variable = "${var.oidc_provider_host}:aud" values = [var.oidc_audience] } }}
# Principal tags are the static fallback dimension: they attribute every# session of this role even when the IdP sends no session tags. Session# tags with the same keys override these per session.resource "aws_iam_role" "bedrock_developer" { name = "${var.name}-bedrock-developer" assume_role_policy = data.aws_iam_policy_document.developer_trust.json tags = { team = var.team cost_center = var.cost_center }}
resource "aws_iam_role_policy" "bedrock_invoke" { name = "${var.name}-bedrock-invoke" role = aws_iam_role.bedrock_developer.id policy = jsonencode({ Version = "2012-10-17" Statement = [ { Sid = "InvokeClaude" Effect = "Allow" Action = [ "bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream" ] Resource = [ "arn:aws:bedrock:${var.region}:${local.account_id}:inference-profile/*", "arn:aws:bedrock:*::foundation-model/${var.model_name}" ] } ] })}
# Activate the tag keys as cost allocation tags so they appear in Cost# Explorer and CUR 2.0. Not retroactive: only spend after activation is# tagged, so apply this before the fleet starts, not after.resource "aws_ce_cost_allocation_tag" "attribution" { for_each = toset(["user", "team", "cost_center"]) tag_key = each.value status = "Active"}
output "developer_role_arn" { value = aws_iam_role.bedrock_developer.arn}Configure the IdP to send each developer’s attributes in the ID token’s
https://aws.amazon.com/tags claim: user (email), team, and
cost_center. They arrive as session tags on every assumption of this
role. The one console step: under Billing → Data Exports, create a
CUR 2.0 export with Include caller identity (IAM principal) allocation
data selected. An export created before this feature will never grow the
identity columns; it must be a new one.
Once the export lands in S3 and Athena is pointed at it, three queries do the month-one work:
-- Spend by developer by day: the per-person series that did not exist-- before April 2026.SELECT date(line_item_usage_start_date) AS day, "iamPrincipal/user" AS developer, round(sum(line_item_unblended_cost), 2) AS usdFROM cur2WHERE product_servicecode = 'AmazonBedrock'GROUP BY 1, 2ORDER BY 1, 3 DESC;
-- Token mix by team: all four usage types, separately. Summing only-- input and output is the classic reconciliation mistake.SELECT "iamPrincipal/team" AS team, line_item_usage_type AS usage_type, sum(line_item_usage_amount) AS tokens, round(sum(line_item_unblended_cost), 2) AS usdFROM cur2WHERE product_servicecode = 'AmazonBedrock'GROUP BY 1, 2ORDER BY 1, 4 DESC;
-- Cache-read share by team: low share is money on fire.SELECT "iamPrincipal/team" AS team, sum(CASE WHEN line_item_usage_type LIKE '%cache-read%' THEN line_item_usage_amount ELSE 0 END) / sum(line_item_usage_amount) AS cache_read_shareFROM cur2WHERE product_servicecode = 'AmazonBedrock'GROUP BY 1;What it does
- The trust policy admits your IdP with both
sts:AssumeRoleWithWebIdentityandsts:TagSession, so per-developer session tags flow from the ID token into every Bedrock call’s billing record: one shared role, per-person dollars. - Principal tags on the role guarantee a team-level dimension even for sessions that arrive without session tags; session tags override them per key when present.
- The invoke policy grants model invocation only (no
bedrock:*), scoped to the account’s inference profiles and the named Claude model. aws_ce_cost_allocation_tagactivates the three tag keys in billing, the step teams most often discover months late because untagged history cannot be recovered.
Failure modes
- Tags activate, data lags. Tag keys appear in the billing console only after a principal has made at least one Bedrock call, activation can take 24 hours, and none of it is retroactive. Sequence: apply this module, make one test call, confirm activation, then roll the fleet.
- The CUR export is the step people miss. Identity columns require a CUR 2.0 export created with the caller-identity option. Recreating it is cheap; discovering in month three that the old export lacks the columns is not.
- Column names vary by export configuration. The queries use the
documented
iamPrincipal/{key}form; your Athena table may surface them with sanitised names. RunSHOW COLUMNSonce and adjust: the shapes hold, the spellings may not. - A gateway in the path erases the person. If Claude Code traffic flows through a LiteLLM or custom gateway, the gateway’s execution role is the captured identity. The gateway must assume this role per user with session tags, and cache the credentials per session, because re-assuming on every request trips STS rate quotas.
- Session tags do not reach invocation logs. They bind at STS time and
surface only in billing data. If you also need per-request identity in
model invocation logs, set a per-user
RoleSessionNamesoidentity.arndiffers per developer. bedrock-mantleAPIs are outside the net. IAM principal attribution coversbedrock-runtime(InvokeModel,Converse, Chat Completions) as of 2026-07-08; mantle support is announced as coming. Spend on that surface needs Projects.
Verified against the Bedrock IAM principal attribution and CUR data documentation and the April 2026 launch announcement (trust-policy requirements, tag precedence, activation steps, export option, usage-type formats), 2026-07-08.