Skip to content

Commit 5eea231

Browse files
authored
Merge pull request #26 from omazin/existing-cloud-scheduler-1
Add ability to use existing Cloud Scheduler
2 parents a3baa9d + 3f924bc commit 5eea231

File tree

10 files changed

+216
-9
lines changed

10 files changed

+216
-9
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ Then perform the following commands on the root folder:
5353
| function\_source\_dependent\_files | A list of any terraform created `local_file`s that the module will wait for before creating the archive. | object | `<list>` | no |
5454
| function\_source\_directory | The contents of this directory will be archived and used as the function source. | string | n/a | yes |
5555
| function\_timeout\_s | The amount of time in seconds allotted for the execution of the function. | number | `"60"` | no |
56-
| job\_description | Addition text to describet the job | string | `""` | no |
57-
| job\_name | The name of the scheduled job to run | string | n/a | yes |
56+
| job\_description | Addition text to describe the job | string | `""` | no |
57+
| job\_name | The name of the scheduled job to run | string | `"null"` | no |
5858
| job\_schedule | The job frequency, in cron syntax | string | `"*/2 * * * *"` | no |
5959
| message\_data | The data to send in the topic message. | string | `"dGVzdA=="` | no |
6060
| project\_id | The ID of the project where the resources will be created | string | n/a | yes |
6161
| region | The region in which resources will be applied. | string | n/a | yes |
62+
| scheduler\_job | An existing Cloud Scheduler job instance | object | `"null"` | no |
6263
| time\_zone | The timezone to use in scheduler | string | `"Etc/UTC"` | no |
6364
| topic\_name | Name of pubsub topic connecting the scheduled job and the function | string | `"test-topic"` | no |
6465

@@ -67,6 +68,8 @@ Then perform the following commands on the root folder:
6768
| Name | Description |
6869
|------|-------------|
6970
| name | The name of the job created |
71+
| pubsub\_topic\_name | PubSub topic name |
72+
| scheduler\_job | The Cloud Scheduler job instance |
7073

7174
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
7275

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Multiple Cloud Functions sharing the same Cloud Scheduler
2+
3+
This example module schedules a job to publish a message to a Pub/Sub topic every 5 minutes, which will trigger a CloudFunctions function.
4+
5+
Running this module requires an App Engine app in the specified project/region, which is not handled by this example.
6+
More information is in the [root readme](../../README.md#app-engine).
7+
8+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
9+
## Inputs
10+
11+
| Name | Description | Type | Default | Required |
12+
|------|-------------|:----:|:-----:|:-----:|
13+
| project\_id | The project ID to host the network in | string | `"flask-app-254610"` | no |
14+
| region | The region the project is in (App Engine specific) | string | `"us-central1"` | no |
15+
16+
## Outputs
17+
18+
| Name | Description |
19+
|------|-------------|
20+
| name | The name of the job created |
21+
| project\_id | The project ID |
22+
| scheduler\_job | An existing Cloud Scheduler job instance |
23+
24+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
/**
19+
* Triggered from a message on a Cloud Pub/Sub topic.
20+
*
21+
* @param {!Object} event Event payload and metadata.
22+
* @param {!Function} callback Callback function to signal completion.
23+
*/
24+
exports.doSomething = (event) => {
25+
console.log("Received event 1");
26+
console.log(event);
27+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
/**
19+
* Triggered from a message on a Cloud Pub/Sub topic.
20+
*
21+
* @param {!Object} event Event payload and metadata.
22+
* @param {!Function} callback Callback function to signal completion.
23+
*/
24+
exports.doSomething2 = (event) => {
25+
console.log("Received event 2");
26+
console.log(event);
27+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
terraform {
18+
required_version = ">= 0.12"
19+
}
20+
21+
provider "google-beta" {
22+
version = "~> 2.5"
23+
project = var.project_id
24+
region = var.region
25+
}
26+
27+
module "pubsub_scheduled_1" {
28+
source = "../../"
29+
project_id = var.project_id
30+
job_name = "pubsub-example"
31+
job_schedule = "*/5 * * * *"
32+
function_entry_point = "doSomething"
33+
function_source_directory = "${path.module}/function_source"
34+
function_name = "testfunction-1"
35+
region = var.region
36+
topic_name = "pubsub-1"
37+
}
38+
39+
module "pubsub_scheduled_2" {
40+
source = "../../"
41+
project_id = var.project_id
42+
function_entry_point = "doSomething2"
43+
function_source_directory = "${path.module}/function_source_2"
44+
function_name = "testfunction-2"
45+
region = var.region
46+
topic_name = module.pubsub_scheduled_1.pubsub_topic_name
47+
scheduler_job = module.pubsub_scheduled_1.scheduler_job
48+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "name" {
18+
value = module.pubsub_scheduled_1.name
19+
description = "The name of the job created"
20+
}
21+
22+
output "project_id" {
23+
value = var.project_id
24+
description = "The project ID"
25+
}
26+
27+
output "scheduler_job" {
28+
value = module.pubsub_scheduled_1.scheduler_job
29+
description = "An existing Cloud Scheduler job instance"
30+
}
31+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "project_id" {
18+
type = string
19+
description = "The project ID to host the network in"
20+
default = "flask-app-254610"
21+
}
22+
23+
variable "region" {
24+
type = string
25+
description = "The region the project is in (App Engine specific)"
26+
default = "us-central1"
27+
}

main.tf

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*****************************************/
2020

2121
resource "google_cloud_scheduler_job" "job" {
22+
count = var.scheduler_job == null ? 1 : 0
23+
2224
name = var.job_name
2325
project = var.project_id
2426
region = var.region
@@ -37,10 +39,11 @@ resource "google_cloud_scheduler_job" "job" {
3739
*****************************************/
3840

3941
module "pubsub_topic" {
40-
source = "terraform-google-modules/pubsub/google"
41-
version = "~> 1.0"
42-
topic = var.topic_name
43-
project_id = var.project_id
42+
source = "terraform-google-modules/pubsub/google"
43+
version = "~> 1.0"
44+
topic = var.topic_name
45+
project_id = var.project_id
46+
create_topic = var.scheduler_job == null ? true : false
4447
}
4548

4649
/******************************************
@@ -54,7 +57,7 @@ module "main" {
5457
entry_point = var.function_entry_point
5558
event_trigger = {
5659
event_type = "google.pubsub.topic.publish"
57-
resource = module.pubsub_topic.topic
60+
resource = var.scheduler_job == null ? module.pubsub_topic.topic : var.topic_name
5861
}
5962
name = var.function_name
6063
project_id = var.project_id

outputs.tf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
*/
1616

1717
output "name" {
18-
value = google_cloud_scheduler_job.job.name
18+
value = var.scheduler_job == null ? google_cloud_scheduler_job.job.0.name : var.scheduler_job.name
1919
description = "The name of the job created"
2020
}
21+
22+
output "scheduler_job" {
23+
value = var.scheduler_job == null ? google_cloud_scheduler_job.job : var.scheduler_job
24+
description = "The Cloud Scheduler job instance"
25+
}
26+
27+
output "pubsub_topic_name" {
28+
value = var.scheduler_job == null ? module.pubsub_topic.topic : var.topic_name
29+
description = "PubSub topic name"
30+
}

variables.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ variable "project_id" {
2222
variable "job_name" {
2323
type = string
2424
description = "The name of the scheduled job to run"
25+
default = null
2526
}
2627

2728
variable "job_description" {
2829
type = string
29-
description = "Addition text to describet the job"
30+
description = "Addition text to describe the job"
3031
default = ""
3132
}
3233

@@ -148,3 +149,9 @@ variable "time_zone" {
148149
description = "The timezone to use in scheduler"
149150
default = "Etc/UTC"
150151
}
152+
153+
variable "scheduler_job" {
154+
type = object({ name = string })
155+
description = "An existing Cloud Scheduler job instance"
156+
default = null
157+
}

0 commit comments

Comments
 (0)