Skip to content

Commit

Permalink
added terraform code
Browse files Browse the repository at this point in the history
  • Loading branch information
antjes88 committed Nov 9, 2023
1 parent f901416 commit 56e12af
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cloud_function/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from cloud_function import repository, services
import repository
import services


def func_entry_point(event, context):
Expand Down
2 changes: 1 addition & 1 deletion cloud_function/repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod
from google.cloud import bigquery
from cloud_function import model
import model


class AbstractRepository(ABC):
Expand Down
4 changes: 2 additions & 2 deletions cloud_function/services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from cloud_function.repository import AbstractRepository
from cloud_function import model
from repository import AbstractRepository
import model


def irr_pipeline(repository: AbstractRepository):
Expand Down
73 changes: 73 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
provider "google" {
project = var.project_id
region = var.region
}

resource "google_service_account" "default" {
account_id = var.service_account_name
display_name = "IRR Calculator Cloud Function SA"
}

resource "google_project_iam_member" "big_query_writer" {
project = var.project_id
role = "roles/bigquery.dataEditor"
member = "serviceAccount:${google_service_account.default.email}"
}

resource "google_project_iam_member" "big_query_jobUser" {
project = var.project_id
role = "roles/bigquery.jobUser"
member = "serviceAccount:${google_service_account.default.email}"
}

resource "google_storage_bucket" "source_code" {
name = "irr-calculator-source-code-location"
storage_class = "STANDARD"
location = var.region
uniform_bucket_level_access = true
}

data "archive_file" "source" {
type = "zip"
source_dir = "${path.root}/cloud_function"
output_path = "${path.root}/zip_to_cloud_function.zip"
}

resource "google_storage_bucket_object" "zip" {
name = "cloud-function-source-code-for-${var.cloud_function_name}.zip"
bucket = google_storage_bucket.source_code.name
source = data.archive_file.source.output_path
}

resource "google_pubsub_topic" "default" {
name = "cloud-function-${var.cloud_function_name}"
}

resource "google_cloud_scheduler_job" "default" {
name = "cloud-function-${var.cloud_function_name}"
description = "Scheduler to trigger the cloud function: ${var.cloud_function_name}"
schedule = "30 0 * * *"

pubsub_target {
topic_name = google_pubsub_topic.default.id
data = base64encode("Trigger Cloud Function")
}
}

resource "google_cloudfunctions_function" "irr_calculator" {
name = var.cloud_function_name

runtime = "python310"
available_memory_mb = 512
timeout = 539
source_archive_bucket = google_storage_bucket.source_code.name
source_archive_object = google_storage_bucket_object.zip.name
entry_point = var.function_entry_point
service_account_email = google_service_account.default.email
max_instances = 1

event_trigger {
event_type = "google.pubsub.topic.publish"
resource = google_pubsub_topic.default.name
}
}
3 changes: 3 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from dotenv import load_dotenv
import sys


def env_var_loader(file_name, file_path=None):
Expand All @@ -21,3 +22,5 @@ def env_var_loader(file_name, file_path=None):


env_var_loader("tests/.env")

sys.path.append(os.path.join(os.getcwd(), 'cloud_function'))
27 changes: 27 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
variable "project_id" {
type = string
description = "Name of the Google Project"
}

variable "region" {
type = string
default = "europe-west2"
description = "Location for the resources"
}

variable "cloud_function_name" {
type = string
description = "Name of the ECB Api Caller Cloud Function"
}

variable "function_entry_point" {
type = string
default = "func_entry_point"
description = "Name of the function entry point for the Python solution at main.py"
}


variable "service_account_name" {
type = string
description = "Name of the Service Account"
}

0 comments on commit 56e12af

Please sign in to comment.