Antes de agendar e executar uma tarefa cron, tem de configurar o seu Google Cloudprojeto e ambiente para o Cloud Scheduler.
Ative a API Cloud Scheduler
Use as instruções seguintes para criar ou selecionar um Google Cloud projeto e para ativar a API Cloud Scheduler.
Consola
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Scheduler API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Scheduler API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles.
gcloud
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Scheduler API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles.gcloud services enable cloudscheduler.googleapis.com
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Scheduler API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles.gcloud services enable cloudscheduler.googleapis.com
Configure a autenticação
A forma como se autentica no Cloud Scheduler depende da interface que usa para aceder à API e do ambiente onde o seu código está a ser executado.
Para mais informações, consulte os artigos Autentique-se no Cloud Scheduler e Use a autenticação com destinos HTTP.
Adicione uma aplicação do App Engine
Se for usar um destino HTTP do App Engine, tem de adicionar uma aplicação do App Engine ao seu projeto. Esta app funciona como a localização do serviço Cloud Scheduler e do processador de tarefas propriamente dito, que realiza o trabalho associado a uma tarefa do Cloud Scheduler e devolve uma confirmação em resposta.
Se quiser ter um controlador de tarefas do App Engine que não esteja no seu projeto, deve selecionar um destino HTTP. Neste caso, não precisa de uma app do App Engine no seu projeto.
Consola
Na Google Cloud consola, aceda à página App Engine.
Na caixa de diálogo Bem-vindo ao App Engine, faça o seguinte:
Se já tiver criado uma aplicação do App Engine e for apresentada a mensagem A sua aplicação do App Engine foi criada, pode ignorar os passos restantes nesta secção.
ou
Se ainda não criou uma aplicação do App Engine, clique em Criar aplicação e continue com os restantes passos nesta secção.
Selecione uma região para a sua aplicação.
Tenha em atenção que
europe-west
eus-central
são denominados, respetivamente,europe-west1
eus-central1
nos comandos do Cloud Scheduler.Depois de definir uma região para a app do App Engine, não pode alterá-la.
Não selecione uma conta de serviço. É usada a conta de serviço do App Engine predefinida.
Clicar em Seguinte.
A aplicação é configurada e criada. Esta operação pode demorar alguns minutos.
Não transfira o Cloud SDK. Em vez disso, clique em Farei isso mais tarde.
Deve ver a mensagem A sua aplicação do App Engine foi criada.
gcloud
Para criar uma app do App Engine, execute o comando
gcloud app create
:gcloud app create --region=REGION
Substitua
REGION
pela localização em que a app é executada. Tenha em atenção queeurope-west
eus-central
são denominados, respetivamente,europe-west1
eus-central1
nos comandos do Cloud Scheduler.Depois de definir uma região para a app do App Engine, não pode alterá-la.
Pode verificar a região de uma app existente com o seguinte comando:
gcloud app describe
O elemento
locationId
indica a região. Por exemplo:locationId: us-central
.