Skip to content

QA plan: Guardrails policy in Terraform (#70)

Story: Provision Guardrails policy in Terraform (#70)
Feature: Safety Gate via Bedrock Guardrails (#69)
Capability: Safety (#44)
ADRs: RAG013 — Safety gate via Bedrock Guardrails

TypeCoverage
IaC (terraform test, CI)With enable_guardrail = false (the default), no guardrail, guardrail version, or guardrail IAM policy exists; the API Lambda has no GUARDRAIL_ARN or GUARDRAIL_VERSION; the guardrail_arn and guardrail_version outputs are null
IaC (terraform test, CI)With enable_guardrail = true, exactly one guardrail and one numbered version exist; the prompt-attack filter is on for input and off for output; the API Lambda receives the guardrail ARN and the numbered version
IaC (terraform test, CI)The Lambda role’s bedrock:ApplyGuardrail grant names the guardrail ARN and never *
IaC (plan review)terraform plan on each root with the flag off shows no guardrail changes
IaC (plan review)terraform plan on each root with the flag on adds the guardrail, its version, and the guardrail IAM policy, updates the API Lambda environment in place, and destroys nothing
Staticterraform fmt -check, terraform validate, tflint, and the Trivy IaC scan pass
CostGuardrail cost is usage-based, with the per-environment ceiling stated in the Cost section of RAG013; the Infracost PR comment covers the fixed-price resources it supports and lists the guardrail as unsupported
Integration (manual, QA)After the flag is enabled and applied in QA, the API Lambda’s configuration holds GUARDRAIL_ARN and GUARDRAIL_VERSION, and aws bedrock get-guardrail on that ARN and version returns the hate, insults, sexual, and violence content filters at HIGH for input and output, the prompt-attack filter on input only, and one placeholder denied topic
  • Story #23 (API module) merged: the module that owns the Lambda, its role, and its environment exists
  • RAG013 accepted
  • Bedrock Guardrails available in the environment’s region
  • terraform test in infra/modules/api passes in CI, including the flag-off and flag-on runs
  • A plan with the flag off shows no guardrail changes in either root
  • A plan with the flag on adds the guardrail, its version, and the guardrail IAM policy, and changes only the API Lambda’s environment among existing resources
  • The api module outputs guardrail_arn and guardrail_version, null when the flag is off
  • The IAM grant for bedrock:ApplyGuardrail is scoped to the guardrail ARN
  • enable_guardrail defaults to false

IaC and static checks: CI. Plan review: the operator’s own shell against the QA root, then the Prod root. Integration: QA, after the flag is enabled.

Terraform (terraform test, terraform plan), tflint, Trivy, Infracost, AWS CLI (aws bedrock get-guardrail, aws lambda get-function-configuration).

# Given enable_guardrail is false
# When the module is planned
# Then no guardrail resource exists and the outputs are null
run "guardrail_absent_by_default" {
command = plan
assert {
condition = length(aws_bedrock_guardrail.this) == 0
error_message = "No guardrail resource may exist when enable_guardrail is false."
}
}
Terminal window
# Given the QA root with enable_guardrail = true in terraform.tfvars
# When the plan runs
# Then the guardrail, its version, and the IAM policy are added and the Lambda environment changes in place
make tf-qa-plan
  • The guardrail version is replaced whenever the guardrail changes. The replacement is created before the old version is destroyed, so the Lambda never points at a missing version within one apply. Confirm this on the first enabled apply: the Lambda environment moves to the new version number and the old version is removed without error.
  • aws_bedrock_guardrail_version exists only in newer releases of the AWS provider. The roots constrain the provider to ~> 5.0 and their lockfiles pin the exact version, so the failure to watch for is a lockfile regenerated against an older provider.
  • terraform test supplies the guardrail ARN and version through overrides during plan, so it does not prove the values the provider returns at apply time. The integration row covers that.
  • Infracost cannot price the guardrail, so a cost regression is caught by the Bedrock spend alarm (#64) and budgets, not by the PR comment.

Safety (#44): the guardrail policy is managed in Terraform per environment, and the API Lambda can call ApplyGuardrail on it and nothing broader.