Sentinel policy as code in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to check policies grows as we add more rules or resources.
How does the number of policy checks change when the input grows?
Analyze the time complexity of this Sentinel policy check in Terraform.
policy "example" {
rule "check_tags" {
all resources as r {
r.tags contains "environment"
}
}
}
This policy checks that every resource has an "environment" tag.
Look at what repeats when the policy runs.
- Primary operation: Checking each resource's tags for the "environment" key.
- How many times: Once for every resource in the plan.
As the number of resources grows, the number of checks grows too.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The checks grow directly with the number of resources.
Time Complexity: O(n)
This means the time to run the policy grows in a straight line as you add more resources.
[X] Wrong: "The policy runs in the same time no matter how many resources there are."
[OK] Correct: Each resource needs to be checked, so more resources mean more work.
Understanding how policy checks scale helps you design efficient rules and predict performance as infrastructure grows.
"What if the policy checked every tag on every resource instead of just one tag? How would the time complexity change?"
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
