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