Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform Integration #1

Merged
merged 24 commits into from
Feb 18, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test files
7174Andy committed Jan 28, 2025
commit 76f798d459c12259dae293c87f8ffffe48993182
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use a lightweight Python base image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file
COPY app/requirements.txt requirements.txt

# Install Python dependencies
RUN pip install -r requirements.txt

# Copy the app code into the container
COPY ./app .

# Expose the port Flask will run on
EXPOSE 8080

# Command to run the Flask app
CMD ["python", "app.py"]
17 changes: 17 additions & 0 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from flask import Flask

app = Flask(__name__)


@app.route("/")
def home():
return "Hello, World! This is a test Flask app."


@app.route("/health")
def health():
return {"status": "healthy"}, 200


if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
1 change: 1 addition & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flask==2.2.5
19 changes: 19 additions & 0 deletions terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 18 additions & 25 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
variable "resource_suffic" {
description = "Suffix to append to all resources"
type = string
default = "prod"
}

locals {
project = var.project_id
service_name = "cosyne-service"
database_name = "test-database-users"
service_name = "${var.resource_suffix}-cosyne-service"
database_name = "users"
}

# TODO: Add resources for Terraform to manage
@@ -24,24 +18,23 @@ resource "google_spanner_database" "default" {
deletion_protection = false
}

resource "google_cloud_run_v2_service" "deployment" {
name = local.service_name
location = "us-central1"
project = var.project_id
deletion_protection = false
template {
containers {
image = "gcr.io/${var.project_id}/cosyne:latest"
}
}
traffic {
percent = 100
}
# Push an image to Google Container Registry
# NOTE: We can automate the process if we have the vmassign repo combined with this repo
# Google Artifact Registry
resource "google_artifact_registry_repository" "repo" {
format = "DOCKER"
location = "us-central1"
repository_id = "flask-app-repo"
}

data "google_iam_policy" "noauth" {
binding {
role = "roles/run.invoker"
members = ["allUsers"]
resource "null_resource" "docker_build_and_push" {
provisioner "local-exec" {
command = <<EOT
gcloud auth configure-docker
docker build -t us-central1-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.repo.name}/flask-app:latest ../
docker push us-central1-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.repo.name}/flask-app:latest
EOT
}
}

# Deployment using Cloud Run
13 changes: 0 additions & 13 deletions terraform/output.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
# output "instance_id" {
# description = "Instance ID"
# value = [for instance in google_compute_instance.test_instance : instance.id]
# }

# output "instance_name" {
# description = "Instance Name"
# value = [for instance in google_compute_instance.test_instance : instance.name]
# }

output "service_url" {
description = "Service URL"
value = google_cloud_run_v2_service.deployment.uri
}