Skip to content

Instantly share code, notes, and snippets.

output "service_account_email" {
value = google_service_account.service_account.email
description = "The service-account that the function runs under."
}
output "source_bucket" {
value = google_storage_bucket.function.name
description = "The bucket created to hold the function source code"
}
variable "project_id" {
type = string
description = "The project where the function should be deployed"
}
variable "function_name" {
type = string
description = "The name of this function"
}
resource "google_project_service" "cloud-functions" {
project = var.project_id
service = "cloudfunctions.googleapis.com"
disable_on_destroy = false
}
resource "google_project_service" "cloudbuild" {
project = var.project_id
service = "cloudbuild.googleapis.com"
resource "google_cloudfunctions_function" "function" {
name = var.function_name
project = var.project_id
available_memory_mb = var.available_memory_mb
timeout = var.timeout
region = var.region
service_account_email = google_service_account.service_account.email
runtime = var.runtime
entry_point = var.entry_point
resource "random_id" "service-account" {
byte_length = 4
}
resource "google_service_account" "service_account" {
account_id = "function-${random_id.service-account.hex}"
display_name = "Service account for the ${var.function_name} cloud-function"
project = var.project_id
}
data "google_client_openid_userinfo" "terraform" {}
data "archive_file" "function" {
type = "zip"
output_path = "${path.module}/function.zip"
dynamic "source" {
for_each = toset(var.source_files)
content {
@dianad96
dianad96 / bucket.tf
Last active November 1, 2020 13:40
Http Cloud Function Boilerplate Code Module
resource "random_id" "bucket" {
byte_length = 6
}
resource "google_storage_bucket" "function" {
project = var.project_id
name = "${var.project_id}_${random_id.bucket.hex}"
location = "EU"
storage_class = "MULTI_REGIONAL"
<form>
<label>Admin Dashboard</label>
<fieldset submitButton="false"></fieldset>
<row>
<panel>
<title>Number of Active Users</title>
<single>
<search>
<query>| dbxquery query="SELECT count(*) FROM user" connection="Local"</query>
<earliest>-24h@h</earliest>
<input type="dropdown" token="site_month">
 <label>Site</label>
 <fieldForLabel>name</fieldForLabel>
 <fieldForValue>id</fieldForValue>
 <choice value="`site_id`">All</choice>
 <search>
 <query>| dbxquery query="SELECT name, CONCAT('\'',id,'\'') AS id FROM site" connection="Local"</query>
 <earliest>-24h@h</earliest>
 <latest>now</latest>
 </search>
| dbxquery query="SELECT modality, SUM(amount) as amount, MONTH(created_on) AS month FROM TRANSACTION WHERE STATUS='success' AND site_id=$site_month$ AND site_id IN (SELECT id FROM site WHERE organization_id=$org_month$) AND YEAR(created_on) = YEAR(CURRENT_DATE()) GROUP BY modality, MONTH(created_on)" connection="Local"| chart first(amount) over month by modality