Skip to content

Commit a695739

Browse files
Fixed terraform director
1 parent cedca9d commit a695739

3 files changed

Lines changed: 33 additions & 7 deletions

File tree

.github/workflows/deploy.yaml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ on:
88
workflow_dispatch:
99

1010
env:
11-
TF_WORKING_DIR: terraform/
11+
TF_WORKING_DIR: terraform
1212

1313
jobs:
1414
terraform:
1515
name: Terraform (plan & apply)
1616
runs-on: ubuntu-latest
1717
environment: ${{ github.ref == 'refs/heads/main' && 'prod' || 'staging' }}
18+
1819
permissions:
1920
contents: read
2021

@@ -34,9 +35,15 @@ jobs:
3435
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
3536
aws-region: ${{ secrets.AWS_REGION }}
3637

38+
# Backend selection based on branch
3739
- name: Terraform Init
3840
working-directory: ${{ env.TF_WORKING_DIR }}
39-
run: terraform init -input=false
41+
run: |
42+
if [ "${{ github.ref }}" = "refs/heads/staging" ]; then
43+
terraform init -reconfigure -backend-config=backend-staging.tfvars -input=false
44+
else
45+
terraform init -reconfigure -backend-config=backend-prod.tfvars -input=false
46+
fi
4047
4148
- name: Terraform Validate & Format
4249
working-directory: ${{ env.TF_WORKING_DIR }}
@@ -54,17 +61,17 @@ jobs:
5461
terraform plan -var-file="prod.tfvars" -out=tfplan
5562
fi
5663
57-
- name: Terraform Apply
64+
- name: Terraform Apply (staging)
5865
if: github.ref == 'refs/heads/staging'
5966
working-directory: ${{ env.TF_WORKING_DIR }}
6067
run: terraform apply -input=false -auto-approve tfplan
6168

62-
- name: Terraform Apply (prod) - requires env approval
69+
- name: Terraform Apply (prod)
6370
if: github.ref == 'refs/heads/main'
6471
working-directory: ${{ env.TF_WORKING_DIR }}
6572
run: terraform apply -input=false -auto-approve tfplan
6673

67-
- name: Show outputs
74+
- name: Show Outputs
6875
if: success()
6976
working-directory: ${{ env.TF_WORKING_DIR }}
7077
run: terraform output -json

.github/workflows/destroy.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ env:
1616

1717
jobs:
1818
destroy:
19+
name: Terraform Destroy
1920
runs-on: ubuntu-latest
21+
environment: ${{ github.event.inputs.environment }}
22+
23+
permissions:
24+
contents: read
2025

2126
steps:
2227
- name: Checkout
@@ -34,9 +39,15 @@ jobs:
3439
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
3540
aws-region: ${{ secrets.AWS_REGION }}
3641

42+
# Backend selection based on input
3743
- name: Terraform Init
3844
working-directory: ${{ env.TF_WORKING_DIR }}
39-
run: terraform init -input=false
45+
run: |
46+
if [ "${{ github.event.inputs.environment }}" = "staging" ]; then
47+
terraform init -reconfigure -backend-config=backend-staging.tfvars -input=false
48+
else
49+
terraform init -reconfigure -backend-config=backend-prod.tfvars -input=false
50+
fi
4051
4152
- name: Terraform Destroy
4253
working-directory: ${{ env.TF_WORKING_DIR }}

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ Run the python funtion locally using VS Code Run Button
3131

3232
- Create the terraform folder structure
3333

34-
- Deploy with: terraform init then terraform apply -var-file="staging.tfvars" (or prod.tfvars)
34+
- Deploy with: terraform init then terraform apply -var-file="staging.tfvars" (or prod.tfvars)
35+
36+
37+
endpoint - https://nrbefv9bcj.execute-api.us-east-1.amazonaws.com/health
38+
39+
40+
terraform init -backend-config=backend-staging.tfvars for staging environment
41+
42+
terraform init -backend-config=backend-prod.tfvars for prod environment

0 commit comments

Comments
 (0)