Update terraform.yaml #51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Terraform workflow | |
on: push | |
env: | |
AWS_REGION: us-east-1 | |
GH_TOKEN: ${{ github.token }} | |
TF_IN_AUTOMATION: true | |
permissions: | |
id-token: write | |
contents: read | |
jobs: | |
terraform: | |
runs-on: ubuntu-24.04 | |
steps: | |
- name: AWS Authentication | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-region: ${{ env.AWS_REGION }} | |
role-to-assume: ${{ secrets.AWS_IAM_ROLE }} | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Setup terraform | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: "1.10.3" | |
- name: Config Terraform plugin cache | |
run: mkdir -pv ~/.terraform.d/plugin-cache | |
- name: Cache Terraform Plugins | |
uses: actions/cache@v4 | |
with: | |
path: ~/.terraform.d/plugin-cache | |
key: ${{ runner.os }}-terraform-cache-${{ hashFiles('**/.terraform.lock.hcl') }} | |
restore-keys: ${{ runner.os }}-terraform-cache- | |
- name: Initialize terraform | |
run: | | |
export TF_PLUGIN_CACHE_DIR="${HOME}/.terraform.d/plugin-cache" | |
terraform init \ | |
-backend-config="bucket=${{ secrets.TF_S3_BACKEND_BUCKET_NAME }}" \ | |
-backend-config="key=${{ secrets.TF_S3_BACKEND_KEY }}" \ | |
-backend-config="dynamodb_table=${{ secrets.TF_S3_BACKEND_DYNAMODB_TABLE }}" \ | |
-backend-config="assume_role={role_arn=\"${{ secrets.TF_S3_BACKEND_ROLE_ARN }}\"}" | |
- name: Terraform plan | |
run: | | |
terraform plan -out terraform-plan.binary | |
terraform show -json terraform-plan.binary > terraform-plan.json | |
- name: Setup OPA | |
uses: open-policy-agent/setup-opa@v2 | |
with: | |
version: 1.0.0 | |
if: github.event_name == 'pull_request' | |
- name: Run OPA Tests | |
run: | | |
opa_output=$(opa eval --data ${{ github.workspace }}/policies/allowed_ec2_instance_types.rego --input ${{ github.workspace }}/terraform-plan.json "data.terraform.deny" | jq -r '.result[].expressions[].value[]') | |
[ -z "$opa_output" ] && exit 0 || echo "$opa_output" && gh pr comment ${{ github.event.pull_request.number }} --body "### $opa_output" && exit 1 | |
if: github.event_name == 'pull_request' | |
- name: List state file resources | |
run: terraform state list | |
- name: Run Checkov action | |
id: checkov | |
uses: bridgecrewio/checkov-action@v12 | |
with: | |
file: terraform-plan.json | |
output_format: cli | |
soft_fail: false | |
download_external_modules: true | |