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

GCP Cloud Functions example #105

Merged
merged 10 commits into from
Mar 9, 2022
Merged
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
1 change: 1 addition & 0 deletions INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
| `each` | [aws](aws/aws_iam/groups) |
| `for` | [aws, aws_vpc](aws/aws_vpc/for) |
| `for_each` | [map, local](local/null_resource/for_each) <p/> [aws](aws/aws_instance/for_each) <p/> [aws](aws/aws_iam/groups)|
| `google_cloud_functions` | [google, simple](google/google_cloud_functions/simple ) <p/> [hello world app](google/google_cloud_functions/simple/hello_world_app ) |
| `google_cloud_run_service` | [google, gcp_cloud_run](google/google_cloud_run_service/simple ) <p/> [noauth](google/google_cloud_run_service/noauth) |
| `google_compute_attached_disk` | [google](google/google_compute_attached_disk/simple) <p/> [count](google/google_compute_attached_disk/count) |
| `google_compute_disk` | [google, gcp_disk](google/google_compute_disk/simple) |
Expand Down
2 changes: 2 additions & 0 deletions google/google_cloud_functions/simple/destroy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
../../../bin/destroy.sh google
2 changes: 2 additions & 0 deletions google/google_cloud_functions/simple/hello_world_app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello_world(request):
return f"Hello, World!"
77 changes: 77 additions & 0 deletions google/google_cloud_functions/simple/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Summary: A simple Cloud Function example with a dummy Python function

# Documentation: https://www.terraform.io/docs/language/settings/index.html
terraform {
required_version = ">= 1.0.0"
required_providers {
google = {
source = "hashicorp/google"
version = "~> 3.0"
}
}
}

# Documentation: https://www.terraform.io/docs/language/values/variables.html
variable "project_id" {
type = string
default = "changeme_myprojectid"
}

# Documentation: https://www.terraform.io/docs/language/providers/requirements.html
provider "google" {
project = var.project_id
region = "us-central1"
zone = "us-central1-a"
}

# Documentation: https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/archive_file
# Documentation (path.module): https://www.terraform.io/docs/language/expressions/references.html#filesystem-and-workspace-info
data "archive_file" "changeme_archive_file" {
type = "zip"
source_dir = "${path.module}/hello_world_app"
output_path = "${path.module}/hello_world.zip"
}

# Needed to have a unique bucket name globally in GCP
# Documentation: https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id
resource "random_id" "changeme_google_storage_bucket_simple_name_2" {
byte_length = 16
}

# Documentation: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket
resource "google_storage_bucket" "changeme_stoarge_bucket" {
name = "changeme-${random_id.changeme_google_storage_bucket_simple_name_2.hex}"
location = "US"
}

# Documentation: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket_object
resource "google_storage_bucket_object" "changeme_hello_world_zip_object" {
name = "hello_world.zip"
source = "${path.module}/hello_world.zip"
bucket = google_storage_bucket.changeme_stoarge_bucket.name
}

# Documentation: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloudfunctions_function
resource "google_cloudfunctions_function" "changeme_function" {
name = "changeme_hello_world_function"
description = "Schedule Hello World Cloud Function"
runtime = "python38"
timeout = 60
available_memory_mb = 128
source_archive_bucket = google_storage_bucket.changeme_stoarge_bucket.name
source_archive_object = google_storage_bucket_object.changeme_hello_world_zip_object.name
trigger_http = true
entry_point = "hello_world"
}

# Needed for the function to be invoked without authentication
# by anyone on the internet, since it's a public http (hello world) function
# Documentation: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloudfunctions_function_iam
resource "google_cloudfunctions_function_iam_member" "changeme_invoker" {
project = google_cloudfunctions_function.changeme_function.project
region = google_cloudfunctions_function.changeme_function.region
cloud_function = google_cloudfunctions_function.changeme_function.name

role = "roles/cloudfunctions.invoker"
member = "allUsers"
}
2 changes: 2 additions & 0 deletions google/google_cloud_functions/simple/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
../../../bin/apply.sh google