|
1 |
| -# terraform-google-scheduled-function |
| 1 | +# Terraform Google Scheduled Functions Module |
| 2 | +This modules makes it easy to set up a scheduled job to trigger events/run functions. |
| 3 | + |
| 4 | +## Usage |
| 5 | +You can go to the examples folder, however the usage of the module could be like this in your own main.tf file: |
| 6 | + |
| 7 | +```hcl |
| 8 | +module "scheduled-function" { |
| 9 | + source = "terraform-google-modules/scheduled-functions/google" |
| 10 | + version = "0.1.0" |
| 11 | + project_id = "<PROJECT ID>" |
| 12 | + job_name="<NAME_OF_JOB>" |
| 13 | + schedule="<CRON_SYNTAX_SCHEDULE" |
| 14 | + function_entry_point="<NAME_OF_FUNCTION>" |
| 15 | + function_source_directory="<DIRECTORY_OF_FUNCTION_SOURCE>" |
| 16 | + name="<RESOURCE_NAMES>" |
| 17 | + region="<REGION>" |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +Then perform the following commands on the root folder: |
| 22 | + |
| 23 | +- `terraform init` to get the plugins |
| 24 | +- `terraform plan` to see the infrastructure plan |
| 25 | +- `terraform apply` to apply the infrastructure build |
| 26 | +- `terraform destroy` to destroy the built infrastructure |
| 27 | + |
| 28 | +[^]: (autogen_docs_start) |
| 29 | + |
| 30 | +## Inputs |
| 31 | + |
| 32 | +| Name | Description | Type | Default | Required | |
| 33 | +|------|-------------|:----:|:-----:|:-----:| |
| 34 | +| bucket\_name | The name to apply to the bucket. Will default to a string of <project-id>-scheduled-function-XXXX> with XXXX being random characters. | string | `""` | no | |
| 35 | +| function\_available\_memory\_mb | The amount of memory in megabytes allotted for the function to use. | string | `"256"` | no | |
| 36 | +| function\_description | The description of the function. | string | `"Processes log export events provided through a Pub/Sub topic subscription."` | no | |
| 37 | +| function\_entry\_point | The name of a method in the function source which will be invoked when the function is executed. | string | n/a | yes | |
| 38 | +| function\_environment\_variables | A set of key/value environment variable pairs to assign to the function. | map | `<map>` | no | |
| 39 | +| function\_event\_trigger\_failure\_policy\_retry | A toggle to determine if the function should be retried on failure. | string | `"false"` | no | |
| 40 | +| function\_labels | A set of key/value label pairs to assign to the function. | map | `<map>` | no | |
| 41 | +| function\_name | The name to apply to the function | string | n/a | yes | |
| 42 | +| function\_runtime | The runtime in which the function will be executed. | string | `"nodejs6"` | no | |
| 43 | +| function\_source\_archive\_bucket\_labels | A set of key/value label pairs to assign to the function source archive bucket. | map | `<map>` | no | |
| 44 | +| function\_source\_directory | The contents of this directory will be archived and used as the function source. | string | n/a | yes | |
| 45 | +| function\_timeout\_s | The amount of time in seconds allotted for the execution of the function. | string | `"60"` | no | |
| 46 | +| job\_description | Addition text to describet the job | string | `""` | no | |
| 47 | +| job\_name | The name of the scheduled job to run | string | n/a | yes | |
| 48 | +| job\_schedule | The job frequency, in cron syntax | string | `"*/2 * * * *"` | no | |
| 49 | +| message\_data | The data to send in the topic message. | string | `"dGVzdA=="` | no | |
| 50 | +| project\_id | The ID of the project where this VPC will be created | string | n/a | yes | |
| 51 | +| region | The region in which resources will be applied. | string | n/a | yes | |
| 52 | +| topic\_name | Name of pubsub topic connecting the scheduled job and the function | string | `"test-topic"` | no | |
| 53 | + |
| 54 | +## Outputs |
| 55 | + |
| 56 | +| Name | Description | |
| 57 | +|------|-------------| |
| 58 | +| name | The name of the job created | |
| 59 | + |
| 60 | +[^]: (autogen_docs_end) |
| 61 | + |
| 62 | +## Requirements |
| 63 | +### Terraform plugins |
| 64 | +- [Terraform](https://www.terraform.io/downloads.html) 0.11.x |
| 65 | +- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v2.1 |
| 66 | + |
| 67 | +### App Engine |
| 68 | +Note that this module requires App Engine being configured in the specified project/region. |
| 69 | +This is because Google Cloud Scheduler is dependent on the project being configured with App Engine. |
| 70 | +Refer to the [Google Cloud Scheduler documentation](https://cloud.google.com/scheduler/docs/) for more information on the App Engine dependency. |
| 71 | + |
| 72 | +The recommended way to create projects with App Engine enabled is via the [Project Factory module](https://github.com/terraform-google-modules/terraform-google-project-factory). |
| 73 | +There is an example of how to create the project [within that module](https://github.com/terraform-google-modules/terraform-google-project-factory/tree/master/examples/app_engine) |
| 74 | + |
| 75 | +### Configure a Service Account |
| 76 | +In order to execute this module you must have a Service Account with the following roles. |
| 77 | + |
| 78 | +- roles/storage.admin |
| 79 | +- roles/pubsub.editor |
| 80 | +- roles/cloudscheduler.admin |
| 81 | +- roles/cloudfunctions.developer |
| 82 | +- roles/iam.serviceAccountUser |
| 83 | + |
| 84 | + |
| 85 | +### Enable API's |
| 86 | +In order to operate with the Service Account you must activate the following API on the project where the Service Account was created: |
| 87 | + |
| 88 | +- Cloud Scheduler API - cloudscheduler.googleapis.com |
| 89 | +- Cloud PubSub API - pubsub.googleapis.com |
| 90 | +- Cloud Functions API - cloudfunctions.googleapis.com |
| 91 | + |
| 92 | +## Install |
| 93 | + |
| 94 | +### Terraform |
| 95 | +Be sure you have the correct Terraform version (0.11.x), you can choose the binary here: |
| 96 | +- https://releases.hashicorp.com/terraform/ |
| 97 | + |
| 98 | +## Testing and documentation generation |
| 99 | + |
| 100 | +### Requirements |
| 101 | +- [docker](https://docker.com) |
| 102 | +- [terraform-docs](https://github.com/segmentio/terraform-docs/releases) 0.6.0 |
| 103 | + |
| 104 | +### Integration test |
| 105 | +##### Terraform integration tests |
| 106 | +It is recommended to to run the integration tests via docker. To do so, run `make test_integration_docker`. In containers, this will |
| 107 | +- Perform `terraform init` command |
| 108 | +- Perform `terraform get` command |
| 109 | +- Perform `terraform validate` command |
| 110 | +- Perform `terraform apply -auto-approve` command and check that it has created the appropriate resources |
| 111 | +- Perform `terraform destroy -force` command and check that it has destroyed the appropriate resources |
| 112 | + |
| 113 | +### Autogeneration of documentation from .tf files |
| 114 | +Run |
| 115 | +``` |
| 116 | +make generate_docs |
| 117 | +``` |
| 118 | + |
| 119 | +### Linting |
| 120 | +The makefile in this project will lint or sometimes just format any shell, |
| 121 | +Python, golang, Terraform, or Dockerfiles. The linters will only be run if |
| 122 | +the makefile finds files with the appropriate file extension. |
| 123 | + |
| 124 | +All of the linter checks are in the default make target, so you just have to |
| 125 | +run |
| 126 | + |
| 127 | +``` |
| 128 | +make -s |
| 129 | +``` |
| 130 | + |
| 131 | +The -s is for 'silent'. Successful output looks like this |
| 132 | + |
| 133 | +``` |
| 134 | +Running shellcheck |
| 135 | +Running flake8 |
| 136 | +Running gofmt |
| 137 | +Running terraform validate |
| 138 | +Running hadolint on Dockerfiles |
| 139 | +Test passed - Verified all file Apache 2 headers |
| 140 | +``` |
| 141 | + |
| 142 | +The linters |
| 143 | +are as follows: |
| 144 | +* Shell - shellcheck. Can be found in homebrew |
| 145 | +* Python - flake8. Can be installed with 'pip install flake8' |
| 146 | +* Golang - gofmt. gofmt comes with the standard golang installation. golang |
| 147 | +is a compiled language so there is no standard linter. |
| 148 | +* Terraform - terraform has a built-in linter in the 'terraform validate' |
| 149 | +command. |
| 150 | +* Dockerfiles - hadolint. Can be found in homebrew |
0 commit comments