Configure o seu ambiente

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

  1. 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.
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

gcloud

  1. 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.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. 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 the resourcemanager.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.

  6. Verify that billing is enabled for your Google Cloud project.

  7. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable cloudscheduler.googleapis.com
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. 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 the resourcemanager.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.

  12. Verify that billing is enabled for your Google Cloud project.

  13. 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 the serviceusage.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

  1. Na Google Cloud consola, aceda à página App Engine.

    Aceder ao App Engine

  2. 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.

  3. Selecione uma região para a sua aplicação.

    Tenha em atenção que europe-west e us-central são denominados, respetivamente, europe-west1 e us-central1 nos comandos do Cloud Scheduler.

    Depois de definir uma região para a app do App Engine, não pode alterá-la.

  4. Não selecione uma conta de serviço. É usada a conta de serviço do App Engine predefinida.

  5. Clicar em Seguinte.

    A aplicação é configurada e criada. Esta operação pode demorar alguns minutos.

  6. 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

  1. 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 que europe-west e us-central são denominados, respetivamente, europe-west1 e us-central1 nos comandos do Cloud Scheduler.

    Depois de definir uma região para a app do App Engine, não pode alterá-la.

  2. 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.