-
Notifications
You must be signed in to change notification settings - Fork 0
106 lines (88 loc) · 2.89 KB
/
terraform_provision.yml
File metadata and controls
106 lines (88 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
name: Terraform Provision (EC2 S3 VPC)
on:
pull_request:
branches: [ main ]
push:
branches: [ main ]
workflow_dispatch:
env:
TF_ROOT: project1_refactor
TF_VERSION: 1.13.0
AWS_REGION: eu-west-2
concurrency:
group: terraform-${{ github.ref }}
cancel-in-progress: false
jobs:
terraform-provision:
runs-on: ubuntu-latest
permissions:
id-token: write # Required for OIDC
contents: read # To checkout repo
steps:
- name: 📥 Checkout repo
uses: actions/checkout@v4
- name: 🔐 Configure AWS credentials (OIDC)
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_OIDC_ROLE }}
aws-region: ${{ env.AWS_REGION }}
- name: 🧪 Verify Remote State S3 Backend
run: |
aws s3api head-bucket \
--bucket my-eu-tf-state-bucket
- name: 🧪 Verify DynamoDB Lock Table
run: |
aws dynamodb describe-table \
--table-name terraform-locks
- name: 🛠️ Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TF_VERSION }}
# Pre-merge checks:
# (uncomment to enable formatting check)
# - name: 🧼 Terraform Format
# run: |
# terraform -chdir=${{ env.TF_ROOT }} \
# fmt -check -recursive
- name: 📄 Terraform Init
run: |
terraform -chdir=${{ env.TF_ROOT }} \
init \
-input=false \
-backend-config="region=${{ env.AWS_REGION }}"
- name: 🔎 Terraform Validate
run: |
terraform -chdir=${{ env.TF_ROOT }} \
validate
# Optional: Security linting with tfsec (uncomment to enable)
# - name: 🔐 tfsec (Terraform security scan)
# uses: aquasecurity/tfsec-pr-commenter-action@v1.4.0
# with:
# working_directory: ${{ env.TF_ROOT }}
# github_token: ${{ secrets.GITHUB_TOKEN }}
- name: 📊 Terraform Plan
run: |
terraform -chdir=${{ env.TF_ROOT }} \
plan \
-out=tfplan
# Post-merge checks
# Manually triggered for main branch (workflow_dispatch)
- name: 🚀 Terraform Apply
if: github.event_name == 'workflow_dispatch'
run: |
terraform -chdir=${{ env.TF_ROOT }} \
apply \
-auto-approve \
tfplan
# Auto apply on main branch
# - name: 🚀 Terraform Apply
# if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
# run: |
# terraform -chdir=${{ env.TF_ROOT }} \
# apply -auto-approve tfplan
# Output Terraform outputs
- name: 📤 Terraform Output
if: github.event_name == 'workflow_dispatch'
run: |
terraform -chdir=${{ env.TF_ROOT }} \
output -json