Lesson 2 of 4 Securing Generative AI Applications

Applying Safety and Privacy Policies with Amazon Bedrock Guardrails

This lesson explains how Amazon Bedrock Guardrails apply configurable safety and privacy policies to model inputs and outputs, protecting generative AI applications from manipulation and from harmful, private or invented content.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish the guardrail policies, such as content filters, denied topics and sensitive information filters.
  • Describe where guardrails apply, including the ApplyGuardrail API.
  • Explain how the bedrock:GuardrailIdentifier condition key enforces guardrail use.

Guardrail policies

A Bedrock guardrail combines several policies, each evaluated on prompts, responses or both. Content filters detect hate, insults, sexual content, violence and misconduct at an adjustable strength, in text and images. The prompt attack filter detects jailbreak attempts and prompt injection. Denied topics block subjects you describe in natural language, such as investment advice, and word filters block profanity and words you list.

Sensitive information filters detect personally identifiable information (PII) and custom regular expression patterns, and either block the content or mask the values. Contextual grounding checks detect hallucinations by checking that a response is supported by the source material and relevant to the question. Automated reasoning checks use formal logic to verify responses against policy rules you define. The following table summarises the policies.

PolicyWhat it does
Content filtersHate, insults, sexual content, violence, misconduct; adjustable strength; covers images
Prompt attackDetects jailbreaks and prompt injection
Denied topicsTopics defined in natural language
Word filtersProfanity and custom words
Sensitive information filtersBlock or mask PII and custom regex patterns
Contextual grounding checksDetect hallucinations against source material
Automated reasoning checksVerify responses against formal policy rules
Sort into groups

Which guardrail policy?

Exam skills 3.2.7

Choose the Bedrock guardrail policy that meets each requirement.

Log in to try this activity Interactive activities are free with an account.

Where guardrails apply

A guardrail is defined once, with its content filters, denied topics, word filters, sensitive information filters, contextual grounding checks and automated reasoning checks, and is then versioned. The same guardrail can be applied wherever model input and output pass, so one policy protects every application that uses it. The following list describes where guardrails can be applied.

  • Model invocations through InvokeModel and Converse, by guardrail ID and version.
  • Bedrock Agents and Knowledge Bases.
  • The ApplyGuardrail API, for any text, including from SageMaker AI or non-AWS models.

The following diagram matches four requirements for a banking assistant to the Bedrock guardrail policy that meets each.

One Bedrock guardrail expressing four requirements: a denied topic for investment advice, a prompt attack filter at high input strength, sensitive information rules that block bank account numbers and anonymise email addresses, and contextual grounding at 0.8. An IAM condition makes calls without the guardrail fail.
The scenario is illustrative.
True or false

Where guardrails apply

Exam skills 3.2.7

Check where a Bedrock guardrail can be used.

Log in to try this activity Interactive activities are free with an account.

Enforcing guardrails

An IAM condition on bedrock:GuardrailIdentifier makes model invocations without the required guardrail fail, and an SCP can apply the same requirement across accounts. Guardrail versions are immutable, so applications reference a tested version and a new version is promoted deliberately.

Scenario questions

Making guardrails mandatory

Exam skills 3.2.7 · 6.1.3

Choose how to make guardrails mandatory and stable.

Log in to try this activity Interactive activities are free with an account.
Worked example

A guardrail for a customer-facing assistant

A bank's assistant must refuse investment advice, resist jailbreaks, never reveal account numbers, and stay grounded in the bank's documentation. One guardrail expresses all four requirements:

aws bedrock create-guardrail --name retail-assistant \
  --blocked-input-messaging "Sorry, I cannot help with that." \
  --blocked-outputs-messaging "Sorry, I cannot share that." \
  --topic-policy-config '{"topicsConfig":[{"name":"InvestmentAdvice","type":"DENY",
      "definition":"Recommendations about buying or selling specific investments."}]}' \
  --content-policy-config '{"filtersConfig":[{"type":"PROMPT_ATTACK","inputStrength":"HIGH","outputStrength":"NONE"}]}' \
  --sensitive-information-policy-config '{"piiEntitiesConfig":[
      {"type":"US_BANK_ACCOUNT_NUMBER","action":"BLOCK"},{"type":"EMAIL","action":"ANONYMIZE"}]}' \
  --contextual-grounding-policy-config '{"filtersConfig":[{"type":"GROUNDING","threshold":0.8}]}'

The application references the guardrail ID and version in every InvokeModel or Converse call, and an IAM condition on bedrock:GuardrailIdentifier makes calls without it fail.

Check yourself

Which guardrail filter blocks jailbreak and prompt injection attempts?

Show answer

The prompt attack filter.

Exam Tips

Remember for the exam

  • To block jailbreak and prompt injection attempts, use the Guardrails prompt attack filter.
  • To mask PII in model responses, use a Guardrails sensitive information filter.
  • To detect hallucinations in RAG answers, use a contextual grounding check.
  • To use guardrails with models outside Bedrock, use the ApplyGuardrail API.
  • To require a guardrail on every invocation, use the bedrock:GuardrailIdentifier condition key.