From 56e12af8fc35922252ed1023ba9d767d845027ee Mon Sep 17 00:00:00 2001 From: $Cuentas-AppServices Date: Thu, 9 Nov 2023 18:01:16 +0000 Subject: [PATCH] added terraform code --- cloud_function/main.py | 3 +- cloud_function/repository.py | 2 +- cloud_function/services.py | 4 +- main.tf | 73 ++++++++++++++++++++++++++++++++++++ tests/__init__.py | 3 ++ variables.tf | 27 +++++++++++++ 6 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 main.tf create mode 100644 variables.tf diff --git a/cloud_function/main.py b/cloud_function/main.py index a79183d..bf941e0 100644 --- a/cloud_function/main.py +++ b/cloud_function/main.py @@ -1,4 +1,5 @@ -from cloud_function import repository, services +import repository +import services def func_entry_point(event, context): diff --git a/cloud_function/repository.py b/cloud_function/repository.py index b1e75e6..751137c 100644 --- a/cloud_function/repository.py +++ b/cloud_function/repository.py @@ -1,6 +1,6 @@ from abc import ABC, abstractmethod from google.cloud import bigquery -from cloud_function import model +import model class AbstractRepository(ABC): diff --git a/cloud_function/services.py b/cloud_function/services.py index 3f606ef..cb905bf 100644 --- a/cloud_function/services.py +++ b/cloud_function/services.py @@ -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): diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..01e4855 --- /dev/null +++ b/main.tf @@ -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 + } +} \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py index 25a0fc3..f93b7e1 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,5 +1,6 @@ import os from dotenv import load_dotenv +import sys def env_var_loader(file_name, file_path=None): @@ -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')) diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..6c5fb5b --- /dev/null +++ b/variables.tf @@ -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" +} \ No newline at end of file