Skip to content

Instantly share code, notes, and snippets.

@JonnyDaenen
Created June 10, 2021 15:56
Show Gist options
  • Save JonnyDaenen/a5ff9c8555c99b897a672ff8337ab2b5 to your computer and use it in GitHub Desktop.
Save JonnyDaenen/a5ff9c8555c99b897a672ff8337ab2b5 to your computer and use it in GitHub Desktop.
Deployment of Cloud Function via terraform
resource "google_storage_bucket" "default" {
name = "cf-bucket"
location = var.data_location_storage
}
data "local_file" "py_main" {
filename = "${path.root}/../../../../cloudfunction/main.py"
depends_on = [
# Make sure archive is created in apply stage
null_resource.trigger
]
}
data "local_file" "py_req" {
filename = "${path.root}/../../../../cloudfunction/requirements.txt"
depends_on = [
# Make sure archive is created in apply stage
null_resource.trigger
]
}
# Dummy resource to ensure archive is created at apply stage
resource "null_resource" "trigger" {
triggers = {
timestamp = timestamp()
}
}
data "archive_file" "cf_zip" {
type = "zip"
output_path = "${path.root}/../../../../tmp/cf.zip"
source {
content = data.local_file.py_main.content
filename = "main.py"
}
source {
content = data.local_file.py_req.content
filename = "requirements.txt"
}
}
resource "google_storage_bucket_object" "archive" {
name = "cf-archive-${data.archive_file.cf_zip.output_md5}.zip"
bucket = google_storage_bucket.default.name
source = data.archive_file.cf_zip.output_path
}
resource "google_cloudfunctions_function" "function" {
project = var.project
name = "your-cf"
description = "your-cf"
runtime = "python38"
service_account_email = "<your-sa>"
available_memory_mb = 128
source_archive_bucket = google_storage_bucket.default.name
source_archive_object = google_storage_bucket_object.archive.name
timeout = 30
entry_point = "main"
event_trigger {
event_type = "google.pubsub.topic.publish"
resource = google_pubsub_topic.events.name
}
labels = {
hash = data.archive_file.cf_zip.output_md5
}
environment_variables = {
CF_HASH = data.archive_file.cf_zip.output_md5
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment