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

Legg inn ny service account #112

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
54 changes: 27 additions & 27 deletions terraform/modules/cloud_function/function.tf
Original file line number Diff line number Diff line change
@@ -1,75 +1,71 @@
data "archive_file" "this" {
# Compress the function source code into a ZIP file
data "archive_file" "function_zip" {
type = "zip"
output_path = "${path.module}/lambda-files.zip"
source_dir = var.function_folder_location
excludes = var.excludes
}

resource "google_storage_bucket_object" "this" {
name = "${var.name}.${data.archive_file.this.output_sha}.zip"
# Upload the ZIP file to a GCS bucket
resource "google_storage_bucket_object" "function_zip" {
name = "${var.name}-${data.archive_file.function_zip.output_sha}.zip"
bucket = var.bucket_id
source = data.archive_file.this.output_path
source = data.archive_file.function_zip.output_path
}

resource "google_cloudfunctions2_function" "this" {
# Define the Cloud Function resource
resource "google_cloudfunctions2_function" "cloud_function" {
name = var.name
location = var.location
description = var.description
project = var.project
labels = var.labels

lifecycle { ignore_changes = [build_config[0].source[0].storage_source[0].generation, build_config[0].docker_repository] }

build_config {
runtime = var.runtime
entry_point = var.entry_point

source {
storage_source {
bucket = var.bucket_id
object = google_storage_bucket_object.this.name
object = google_storage_bucket_object.function_zip.name
}
}
}

service_config {
available_memory = var.available_memory
min_instance_count = var.min_instance_count
max_instance_count = var.max_instance_count
timeout_seconds = var.timeout_seconds
environment_variables = var.environment_variables
ingress_settings = var.ingress_settings
all_traffic_on_latest_revision = var.all_traffic_on_latest_revision
service_account_email = var.service_account_email
available_memory = var.available_memory
min_instance_count = var.min_instance_count
max_instance_count = var.max_instance_count
timeout_seconds = var.timeout_seconds
environment_variables = var.environment_variables
ingress_settings = var.ingress_settings
service_account_email = var.service_account_email
}
}

# Grant permissions for invoking the function
resource "google_cloudfunctions2_function_iam_member" "invoker" {
project = var.project
location = var.location
cloud_function = google_cloudfunctions2_function.this.name
cloud_function = google_cloudfunctions2_function.cloud_function.name
role = "roles/cloudfunctions.invoker"
member = "serviceAccount:${var.service_account_email}"
}

resource "google_project_iam_member" "token_creator" {
project = var.project
role = "roles/iam.serviceAccountTokenCreator"
member = "serviceAccount:${var.service_account_email}"
}

resource "google_cloud_scheduler_job" "invoke_cloud_function" {
# Define the Cloud Scheduler job that triggers the Cloud Function
resource "google_cloud_scheduler_job" "cloud_scheduler_job" {
for_each = { for idx, val in var.schedule_params : idx => val }
name = "invoke-${var.name}${each.value.body != null ? each.value.body.job_postfix : ""}"
description = "Schedule the HTTPS trigger for cloud function"
schedule = each.value.schedule
time_zone = "Europe/Oslo"
project = google_cloudfunctions2_function.this.project
region = google_cloudfunctions2_function.this.location
project = google_cloudfunctions2_function.cloud_function.project
region = google_cloudfunctions2_function.cloud_function.location
attempt_deadline = "${var.timeout_seconds}s"

http_target {
uri = google_cloudfunctions2_function.this.service_config[0].uri
uri = google_cloudfunctions2_function.cloud_function.service_config[0].uri
http_method = "POST"
body = base64encode(jsonencode(each.value.body))
headers = {
Expand All @@ -80,4 +76,8 @@ resource "google_cloud_scheduler_job" "invoke_cloud_function" {
service_account_email = var.service_account_email
}
}

depends_on = [
google_cloudfunctions2_function.cloud_function
]
}
24 changes: 0 additions & 24 deletions terraform/modules/cloud_function/outputs.tf

This file was deleted.