TERRAFORM · multi-agent-orchestration
Terraform: least-privilege IAM for a Bedrock agent workload
Role, EU Geo-aware invocation policy, optional guardrail enforcement, and a cost-attributed application inference profile for an agent service.
✓ verified 2026-06-10 · stack: terraform, aws, bedrock · source
A self-contained module for the IAM footprint of one agent service (Lambda or ECS) calling Claude on Bedrock from eu-west-2 via the EU Geographic inference profile. It encodes the two facts teams get wrong: EU Geo invocation needs the foundation-model ARN in every destination EU region, not just your own, and guardrails are only mandatory if IAM makes them so.
terraform { required_providers { aws = { source = "hashicorp/aws" version = ">= 5.0" } }}
variable "name" { description = "Service name; prefixes all created resources" type = string}
variable "region" { description = "Home region of the workload" 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 "geo_destination_regions" { description = <<-EOT EU regions the eu. Geo profile may route to. Enumerate with: aws bedrock get-inference-profile \ --inference-profile-identifier eu.<model-name> --region eu-west-2 and keep this list (and any region SCPs) in sync with the output. EOT type = list(string) default = ["eu-west-1", "eu-west-2", "eu-central-1"]}
variable "trusted_service" { description = "Service principal that assumes the role (lambda.amazonaws.com or ecs-tasks.amazonaws.com)" type = string default = "lambda.amazonaws.com"}
variable "required_guardrail_arn" { description = "If set, every invocation must carry exactly this guardrail" type = string default = null}
variable "team" { type = string}
variable "cost_center" { type = string}
variable "environment" { type = string}
data "aws_caller_identity" "current" {}
locals { account_id = data.aws_caller_identity.current.account_id geo_profile_arn = "arn:aws:bedrock:${var.region}:${local.account_id}:inference-profile/eu.${var.model_name}" foundation_model_arn = "arn:aws:bedrock:${var.region}::foundation-model/${var.model_name}" foundation_model_arns = [ for r in var.geo_destination_regions : "arn:aws:bedrock:${r}::foundation-model/${var.model_name}" ]}
data "aws_iam_policy_document" "agent_trust" { statement { actions = ["sts:AssumeRole"] principals { type = "Service" identifiers = [var.trusted_service] } }}
resource "aws_iam_role" "bedrock_agent" { name = "${var.name}-bedrock-agent" assume_role_policy = data.aws_iam_policy_document.agent_trust.json tags = { team = var.team costcenter = var.cost_center environment = var.environment }}
resource "aws_iam_role_policy" "bedrock_invoke" { name = "${var.name}-bedrock-invoke" role = aws_iam_role.bedrock_agent.id policy = jsonencode({ Version = "2012-10-17" Statement = concat( [ { Sid = "InvokeEuGeoInferenceProfile" Effect = "Allow" Action = [ "bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream" ] # EU Geo requires the profile ARN plus the foundation model # in every destination region the profile can route to. Resource = concat([local.geo_profile_arn], local.foundation_model_arns) }, { Sid = "GetInferenceProfile" Effect = "Allow" Action = "bedrock:GetInferenceProfile" Resource = local.geo_profile_arn } ], var.required_guardrail_arn == null ? [] : [ { Sid = "AllowGuardrailUse" Effect = "Allow" Action = ["bedrock:ApplyGuardrail", "bedrock:GetGuardrail"] Resource = var.required_guardrail_arn }, { # Reject any invocation that omits or substitutes the guardrail. Sid = "DenyInvokeWithoutRequiredGuardrail" Effect = "Deny" Action = [ "bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream" ] Resource = "*" Condition = { StringNotEquals = { "bedrock:GuardrailIdentifier" = var.required_guardrail_arn } } } ] ) })}
# Cost-attributed application inference profile: spend appears in# Cost Explorer / CUR 2.0 under these tags.resource "aws_bedrock_inference_profile" "team" { name = "${var.name}-${var.team}" description = "Cost-attributed profile for ${var.team}" model_source { copy_from = local.foundation_model_arn } tags = { team = var.team costcenter = var.cost_center environment = var.environment }}
output "role_arn" { value = aws_iam_role.bedrock_agent.arn}
output "application_inference_profile_arn" { value = aws_bedrock_inference_profile.team.arn}What it does
- Creates a service role assumable by Lambda or ECS tasks, with invocation
permissions scoped to one EU Geo inference profile, not
foundation-model/*, and nobedrock:*. - Grants the foundation-model ARN in each Geo destination region, which the prerequisites doc requires for cross-region profiles; a policy listing only eu-west-2 fails whenever the profile routes elsewhere in the EU.
- Optionally hard-requires a guardrail using the
bedrock:GuardrailIdentifiercondition key: a deny that fires when the invocation omits the guardrail or names a different one. Application code cannot opt out. - Creates an application inference profile tagged with
team,costcenter, andenvironmentso spend is attributable in Cost Explorer without request-level logging.
Failure modes
- The Geo destination list drifts. AWS can add destination regions to the
eu.profile; a new destination missing fromgeo_destination_regions(or blocked by an organisation SCP) surfaces as intermittentAccessDeniedExceptionunder load. Re-runaws bedrock get-inference-profileafter Bedrock region announcements and keep SCPs in sync. - The guardrail deny is account-wide in effect.
Resource = "*"on the deny statement means this role can never invoke any model without that exact guardrail, including via other inference profiles you attach later. That is the point, but do not reuse the role for a workload with a different guardrail. - In-region pinning changes the resource list. If you swap the Geo
profile for strict in-region inference (
anthropic.claude-sonnet-4-6, available at eu-west-2 for Sonnet 4.6 and Opus 4.6 as of 2026-06-10), drop the cross-region foundation-model ARNs: leaving them grants more than the workload needs. - Tags only attribute what flows through the profile. A caller permitted to hit the foundation model directly bypasses attribution. Pair this module with IAM that denies direct model IDs to teams, or the Cost Explorer picture is partial.
copy_frommust be a foundation-model ARN, not an inference-profile ARN. Application inference profiles copy from a foundation-model ARN (e.g.arn:aws:bedrock:<region>::foundation-model/<model-name>). Passing an inference-profile ARN tocopy_fromfails or routes incorrectly; always uselocal.foundation_model_arn, notlocal.geo_profile_arn.- Provider version matters.
aws_bedrock_inference_profilerequires a recent AWS provider (5.x line); on older providers the plan fails. Pin and upgrade deliberately.
Verified against the Bedrock inference prerequisites (IAM actions and
inference-profile/foundation-model ARN shapes for cross-region profiles) and
the identity-based policy examples, 2026-06-10. The
bedrock:GuardrailIdentifier enforcement pattern follows the March 2025
policy-based enforcement release.