Skip to content

Commit 90555b3

Browse files
authored
Merge pull request #5 from anqorithm/feat/terraform-impl
feat: implement Terraform configuration for Cloud Run deployment and …
2 parents 9f3bbe6 + cbc2eaf commit 90555b3

File tree

12 files changed

+202
-2
lines changed

12 files changed

+202
-2
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
.env.local
44
.env.*.local
55

6+
# Service Account Keys
7+
*.json
8+
sa.json
9+
gcps.json
10+
11+
# Terraform
12+
terraform/terraform.tfvars
13+
terraform/.terraform/
14+
terraform/*.tfstate
15+
terraform/*.tfstate.backup
16+
617
# Binaries
718
/bin/
819
*.exe

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ COPY . .
1515
RUN go build -o main ./cmd/api
1616

1717
# App port
18-
EXPOSE 3000
18+
EXPOSE 8080
1919

2020
# Run application
2121
CMD ["./main"]

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
dev:
2-
air
2+
air
3+
4+
deploy:
5+
./build-and-deploy.sh

build-and-deploy.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
set -e
3+
4+
PROJECT_ID="ai-credit-product"
5+
SERVICE_NAME="naqa-api"
6+
REGION="us-central1"
7+
IMAGE_NAME="gcr.io/$PROJECT_ID/$SERVICE_NAME"
8+
9+
echo "🚀 Building and deploying $SERVICE_NAME..."
10+
11+
docker build -t $IMAGE_NAME:latest .
12+
docker push $IMAGE_NAME:latest
13+
14+
cd terraform
15+
terraform init
16+
terraform apply -auto-approve
17+
18+
echo "✅ Deployment completed!"

cloudbuild.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
steps:
2+
- name: 'gcr.io/cloud-builders/docker'
3+
args: ['build', '-t', 'gcr.io/$PROJECT_ID/naqa-api:latest', '.']
4+
- name: 'gcr.io/cloud-builders/docker'
5+
args: ['push', 'gcr.io/$PROJECT_ID/naqa-api:latest']
6+
7+
images:
8+
- 'gcr.io/$PROJECT_ID/naqa-api:latest'

terraform/.terraform.lock.hcl

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/cloud_run.tf

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
resource "google_cloud_run_service" "naqa_api" {
2+
name = var.service_name
3+
location = var.region
4+
project = var.project_id
5+
6+
depends_on = [google_project_service.cloud_run_api]
7+
8+
template {
9+
spec {
10+
containers {
11+
image = "gcr.io/${var.project_id}/${var.service_name}:latest"
12+
13+
ports {
14+
container_port = 8080
15+
}
16+
17+
env {
18+
name = "ENVIRONMENT"
19+
value = "production"
20+
}
21+
22+
env {
23+
name = "APP_NAME"
24+
value = "Naqa API"
25+
}
26+
27+
env {
28+
name = "API_VERSION"
29+
value = "1.0.0"
30+
}
31+
32+
env {
33+
name = "MONGO_URI"
34+
value = var.mongo_uri
35+
}
36+
37+
env {
38+
name = "MONGO_DATABASE"
39+
value = var.mongo_database
40+
}
41+
42+
resources {
43+
limits = {
44+
memory = "1Gi"
45+
cpu = "1000m"
46+
}
47+
}
48+
}
49+
50+
container_concurrency = 1000
51+
}
52+
53+
metadata {
54+
annotations = {
55+
"autoscaling.knative.dev/maxScale" = "100"
56+
"autoscaling.knative.dev/minScale" = "0"
57+
}
58+
}
59+
}
60+
61+
traffic {
62+
percent = 100
63+
latest_revision = true
64+
}
65+
}
66+
67+
resource "google_cloud_run_service_iam_binding" "public_access" {
68+
location = google_cloud_run_service.naqa_api.location
69+
project = google_cloud_run_service.naqa_api.project
70+
service = google_cloud_run_service.naqa_api.name
71+
role = "roles/run.invoker"
72+
members = [
73+
"allUsers",
74+
]
75+
}

terraform/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_providers {
3+
google = {
4+
source = "hashicorp/google"
5+
version = "~> 4.0"
6+
}
7+
}
8+
}
9+
10+
provider "google" {
11+
credentials = file("../sa.json")
12+
project = var.project_id
13+
region = var.region
14+
}

terraform/outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "service_url" {
2+
value = google_cloud_run_service.naqa_api.status[0].url
3+
}
4+
5+
output "service_name" {
6+
value = google_cloud_run_service.naqa_api.name
7+
}

terraform/services.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource "google_project_service" "cloud_run_api" {
2+
project = var.project_id
3+
service = "run.googleapis.com"
4+
}
5+
6+
resource "google_project_service" "container_registry_api" {
7+
project = var.project_id
8+
service = "containerregistry.googleapis.com"
9+
}
10+
11+
resource "google_project_service" "cloud_build_api" {
12+
project = var.project_id
13+
service = "cloudbuild.googleapis.com"
14+
}

0 commit comments

Comments
 (0)