Skip to content

Cloud Function Implemented #1

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

Open
wants to merge 3 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# node modules directories
**/node_modules/*
# Local .terraform directories
**/.terraform/*

Expand Down
Binary file modified README.md
Binary file not shown.
20 changes: 20 additions & 0 deletions example/test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module "gcp_function" {
source = "../../"
source_filename = var.source_filename
source_dir = var.source_dir
function_name = var.function_name
function_entry_point = var.function_entry_point
function_invoker_user = var.function_invoker_user
project = var.project
runtime = var.runtime
environment_variables = var.environment_variables
}

variable "source_filename" { type = string }
variable "source_dir" { type = string }
variable "function_name" { type = string }
variable "function_entry_point" {}
variable "function_invoker_user" { type = string }
variable "project" { type = string }
variable "runtime" { type = string }
variable "environment_variables" { type = map(string) }
4 changes: 4 additions & 0 deletions example/test/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "function_url" {
# Access the module output with module.<module_name>.<output_name>
value = module.gcp_function.function_url
}
11 changes: 11 additions & 0 deletions example/test/src/nodjs_sample/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fastify = require('fastify')
const app = fastify({ logger: true })

app.get('/', async (req, res) => {
return { works: true }
})

exports.app = async (req, res) => {
await app.ready()
app.server.emit('request', req, res)
}
14 changes: 14 additions & 0 deletions example/test/src/nodjs_sample/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "function",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"fastify": "^3.24.1"
}
}
21 changes: 21 additions & 0 deletions example/test/src/python_sample/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import logging


def http_handler(request):
"""
HTTP Cloud Function that returns the value of FOO.
Args:
request (flask.Request): The request object.
<http://flask.pocoo.org/docs/1.0/api/#flask.Request>
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
"""

logging.info('Handler Started!')

foo = os.environ.get('FOO')
response_body = f'The value of FOO is {foo}'
return response_body
1 change: 1 addition & 0 deletions example/test/src/python_sample/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Requirements added here will automatically downloaded by the Cloud Function
36 changes: 27 additions & 9 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
locals {
timestamp = formatdate("YYMMDDhhmmss", timestamp())
root_dir = abspath("../")
}

# Create bucket that will host the source code
Expand All @@ -12,7 +11,7 @@ resource "google_storage_bucket" "function_bucket" {
# Compress source code
data "archive_file" "source" {
type = "zip"
source_dir = var.filename
source_dir = var.source_dir
output_path = "/tmp/function-${local.timestamp}.zip"
}

Expand All @@ -29,14 +28,22 @@ resource "google_service_account" "service_account" {
display_name = "Cloud Function Tutorial Invoker Service Account"
}

# Create IAM entry so all users can invoke the function
resource "google_cloudfunctions_function_iam_member" "invoker" {
project = google_cloudfunctions_function.function.project
region = google_cloudfunctions_function.function.region
cloud_function = google_cloudfunctions_function.function.name
# Enable Cloud Functions API
resource "google_project_service" "cf" {
project = var.project
service = "cloudfunctions.googleapis.com"

role = "roles/cloudfunctions.invoker"
member = "allUsers"
disable_dependent_services = true
disable_on_destroy = false
}

# Enable Cloud Build API
resource "google_project_service" "cb" {
project = var.project
service = "cloudbuild.googleapis.com"

disable_dependent_services = true
disable_on_destroy = false
}

# Create Cloud Function
Expand All @@ -49,4 +56,15 @@ resource "google_cloudfunctions_function" "function" {
source_archive_object = google_storage_bucket_object.zip.name
trigger_http = true
entry_point = var.function_entry_point
environment_variables = var.environment_variables
}

# Create IAM entry so all users can invoke the function
resource "google_cloudfunctions_function_iam_member" "invoker" {
project = google_cloudfunctions_function.function.project
region = google_cloudfunctions_function.function.region
cloud_function = google_cloudfunctions_function.function.name

role = "roles/cloudfunctions.invoker"
member = var.function_invoker_user
}
3 changes: 3 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "function_url" {
value = google_cloudfunctions_function.function.https_trigger_url
}
3 changes: 0 additions & 3 deletions test/hello.js

This file was deleted.

12 changes: 0 additions & 12 deletions test/test.tf

This file was deleted.

36 changes: 23 additions & 13 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
variable "project" {
description = "Google Cloud Project ID"
}
variable "region" {
description = "Google Cloud Region"
default = "us-central1"
}

variable "runtime" {
description = "The runtime the google cloud function should run in. A list of all available runtimes can be found here: https://cloud.google.com/functions/docs/concepts/exec#runtimes"
type = string
default = "nodejs12.x"
default = "nodejs12"
}

variable "source_filename" {
description = "name of the file that contains the source code."
type = string
}

variable "filename" {
description = "The path to the .zip file that contains the cloud function source code."
variable "source_dir" {
description = "The path to the directory that contains the source code file. This directory should only contain the source code and will be compressed and uploaded to the bucket."
type = string
}

Expand All @@ -28,10 +25,14 @@ variable "environment_variables" {
}

variable "function_entry_point" {
description = "The function entrypoint in the code. This is the name of the method in the code which receives the event and context parameter when this Lambda function is triggered. e.g: function_1.handler"
description = "The function entrypoint in the code. It might be the name of a method in your source code."
type = string
}

variable "function_invoker_user" {
description = "Identities that will be granted the privilege to invoke the function. Public: 'allUsers'."
}

variable "timeout" {
description = "Timeout (in seconds) for the function. Default value is 60 seconds. Cannot be more than 540 seconds."
default = 60
Expand All @@ -43,11 +44,20 @@ variable "available_memory_mb" {
}

variable "labels" {
description = "{ my-label = \"my-label-value\" }"
description = "These lables will be attached to your hosted function in the cloud. { my-label = \"my-label-value\" }"
default = {}
}

variable "bucket_location" {
description = "Available Google Bucket locations: https://cloud.google.com/storage/docs/locations"
default = "US"
}

variable "project" {
description = "Google Cloud Project ID"
type = string
}
variable "region" {
description = "Google Cloud Region"
default = "us-central1"
}