# A guardrail for prompt attacks and five kinds of harmful content, with a # published version, and optionally enforced on every model call in this # account and Region. # # The guardrail stays on the AWS owned key. With a customer key, every caller # that applies it needs kms:Decrypt on that key, and an enforced guardrail # applies to every caller. resource "aws_bedrock_guardrail" "models" { name = var.name description = "Prompt attacks and harmful content, from the ${var.name} template." blocked_input_messaging = "This request was blocked by the account's guardrail." blocked_outputs_messaging = "This response was blocked by the account's guardrail." content_policy_config { # AWS's API example for this filter sets only the input side, and an AWS # sample template notes the API rejects any output strength but NONE. # The provider requires the field, so it is NONE. filters_config { type = "PROMPT_ATTACK" input_strength = var.guardrail_strength output_strength = "NONE" } dynamic "filters_config" { for_each = toset(["HATE", "INSULTS", "SEXUAL", "VIOLENCE", "MISCONDUCT"]) content { type = filters_config.value input_strength = var.guardrail_strength output_strength = var.guardrail_strength } } } } # A numbered version, which enforcement requires. Any change to the guardrail # publishes a new one. Old versions are kept, so a caller pinned to one keeps # working, and no DeleteGuardrail call trips guardrails-weakened. resource "aws_bedrock_guardrail_version" "models" { guardrail_arn = aws_bedrock_guardrail.models.guardrail_arn description = "Published by Terraform." skip_destroy = true lifecycle { replace_triggered_by = [aws_bedrock_guardrail.models] } } # Applied to every model call in this account and Region, including calls # that name no guardrail. System prompts are judged only where the caller # tags them, since a developer's instructions can look like a prompt attack. resource "aws_bedrock_enforced_guardrail_configuration" "models" { count = var.enforce_guardrail ? 1 : 0 guardrail_arn = aws_bedrock_guardrail.models.guardrail_arn guardrail_version = aws_bedrock_guardrail_version.models.version selective_content_guarding { system = "SELECTIVE" messages = "COMPREHENSIVE" } }