1
+ name : " Terraform Workflow"
2
+
3
+ on :
4
+ workflow_call :
5
+ inputs :
6
+ environment :
7
+ type : string
8
+ required : true
9
+ aws-assume-role-arn :
10
+ type : string
11
+ required : true
12
+ aws-region :
13
+ type : string
14
+ required : true
15
+ aws-statefile-s3-bucket :
16
+ type : string
17
+ required : true
18
+ aws-lock-dynamodb-table :
19
+ type : string
20
+ required : true
21
+
22
+ jobs :
23
+ terraform :
24
+ runs-on : ubuntu-latest
25
+ defaults :
26
+ run :
27
+ shell : bash
28
+
29
+ steps :
30
+ - name : Checkout code
31
+ uses : actions/checkout@v4
32
+
33
+ - name : Setup Terraform
34
+ uses : hashicorp/setup-terraform@v3
35
+ with :
36
+ terraform_version : 1.8.3
37
+
38
+ - name : Configure AWS credentials
39
+ uses : aws-actions/configure-aws-credentials@v4
40
+ with :
41
+ role-to-assume : ${{ inputs.aws-assume-role-arn }}
42
+ role-session-name : GitHub_to_AWS_via_FederatedOIDC
43
+ aws-region : ${{ inputs.aws-region }}
44
+
45
+ - name : Read destroy configuration
46
+ id : read-destroy-config
47
+ run : |
48
+ DESTROY="$(jq -r '.${{ inputs.environment }}' ./infra/destroy_config.json)"
49
+ echo "destroy=$(echo $DESTROY)" >> $GITHUB_OUTPUT
50
+
51
+ - name : Terraform Init
52
+ run : |
53
+ cd infra && terraform init \
54
+ -backend-config="bucket=${{ inputs.aws-statefile-s3-bucket }}" \
55
+ -backend-config="key=${{ github.event.repository.name }}" \
56
+ -backend-config="region=${{ inputs.aws-region }}" \
57
+ -backend-config="dynamodb_table=${{ inputs.aws-lock-dynamodb-table }}"
58
+
59
+ - name : Terraform Validate
60
+ run : terraform validate
61
+
62
+ - name : Terraform Destroy
63
+ if : steps.read-destroy-config.outputs.destroy == 'true'
64
+ id : terraform-destroy
65
+ run : cd infra &&
66
+ terraform workspace select ${{ inputs.environment }} || terraform workspace new ${{ inputs.environment }} &&
67
+ terraform destroy -var-file="./envs/${{ inputs.environment }}/terraform.tfvars" -auto-approve
68
+
69
+ - name : Terraform Plan
70
+ if : steps.read-destroy-config.outputs.destroy != 'true'
71
+ id : terraform-plan
72
+ run : cd infra &&
73
+ terraform workspace select ${{ inputs.environment }} || terraform workspace new ${{ inputs.environment }} &&
74
+ terraform plan -var-file="./envs/${{ inputs.environment }}/terraform.tfvars" -out="${{ inputs.environment }}.plan"
75
+
76
+ - name : Terraform Apply
77
+ if : steps.read-destroy-config.outputs.destroy != 'true'
78
+ id : terraform-apply
79
+ run : cd infra &&
80
+ terraform workspace select ${{ inputs.environment }} || terraform workspace new ${{ inputs.environment }} &&
81
+ terraform apply "${{ inputs.environment }}.plan"
0 commit comments