What if your cloud could police itself and stop mistakes before they happen?
Why Sentinel policy as code in Terraform? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you manage cloud resources for your company. Every time someone creates or changes resources, you have to check manually if they follow the rules. You open spreadsheets, emails, and chat messages to verify compliance. This takes hours and is easy to miss.
Manual checks are slow and tiring. People can forget steps or make mistakes. If a rule is missed, it can cause security risks or extra costs. Fixing errors later wastes even more time and money.
Sentinel policy as code lets you write rules in code that automatically check your cloud changes. It runs every time you update resources, stopping bad changes before they happen. This saves time, reduces errors, and keeps your cloud safe.
Check each resource manually in spreadsheets and emails.
policy "check_tags" { rule = all resources have tags }It makes cloud governance automatic, consistent, and fast, so your team can focus on building instead of policing.
A company uses Sentinel policies to block any cloud server without proper security tags, preventing accidental exposure of sensitive data.
Manual cloud policy checks are slow and error-prone.
Sentinel policy as code automates and enforces rules consistently.
This leads to safer, faster, and more reliable cloud management.
Practice
Solution
Step 1: Understand Sentinel policy role
Sentinel policies are designed to enforce rules and guardrails on infrastructure changes.Step 2: Differentiate from other Terraform tasks
Writing configs and deploying resources are Terraform tasks, not Sentinel's role.Final Answer:
To enforce rules that control changes to cloud infrastructure -> Option AQuick Check:
Sentinel policy = enforce rules [OK]
- Confusing Sentinel with Terraform configuration writing
- Thinking Sentinel deploys resources
- Assuming Sentinel monitors usage
Solution
Step 1: Recall Sentinel policy syntax
Sentinel policies start with the keyword 'policy' followed by the policy name in quotes and curly braces.Step 2: Compare options
policy "example" { matches the correct syntax: policy "example" { ... }Final Answer:
policy "example" { -> Option BQuick Check:
Correct Sentinel block start = policy "name" { [OK]
- Omitting quotes around policy name
- Using '=' instead of '{' to start block
- Adding extra keywords like 'sentinel'
policy "check_tags" {
main = rule {
all tfplan.resource_changes as _, rc {
rc.change.after.tags contains "environment"
}
}
}What does this policy check?
Solution
Step 1: Analyze the 'all' keyword usage
The policy uses 'all' to check every resource change in the plan.Step 2: Understand the condition
It requires each resource's tags to contain the key "environment".Final Answer:
All resources must have a tag named "environment" -> Option AQuick Check:
all resources have "environment" tag = All resources must have a tag named "environment" [OK]
- Confusing 'all' with 'any' keyword
- Thinking it checks only one resource
- Ignoring the 'contains' check on tags
policy "check_region" {
main = rule {
all tfplan.resource_changes as _, rc {
rc.change.after.region is "us-east-1"
}
}
}Solution
Step 1: Check comparison operator
Sentinel uses '==' for equality, not 'is'. 'is' causes syntax error.Step 2: Verify other parts
The loop uses 'all' correctly. Policy name requires quotes. 'main = rule { }' is standard syntax.Final Answer:
Incorrect use of 'is' instead of '==' for comparison -> Option DQuick Check:
Use '==' for equality in Sentinel [OK]
- Using 'is' instead of '==' for comparisons
- Thinking policy name cannot be quoted
- Confusing rule and function syntax
Solution
Step 1: Identify the requirement
Policy must block plans creating EC2 instances missing 'owner' tag.Step 2: Choose correct logic
'all' ensures every EC2 instance resource has the 'owner' tag in the planned changes.Step 3: Evaluate other options
'any' would allow plans if just one has the tag, which is unsafe. Checking only first resource misses others. Allowing plans with no EC2 instances is unrelated to the requirement.Final Answer:
Use 'all' to check every resource of type 'aws_instance' has 'owner' tag in 'after' changes -> Option CQuick Check:
All EC2 instances must have 'owner' tag = Use 'all' to check every resource of type 'aws_instance' has 'owner' tag in 'after' changes [OK]
- Using 'any' instead of 'all' allowing missing tags
- Checking only one resource instead of all
- Ignoring resource type filtering
