Skip to content

Commit 91ea00d

Browse files
committed
Enhance Terraform configuration: add Google Cloud Storage bucket for state management with versioning and uniform access; update .gitignore to exclude auth.json
1 parent e1247fc commit 91ea00d

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

.github/workflows/terraform.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ jobs:
1111
name: 'Terraform'
1212
runs-on: ubuntu-latest
1313
env:
14-
TF_VAR_project_id: ${{ secrets.GCP_PROJECT_ID }} # Make sure to set this secret in your repository
14+
TF_VAR_project_id: ${{ secrets.GCP_PROJECT_ID }}
15+
TF_VAR_region: ${{ secrets.GCP_REGION }}
16+
TF_VAR_environment: ${{ secrets.GCP_ENVIRONMENT }}
1517

1618
steps:
1719
- name: 'Checkout'
@@ -20,17 +22,20 @@ jobs:
2022
- name: 'Authenticate to Google Cloud'
2123
uses: 'google-github-actions/auth@v2'
2224
with:
23-
credentials_json: '${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}' # Make sure to set this secret in your repository
25+
credentials_json: '${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}'
26+
27+
- name: 'Set up Google Cloud SDK'
28+
uses: 'google-github-actions/setup-gcloud@v2'
2429

2530
- name: 'Set up Terraform'
2631
uses: hashicorp/setup-terraform@v3
2732
with:
28-
terraform_version: latest # Or specify a version e.g., 1.0.0
33+
terraform_version: latest
2934

3035
- name: 'Terraform Init'
3136
id: init
3237
run: terraform init
33-
working-directory: ./terraform # Assuming your Terraform files are in a 'terraform' subdirectory
38+
working-directory: ./terraform
3439

3540
- name: 'Terraform Validate'
3641
id: validate
@@ -41,14 +46,12 @@ jobs:
4146
id: plan
4247
run: terraform plan -no-color -input=false -out=tfplan
4348
working-directory: ./terraform
44-
# Only run on pull requests or direct pushes to main (not on merges)
4549
if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
4650

4751
- name: 'Terraform Apply'
4852
id: apply
4953
run: terraform apply -auto-approve -input=false tfplan
5054
working-directory: ./terraform
51-
# Only run on pushes to the main branch (e.g., after a PR is merged)
5255
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
5356

5457
- name: 'Check BigQuery Dataset and Load Titanic Data'
@@ -57,5 +60,4 @@ jobs:
5760
chmod +x ../scripts/check_and_load_titanic_data.sh
5861
../scripts/check_and_load_titanic_data.sh ${{ secrets.GCP_PROJECT_ID }}
5962
working-directory: ./terraform
60-
# Only run on pushes to the main branch (after terraform apply)
6163
if: github.ref == 'refs/heads/main' && github.event_name == 'push'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ venv.bak/
105105
*.tfvars
106106
.terraform/
107107
.terraform*
108+
auth.json
108109

109110
# Spyder project settings
110111
.spyderproject

terraform/backend.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
terraform {
2+
backend "gcs" {
3+
bucket = "agentic-data-science-460701-terraform-state"
4+
prefix = "terraform/state"
5+
}
6+
}

terraform/main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ provider "google" {
44
region = var.region
55
}
66

7+
# Cloud Storage bucket for Terraform state
8+
resource "google_storage_bucket" "terraform_state" {
9+
name = "${var.project_id}-terraform-state"
10+
location = var.region
11+
12+
# Prevent accidental deletion of this bucket
13+
lifecycle {
14+
prevent_destroy = true
15+
}
16+
# Enable versioning for state file history
17+
versioning {
18+
enabled = true
19+
}
20+
21+
# Enable uniform bucket-level access
22+
uniform_bucket_level_access = true
23+
24+
labels = {
25+
environment = var.environment
26+
purpose = "terraform-state"
27+
}
28+
}
29+
730
resource "google_bigquery_dataset" "test_dataset" {
831
dataset_id = "test_dataset"
932
friendly_name = "test_dataset"

0 commit comments

Comments
 (0)