Skip to content

Commit

Permalink
GCP cloud-function created through terrraform
Browse files Browse the repository at this point in the history
  • Loading branch information
abidknashtech committed Nov 22, 2023
1 parent f010adb commit d0f7a51
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 13 deletions.
6 changes: 6 additions & 0 deletions admin-service/cloud/gcpcloudfunction/googlecloud.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="SonarLintModuleSettings">
<option name="uniqueId" value="12f9ab80-8eb7-4814-9d83-36cda23c85a6" />
</component>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ To utilize this project, follow the steps below
+ Deploy the cloud function to Google Cloud Platform.
+ Open the console and navigate to the project directory.Run the following command to deploy the cloud function.

gcloud alpha functions deploy cloud_function --entry-point pubsub.PubSubDataHandler --runtime java17 --trigger-topic topic_name
gcloud alpha functions deploy cloud_function --entry-point com.knoldus.cloudfunction.PubSubDataHandler --runtime java17 --trigger-topic topic_name

### Publishing a Message to the Topic
After deploying the cloud function, publish a message to the configured topic.
Expand Down
6 changes: 6 additions & 0 deletions cloud-function/gcpcarfunction/gcpcarfunction.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="SonarLintModuleSettings">
<option name="uniqueId" value="2c962da3-dcd4-4788-b31b-8b2fc219618a" />
</component>
</module>
6 changes: 6 additions & 0 deletions cloud-function/gcpcarfunction/gcpfunction.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="SonarLintModuleSettings">
<option name="uniqueId" value="db79beec-acd8-4e19-8eb4-0238c253bb28" />
</component>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.nashtech</groupId>
<artifactId>gcpfunction</artifactId>
<artifactId>gcpcarfunction</artifactId>
<version>1.0</version>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

# this script is outdated please ref terraform deployment/gcpresources/terraform/main.tf
# Set your GCP project ID and other variables
PROJECT_ID="gcp-demo-project-399609"
CLUSTER_NAME="cluster-gcp-deployment"
Expand Down
73 changes: 63 additions & 10 deletions deployment/gcpresources/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resource "random_id" "instance_id" {
byte_length = 4
}

#----------------------------------------my-sql-----------------------------
# create My SQL database instance
resource "google_sql_database_instance" "my_sql" {
Expand Down Expand Up @@ -132,17 +133,9 @@ resource "google_container_cluster" "car-demo-gke" {
services_ipv4_cidr_block = ""
}
enable_autopilot = true

}

resource "null_resource" "external-secret-car-demo-gke" {
provisioner "local-exec" {
command = "/bin/bash external-secret-gcsm.sh car-demo-gke ${var.gcp_region_1}"
}
depends_on = [google_container_cluster.car-demo-gke]
}

#-----------------------GKE Cluster for axon-server----------------------------
#GKE Cluster for axon-server
resource "google_container_cluster" "axon-server-gke" {
name = "axon-server-gke"
location = var.gcp_region_1
Expand All @@ -151,7 +144,13 @@ resource "google_container_cluster" "axon-server-gke" {
services_ipv4_cidr_block = ""
}
enable_autopilot = true
}

resource "null_resource" "external-secret-car-demo-gke" {
provisioner "local-exec" {
command = "/bin/bash external-secret-gcsm.sh car-demo-gke ${var.gcp_region_1}"
}
depends_on = [google_container_cluster.car-demo-gke]
}

resource "null_resource" "axon-server-gke" {
Expand All @@ -169,12 +168,66 @@ resource "google_project_service" "firestore" {

resource "google_firestore_database" "database" {
project = var.app_project
name = "(default)"
name = "car-db"
location_id = var.gcp_region_1
type = "FIRESTORE_NATIVE"
depends_on = [google_project_service.firestore]
}

#------------------------- Cloud function----------------------
resource "google_storage_bucket" "function_bucket" {
name = "${random_id.instance_id.hex}-gcf-source" # Every bucket name must be globally unique
location = var.gcp_region_1
uniform_bucket_level_access = true
}

data "archive_file" "source" {
type = "zip"
output_path = "/tmp/function-source.zip"
source_dir = "/home/knoldus/IdeaProjects/car-demo/cloud-function/gcpcarfunction"
}

resource "google_storage_bucket_object" "zip" {
name = "function-source.zip"
bucket = google_storage_bucket.function_bucket.name
source = data.archive_file.source.output_path # Add path to the zipped function source code
content_type = "application/zip"
}

resource "google_cloudfunctions2_function" "function-v2" {
name = "car_cloud_function"
location = var.gcp_region_1

event_trigger {
trigger_region = var.gcp_region_1
event_type = "google.cloud.pubsub.topic.v1.messagePublished"
pubsub_topic = google_pubsub_topic.vehicle.id
retry_policy = "RETRY_POLICY_RETRY"
}

build_config {
runtime = "java17"
entry_point = "com.knoldus.cloudfunction.PubSubDataHandler" # Set the entry point
source {
storage_source {
bucket = google_storage_bucket.function_bucket.name
object = google_storage_bucket_object.zip.name
}
}
}

service_config {
max_instance_count = 1
available_memory = "256M"
timeout_seconds = 60
}

depends_on = [
google_storage_bucket.function_bucket, # declared in `storage.tf`
google_storage_bucket_object.zip
]
}

#------------------------- secret manger----------------------
resource "google_secret_manager_secret" "car-demo-secret" {
secret_id = "car-demo-secret"
Expand Down

0 comments on commit d0f7a51

Please sign in to comment.