Skip to content

Commit

Permalink
updated plan.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Morgan committed Oct 19, 2024
1 parent db0c866 commit 9cfc649
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
19 changes: 15 additions & 4 deletions .github/workflows/plan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,20 @@ jobs:
- name: Terraform Format
run: terraform fmt -check

# Generates an execution plan for Terraform
# Terraform Plan
- name: Terraform Plan
run: terraform plan -input=false
id: plan
run: |
terraform plan -out=plan.tfplan
terraform show -json plan.tfplan > /tmp/plan.json
cat /tmp/plan.json
- name: View Terraform Plan
run: terraform plan -no-color -json
- name: Setup OPA
uses: open-policy-agent/setup-opa@v2
with:
version: latest

- name: Run OPA Tests
run: |
opaout=$(opa eval --data ../policies/plan.rego --input /tmp/plan.json "data.terraform.deny" | jq -r '.result[].expressions[].value[]')
[ -z "$opaout" ] && exit 0 || echo "$opaout" && gh pr comment ${{ github.event.pull_request.number }} --body "### $opaout" && exit 1
Binary file added opa
Binary file not shown.
13 changes: 13 additions & 0 deletions policies/plan.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package terraform

import future.keywords.in

allowed_instance_types := {"t3.micro", "t3.small"}

deny[msg] {
some resource in input.resource_changes
resource.type == "aws_instance"
instance_type := resource.change.after.instance_type
not instance_type in allowed_instance_types
msg := sprintf("AWS instance type '%s' is not allowed. Only 't3.micro' or 't3.small' are permitted.", [instance_type])
}

0 comments on commit 9cfc649

Please sign in to comment.