Schedule and run a cron job using Terraform
This quickstart shows you how to use Terraform to create a Cloud Scheduler cron job. Terraform is an infrastructure as code (IaC) tool that lets you predictably create, change, and improve your cloud infrastructure by using code. You can learn more about using Terraform to provision infrastructure on Google Cloud.
Cloud Scheduler has a free tier and running this quickstart shouldn't result in any costs. For more information, see Pricing.
In this quickstart, you:
- Use Terraform to create a cron job for Cloud Scheduler.
- Set a recurring schedule for the job.
- Specify a Pub/Sub topic as the job target.
- Run the job.
- Verify that the job has run successfully.
Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
- Cloud Shell has Terraform already integrated. If you need to install Terraform, see the HashiCorp Terraform documentation.
Create a Terraform configuration file
To use Terraform with Cloud Scheduler, you need to create a configuration file to describe your infrastructure and create an execution plan. You then apply the configuration file to your platform or service to perform operations that provision your infrastructure.
Complete the following steps to create a Terraform configuration file called
main.tf
:
Open a terminal and create a directory:
mkdir terraform
Go to the
terraform
directory:cd terraform
Add a new file,
main.tf
, to the directory:nano main.tf
Add the following Terraform provider for Google Cloud resources to the
main.tf
file:Enable the Cloud Scheduler and Pub/Sub APIs:
Create a Pub/Sub topic as a resource to which messages can be sent by publishers:
This creates a topic called
pubsub_topic
.Create a subscription to receive messages published to the Pub/Sub topic:
Create a cron job using the
google_cloud_scheduler_job
resource:The following arguments are used in the sample:
name
: the name of the job.description
: a description for the job.schedule
: the frequency for the job, using a format based on unix-cron.In this sample,
30 16 * * 7
means that the job will run at 16:30 on Sundays. For more information, see Cron job format and time zone.region
: the region where the job resides.pubsub_target
: the Pub/Sub topic target, including the full resource name of the topic (topic_name
) to which the message payload (data
) is published when the job is run.
Other arguments are supported. For details, see the Terraform Registry argument reference.
Create the cron job
Deploy your Terraform resources to create the cron job.
Open a terminal and in the
terraform
directory, initialize Terraform:terraform init
Check that the changes you propose with Terraform match the expected plan:
terraform plan
You can ignore the note regarding not using the
-out
option.Create the cron job:
terraform apply
At the Enter a value prompt, type
yes
to proceed with the creation of resources.Confirm that a job is created:
gcloud scheduler jobs describe test-job --location=us-central1
The output should be similar to the following:
description: test job lastAttemptTime: '2024-04-04T13:56:00.669530Z' name: projects/PROJECT_ID/locations/us-central1/jobs/test-job pubsubTarget: data: dGVzdA== topicName: projects/PROJECT_ID/topics/pubsub_topic schedule: '30 16 * * 7' scheduleTime: '2024-04-04T13:58:00.737907Z' state: ENABLED
You've created a job that sends a message to a Pub/Sub topic at 16:30 on Sundays. You can now run the job.
Run your job
In addition to executing according to its specified schedule, you can force your job to execute immediately by running the following command.
gcloud scheduler jobs run test-job --location=us-central1
Note that due to some initial configuration, the first job created in a project can take a few minutes to run.
Verify the results
Verify that your Pub/Sub topic is receiving messages from your job.
Pull Pub/Sub messages from a subscription:
gcloud pubsub subscriptions pull pubsub_subscription --limit 5
If there are no messages pulled initially, run the command again.
View the results of running your job. The output should look similar to the following:
DATA: Hello world! MESSAGE_ID: 5028933846601543 ORDERING_KEY: ATTRIBUTES: DELIVERY_ATTEMPT: ACK_ID: RFAGFixdRkhRNxkIaFEOT14jPzUgKEUQAgVPAihdeTFXLkFacGhRDRlyfWB9[...]
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, delete the Google Cloud project with the resources.
- Deleting your Google Cloud project stops billing for all the
resources used within that project. Note that by default, any files in your
Cloud Shell home directory (for example, any Terraform files) persist
between sessions.
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
Alternatively, you can delete all the resources you created with Terraform.
The
terraform destroy
command terminates all the resources specified in your Terraform state. It doesn't destroy resources that aren't managed by the current Terraform project. Your Terraform configuration file is not destroyed. For more information, see Destroy infrastructure.terraform destroy