Specify the network for a job

This document explains how to specify the network for the VMs that run a job.

You can control connections for the VMs that run a job by specifying a network with the desired access. For example, you might specify a network that allows a job to access required resources or limits access to improve security. Alternatively, if you don't have any networking requirements and don't want to configure networking for a job, skip specifying the network to use the default networking configuration instead.

For more information about networking concepts and when to configure networking, see Batch networking overview .

Before you begin

  1. If you haven't used Batch before, review Get started with Batch and enable Batch by completing the prerequisites for projects and users .
  2. To get the permissions that you need to create a job that runs on a specific network, ask your administrator to grant you the following IAM roles:

    For more information about granting roles, see Manage access to projects, folders, and organizations .

    You might also be able to get the required permissions through custom roles or other predefined roles .

  3. Identify the network that you want to use for the job. The network you specify for a job must meet the following requirements: For more information, see Create and manage VPC networks .
  4. If you want a job to run on a subnet of a Shared VPC network that is hosted by another project, your project's Batch service agent must be granted permission to use that subnet.

    To ensure that your project's Batch service agent has the necessary permissions to create a job that runs on a subnet of a Shared VPC network, ask your administrator to grant your project's Batch service agent the Compute Network User ( roles/compute.networkUser ) IAM role on the Shared VPC subnet.

    For more information, see the documentation for setting up Shared VPC for service accounts .

Create a job that runs on a specific network

Specify the network for a job when you are creating it. Specifically, you need to specify a VPC network and a subnet that is located where you want to run this job.

If you want to use a VM instance template while creating this job , you must specify the network in the VM instance template. Otherwise, use the following steps to specify the network for a job by using the gcloud CLI or Batch API.

gcloud

To create a job that runs on a specific network using the gcloud CLI, select one of the following options:

Use gcloud flags to specify the network for a job

To create a job and use gcloud flags to specify the network for the job, complete the following steps:

  1. Create a JSON file that specifies your job's configuration details.

    For example, to create a basic script job, create a JSON file with the following contents.

      { 
      
     "taskGroups" 
     : 
      
     [ 
      
     { 
      
     "taskSpec" 
     : 
      
     { 
      
     "runnables" 
     : 
      
     [ 
      
     { 
      
     "script" 
     : 
      
     { 
      
     "text" 
     : 
      
     "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks." 
      
     } 
      
     } 
      
     ] 
      
     }, 
      
     "taskCount" 
     : 
      
     3 
      
     } 
      
     ], 
      
     "logsPolicy" 
     : 
      
     { 
      
     "destination" 
     : 
      
     "CLOUD_LOGGING" 
      
     } 
     } 
     
    
  2. Create the job by using the gcloud batch jobs submit command . To specify the network the job, include the --network and --subnetwork flags.

     gcloud batch jobs submit JOB_NAME 
    \
        --location LOCATION 
    \
        --config JSON_CONFIGURATION_FILE 
    \
        --network projects/ HOST_PROJECT_ID 
    /global/networks/ NETWORK 
    \
        --subnetwork projects/ HOST_PROJECT_ID 
    /regions/ REGION 
    /subnetworks/ SUBNET 
     
    

    Replace the following:

    • JOB_NAME : the name for this job.
    • LOCATION : the location for this job.
    • JSON_CONFIGURATION_FILE : the path for the JSON file with the job's configuration details.
    • HOST_PROJECT_ID : the project ID of the project for the network you specify:
      • If you are using a Shared VPC network, specify the host project.
      • Otherwise, specify the current project.
    • NETWORK : the name of a VPC network in the current project or a Shared VPC network that is hosted by or shared with the current project.
    • REGION : the region where the subnet and the VMs for the job are located:
      • If you include the allowedLocations field to specify the allowed location for the VMs for the job, you must specify the same region here.
      • Otherwise, the region must be the same as the location you select for the job ( LOCATION ).
    • SUBNET : the name of a subnet that is part of the VPC network and is located in the same region as the VMs for the job.

Use JSON fields to specify the network for a job

To create a job and use fields in the JSON configuration file to specify the network for the job, complete the following steps:

  1. Create a JSON file that specifies your job's configuration details. To specify the network for the job, include the network and subnetwork fields .

    For example, to create a basic script job that runs on a specific network, create a JSON file with the following contents.

      { 
      
     "taskGroups" 
     : 
      
     [ 
      
     { 
      
     "taskSpec" 
     : 
      
     { 
      
     "runnables" 
     : 
      
     [ 
      
     { 
      
     "script" 
     : 
      
     { 
      
     "text" 
     : 
      
     "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks." 
      
     } 
      
     } 
      
     ] 
      
     }, 
      
     "taskCount" 
     : 
      
     3 
      
     } 
      
     ], 
      
     "allocationPolicy" 
     : 
      
     { 
      
     "network" 
     : 
      
     { 
      
     "networkInterfaces" 
     : 
      
     [ 
      
     { 
      
     "network" 
     : 
      
     "projects/ HOST_PROJECT_ID 
    /global/networks/ NETWORK 
    " 
     , 
      
     "subnetwork" 
     : 
      
     "projects/ HOST_PROJECT_ID 
    /regions/ REGION 
    /subnetworks/ SUBNET 
    " 
      
     } 
      
     ] 
      
     } 
      
     }, 
      
     "logsPolicy" 
     : 
      
     { 
      
     "destination" 
     : 
      
     "CLOUD_LOGGING" 
      
     } 
     } 
     
    

    Replace the following:

    • HOST_PROJECT_ID : the project ID of the project for the network you specify:
      • If you are using a Shared VPC network, specify the host project.
      • Otherwise, specify the current project.
    • NETWORK : the name of a VPC network in the current project or a Shared VPC network that is hosted by or shared with the current project.
    • REGION : the region where the subnet and the VMs for the job are located:
      • If you include the allowedLocations field to specify the allowed location for the VMs for the job, you must specify the same region here.
      • Otherwise, the region must be the same as the location you select for the job ( LOCATION ).
    • SUBNET : the name of a subnet that is part of the VPC network and is located in the same region as the VMs for the job.
  2. Create the job by using the gcloud batch jobs submit command .

     gcloud batch jobs submit JOB_NAME 
    \
        --location LOCATION 
    \
        --config JSON_CONFIGURATION_FILE 
     
    

    Replace the following:

    • JOB_NAME : the name for this job.
    • LOCATION : the location for this job.
    • JSON_CONFIGURATION_FILE : the path for the JSON file with the job's configuration details.

Java

  import 
  
 com.google.cloud.batch.v1. AllocationPolicy 
 
 ; 
 import 
  
 com.google.cloud.batch.v1. BatchServiceClient 
 
 ; 
 import 
  
 com.google.cloud.batch.v1. CreateJobRequest 
 
 ; 
 import 
  
 com.google.cloud.batch.v1. Job 
 
 ; 
 import 
  
 com.google.cloud.batch.v1. LogsPolicy 
 
 ; 
 import 
  
 com.google.cloud.batch.v1. LogsPolicy 
. Destination 
 
 ; 
 import 
  
 com.google.cloud.batch.v1. Runnable 
 
 ; 
 import 
  
 com.google.cloud.batch.v1. Runnable 
. Script 
 
 ; 
 import 
  
 com.google.cloud.batch.v1. TaskGroup 
 
 ; 
 import 
  
 com.google.cloud.batch.v1. TaskSpec 
 
 ; 
 import 
  
 com.google.protobuf. Duration 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 import 
  
 java.util.concurrent.ExecutionException 
 ; 
 import 
  
 java.util.concurrent.TimeUnit 
 ; 
 import 
  
 java.util.concurrent.TimeoutException 
 ; 
 public 
  
 class 
 CreateBatchCustomNetwork 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
 , 
  
 TimeoutException 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 // Project ID or project number of the Google Cloud project you want to use. 
  
 String 
  
 projectId 
  
 = 
  
 "YOUR_PROJECT_ID" 
 ; 
  
 // Name of the region you want to use to run the job. Regions that are 
  
 // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations 
  
 String 
  
 region 
  
 = 
  
 "europe-central2" 
 ; 
  
 // The name of the job that will be created. 
  
 // It needs to be unique for each project and region pair. 
  
 String 
  
 jobName 
  
 = 
  
 "JOB_NAME" 
 ; 
  
 // The name of a VPC network in the current project or a Shared VPC network that is hosted by 
  
 // or shared with the current project. 
  
 String 
  
 network 
  
 = 
  
 String 
 . 
 format 
 ( 
 "global/networks/%s" 
 , 
  
 "test-network" 
 ); 
  
 // The name of a subnet that is part of the VPC network and is located 
  
 // in the same region as the VMs for the job. 
  
 String 
  
 subnet 
  
 = 
  
 String 
 . 
 format 
 ( 
 "regions/%s/subnetworks/%s" 
 , 
  
 region 
 , 
  
 "subnet" 
 ); 
  
 createBatchCustomNetwork 
 ( 
 projectId 
 , 
  
 region 
 , 
  
 jobName 
 , 
  
 network 
 , 
  
 subnet 
 ); 
  
 } 
  
 // Create a job that runs on a specific network. 
  
 public 
  
 static 
  
  Job 
 
  
 createBatchCustomNetwork 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 region 
 , 
  
 String 
  
 jobName 
 , 
  
 String 
  
 network 
 , 
  
 String 
  
 subnet 
 ) 
  
 throws 
  
 IOException 
 , 
  
 ExecutionException 
 , 
  
 InterruptedException 
 , 
  
 TimeoutException 
  
 { 
  
 // Initialize client that will be used to send requests. This client only needs to be created 
  
 // once, and can be reused for multiple requests. 
  
 try 
  
 ( 
  BatchServiceClient 
 
  
 batchServiceClient 
  
 = 
  
  BatchServiceClient 
 
 . 
 create 
 ()) 
  
 { 
  
 // Define what will be done as part of the job. 
  
  Runnable 
 
  
 runnable 
  
 = 
  
  Runnable 
 
 . 
 newBuilder 
 () 
  
 . 
  setScript 
 
 ( 
  
  Script 
 
 . 
 newBuilder 
 () 
  
 . 
  setText 
 
 ( 
  
 "echo Hello world! This is task ${BATCH_TASK_INDEX}. " 
  
 + 
  
 "This job has a total of ${BATCH_TASK_COUNT} tasks." 
 ) 
  
 // You can also run a script from a file. Just remember, that needs to be a 
  
 // script that's already on the VM that will be running the job. 
  
 // Using setText() and setPath() is mutually exclusive. 
  
 // .setPath("/tmp/test.sh") 
  
 . 
 build 
 ()) 
  
 . 
 build 
 (); 
  
  TaskSpec 
 
  
 task 
  
 = 
  
  TaskSpec 
 
 . 
 newBuilder 
 () 
  
 // Jobs can be divided into tasks. In this case, we have only one task. 
  
 . 
  addRunnables 
 
 ( 
 runnable 
 ) 
  
 . 
  setMaxRetryCount 
 
 ( 
 2 
 ) 
  
 . 
  setMaxRunDuration 
 
 ( 
  Duration 
 
 . 
 newBuilder 
 (). 
 setSeconds 
 ( 
 3600 
 ). 
 build 
 ()) 
  
 . 
 build 
 (); 
  
 // Tasks are grouped inside a job using TaskGroups. 
  
 // Currently, it's possible to have only one task group. 
  
  TaskGroup 
 
  
 taskGroup 
  
 = 
  
  TaskGroup 
 
 . 
 newBuilder 
 () 
  
 . 
  setTaskCount 
 
 ( 
 3 
 ) 
  
 . 
  setParallelism 
 
 ( 
 1 
 ) 
  
 . 
  setTaskSpec 
 
 ( 
 task 
 ) 
  
 . 
 build 
 (); 
  
 // Specifies a VPC network and a subnet for Allocation Policy 
  
  AllocationPolicy 
 
 . 
  NetworkPolicy 
 
  
 networkPolicy 
  
 = 
  
  AllocationPolicy 
 
 . 
 NetworkPolicy 
 . 
 newBuilder 
 () 
  
 . 
  addNetworkInterfaces 
 
 ( 
  AllocationPolicy 
 
 . 
 NetworkInterface 
 . 
 newBuilder 
 () 
  
 . 
 setNetwork 
 ( 
 network 
 ) 
  
 // Set the network name 
  
 . 
  setSubnetwork 
 
 ( 
 subnet 
 ) 
  
 // Set the subnet name 
  
 . 
  setNoExternalIpAddress 
 
 ( 
 true 
 ) 
  
 // Blocks external access for all VMs 
  
 . 
 build 
 ()) 
  
 . 
 build 
 (); 
  
 // Policies are used to define on what kind of virtual machines the tasks will run on. 
  
 // In this case, we tell the system to use "e2-standard-4" machine type. 
  
 // Read more about machine types here: https://cloud.google.com/compute/docs/machine-types 
  
  AllocationPolicy 
 
 . 
  InstancePolicy 
 
  
 instancePolicy 
  
 = 
  
  AllocationPolicy 
 
 . 
 InstancePolicy 
 . 
 newBuilder 
 (). 
 setMachineType 
 ( 
 "e2-standard-4" 
 ) 
  
 . 
 build 
 (); 
  
  AllocationPolicy 
 
  
 allocationPolicy 
  
 = 
  
  AllocationPolicy 
 
 . 
 newBuilder 
 () 
  
 . 
 addInstances 
 ( 
  AllocationPolicy 
 
 . 
 InstancePolicyOrTemplate 
 . 
 newBuilder 
 () 
  
 . 
  setPolicy 
 
 ( 
 instancePolicy 
 ). 
 build 
 ()) 
  
 . 
 setNetwork 
 ( 
 networkPolicy 
 ) 
  
 . 
 build 
 (); 
  
  Job 
 
  
 job 
  
 = 
  
  Job 
 
 . 
 newBuilder 
 () 
  
 . 
  addTaskGroups 
 
 ( 
 taskGroup 
 ) 
  
 . 
  setAllocationPolicy 
 
 ( 
 allocationPolicy 
 ) 
  
 // We use Cloud Logging as it's an out of the box available option. 
  
 . 
  setLogsPolicy 
 
 ( 
  
  LogsPolicy 
 
 . 
 newBuilder 
 (). 
  setDestination 
 
 ( 
  Destination 
 
 . 
 CLOUD_LOGGING 
 )) 
  
 . 
 build 
 (); 
  
  CreateJobRequest 
 
  
 createJobRequest 
  
 = 
  
  CreateJobRequest 
 
 . 
 newBuilder 
 () 
  
 // The job's parent is the region in which the job will run for the specific project. 
  
 . 
 setParent 
 ( 
 String 
 . 
 format 
 ( 
 "projects/%s/locations/%s" 
 , 
  
 projectId 
 , 
  
 region 
 )) 
  
 . 
 setJob 
 ( 
 job 
 ) 
  
 . 
  setJobId 
 
 ( 
 jobName 
 ) 
  
 . 
 build 
 (); 
  
  Job 
 
  
 result 
  
 = 
  
 batchServiceClient 
  
 . 
  createJobCallable 
 
 () 
  
 . 
 futureCall 
 ( 
 createJobRequest 
 ) 
  
 . 
 get 
 ( 
 5 
 , 
  
 TimeUnit 
 . 
 MINUTES 
 ); 
  
 System 
 . 
 out 
 . 
 printf 
 ( 
 "Successfully created the job: %s" 
 , 
  
 result 
 . 
  getName 
 
 ()); 
  
 return 
  
 result 
 ; 
  
 } 
  
 } 
 } 
 

Node.js

  // Imports the Batch library 
 const 
  
 batchLib 
  
 = 
  
 require 
 ( 
 ' @google-cloud/batch 
' 
 ); 
 const 
  
 batch 
  
 = 
  
 batchLib 
 . 
 protos 
 . 
 google 
 . 
 cloud 
 . 
 batch 
 . 
 v1 
 ; 
 // Instantiates a client 
 const 
  
 batchClient 
  
 = 
  
 new 
  
 batchLib 
 . 
 v1 
 . 
  BatchServiceClient 
 
 (); 
 /** 
 * TODO(developer): Update these variables before running the sample. 
 */ 
 // Project ID or project number of the Google Cloud project you want to use. 
 const 
  
 projectId 
  
 = 
  
 await 
  
 batchClient 
 . 
 getProjectId 
 (); 
 // Name of the region you want to use to run the job. Regions that are 
 // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations 
 const 
  
 region 
  
 = 
  
 'europe-central2' 
 ; 
 // The name of the job that will be created. 
 // It needs to be unique for each project and region pair. 
 const 
  
 jobName 
  
 = 
  
 'example-job' 
 ; 
 // The name of a VPC network in the current project or a Shared VPC network that is hosted by 
 // or shared with the current project. 
 const 
  
 network 
  
 = 
  
 'global/networks/test-network' 
 ; 
 // The name of a subnetwork that is part of the VPC network and is located 
 // in the same region as the VMs for the job. 
 const 
  
 subnetwork 
  
 = 
  
 `regions/ 
 ${ 
 region 
 } 
 /subnetworks/subnet` 
 ; 
 // Define what will be done as part of the job. 
 const 
  
 runnable 
  
 = 
  
 new 
  
 batch 
 . 
 Runnable 
 ({ 
  
 script 
 : 
  
 new 
  
 batch 
 . 
 Runnable 
 . 
 Script 
 ({ 
  
 commands 
 : 
  
 [ 
 '-c' 
 , 
  
 'echo Hello world! This is task ${BATCH_TASK_INDEX}.' 
 ], 
  
 }), 
 }); 
 // Specify what resources are requested by each task. 
 const 
  
 computeResource 
  
 = 
  
 new 
  
 batch 
 . 
 ComputeResource 
 ({ 
  
 // In milliseconds per cpu-second. This means the task requires 50% of a single CPUs. 
  
 cpuMilli 
 : 
  
 500 
 , 
  
 // In MiB. 
  
 memoryMib 
 : 
  
 16 
 , 
 }); 
 const 
  
 task 
  
 = 
  
 new 
  
 batch 
 . 
 TaskSpec 
 ({ 
  
 runnables 
 : 
  
 [ 
 runnable 
 ], 
  
 computeResource 
 , 
  
 maxRetryCount 
 : 
  
 2 
 , 
  
 maxRunDuration 
 : 
  
 { 
 seconds 
 : 
  
 3600 
 }, 
 }); 
 // Tasks are grouped inside a job using TaskGroups. 
 const 
  
 group 
  
 = 
  
 new 
  
 batch 
 . 
 TaskGroup 
 ({ 
  
 taskCount 
 : 
  
 3 
 , 
  
 taskSpec 
 : 
  
 task 
 , 
 }); 
 // Specify VPC network and a subnet for Allocation Policy 
 const 
  
 networkPolicy 
  
 = 
  
 new 
  
 batch 
 . 
 AllocationPolicy 
 . 
 NetworkPolicy 
 ({ 
  
 networkInterfaces 
 : 
  
 [ 
  
 new 
  
 batch 
 . 
 AllocationPolicy 
 . 
 NetworkInterface 
 ({ 
  
 // Set the network name 
  
 network 
 , 
  
 // Set the subnetwork name 
  
 subnetwork 
 , 
  
 // Blocks external access for all VMs 
  
 noExternalIpAddress 
 : 
  
 true 
 , 
  
 }), 
  
 ], 
 }); 
 // Policies are used to define on what kind of virtual machines the tasks will run on. 
 // In this case, we tell the system to use "e2-standard-4" machine type. 
 // Read more about machine types here: https://cloud.google.com/compute/docs/machine-types 
 const 
  
 instancePolicy 
  
 = 
  
 new 
  
 batch 
 . 
 AllocationPolicy 
 . 
 InstancePolicy 
 ({ 
  
 machineType 
 : 
  
 'e2-standard-4' 
 , 
 }); 
 const 
  
 allocationPolicy 
  
 = 
  
 new 
  
 batch 
 . 
 AllocationPolicy 
 . 
 InstancePolicyOrTemplate 
 ({ 
  
 policy 
 : 
  
 instancePolicy 
 , 
  
 network 
 : 
  
 networkPolicy 
 , 
 }); 
 const 
  
 job 
  
 = 
  
 new 
  
 batch 
 . 
 Job 
 ({ 
  
 name 
 : 
  
 jobName 
 , 
  
 taskGroups 
 : 
  
 [ 
 group 
 ], 
  
 labels 
 : 
  
 { 
 env 
 : 
  
 'testing' 
 , 
  
 type 
 : 
  
 'script' 
 }, 
  
 allocationPolicy 
 , 
  
 // We use Cloud Logging as it's an option available out of the box 
  
 logsPolicy 
 : 
  
 new 
  
 batch 
 . 
 LogsPolicy 
 ({ 
  
 destination 
 : 
  
 batch 
 . 
 LogsPolicy 
 . 
 Destination 
 . 
 CLOUD_LOGGING 
 , 
  
 }), 
 }); 
 // The job's parent is the project and region in which the job will run 
 const 
  
 parent 
  
 = 
  
 `projects/ 
 ${ 
 projectId 
 } 
 /locations/ 
 ${ 
 region 
 } 
 ` 
 ; 
 async 
  
 function 
  
 callCreateBatchCustomNetwork 
 () 
  
 { 
  
 // Construct request 
  
 const 
  
 request 
  
 = 
  
 { 
  
 parent 
 , 
  
 jobId 
 : 
  
 jobName 
 , 
  
 job 
 , 
  
 }; 
  
 // Run request 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 batchClient 
 . 
 createJob 
 ( 
 request 
 ); 
  
 console 
 . 
 log 
 ( 
 JSON 
 . 
 stringify 
 ( 
 response 
 )); 
 } 
 await 
  
 callCreateBatchCustomNetwork 
 (); 
 

Python

  from 
  
 google.cloud 
  
 import 
  batch_v1 
 
 def 
  
 create_with_custom_network 
 ( 
 project_id 
 : 
 str 
 , 
 region 
 : 
 str 
 , 
 network_name 
 : 
 str 
 , 
 subnet_name 
 : 
 str 
 , 
 job_name 
 : 
 str 
 , 
 ) 
 - 
> batch_v1 
 . 
 Job 
 : 
  
 """Create a Batch job that runs on a specific network and subnet. 
 Args: 
 project_id: project ID or project number of the Cloud project you want to use. 
 region: name of the region you want to use to run the job. Regions that are 
 available for Batch are listed on: https://cloud.google.com/batch/docs/locations 
 network_name: The name of a VPC network in the current project or a Shared VPC network. 
 subnet_name: Name of the subnetwork to be used within the specified region. 
 job_name: the name of the job that will be created. 
 It needs to be unique for each project and region pair. 
 Returns: 
 A job object representing the job created. 
 """ 
 client 
 = 
  batch_v1 
 
 . 
  BatchServiceClient 
 
 () 
 # Define what will be done as part of the job. 
 runnable 
 = 
  batch_v1 
 
 . 
  Runnable 
 
 () 
 runnable 
 . 
 script 
 = 
  batch_v1 
 
 . 
  Runnable 
 
 . 
  Script 
 
 () 
 runnable 
 . 
 script 
 . 
 text 
 = 
 "echo Hello world! This is task $ 
 {BATCH_TASK_INDEX} 
 . This job has a total of $ 
 {BATCH_TASK_COUNT} 
 tasks." 
 # Jobs can be divided into tasks. In this case, we have only one task. 
 task 
 = 
  batch_v1 
 
 . 
  TaskSpec 
 
 () 
 task 
 . 
 runnables 
 = 
 [ 
 runnable 
 ] 
 # We can specify what resources are requested by each task. 
 resources 
 = 
  batch_v1 
 
 . 
  ComputeResource 
 
 () 
 resources 
 . 
 cpu_milli 
 = 
 2000 
 # in milliseconds per cpu-second. This means the task requires 2 whole CPUs. 
 resources 
 . 
 memory_mib 
 = 
 16 
 # in MiB 
 task 
 . 
 compute_resource 
 = 
 resources 
 task 
 . 
 max_retry_count 
 = 
 2 
 task 
 . 
 max_run_duration 
 = 
 "3600s" 
 # Tasks are grouped inside a job using TaskGroups. 
 # Currently, it's possible to have only one task group. 
 group 
 = 
  batch_v1 
 
 . 
  TaskGroup 
 
 () 
 group 
 . 
 task_count 
 = 
 3 
 group 
 . 
 task_spec 
 = 
 task 
 # Policies are used to define on what kind of virtual machines the tasks will run on. 
 # In this case, we tell the system to use "e2-standard-4" machine type. 
 # Read more about machine types here: https://cloud.google.com/compute/docs/machine-types 
 policy 
 = 
  batch_v1 
 
 . 
  AllocationPolicy 
 
 . 
  InstancePolicy 
 
 () 
 policy 
 . 
 machine_type 
 = 
 "e2-standard-4" 
 instances 
 = 
  batch_v1 
 
 . 
  AllocationPolicy 
 
 . 
  InstancePolicyOrTemplate 
 
 () 
 instances 
 . 
 policy 
 = 
 policy 
 allocation_policy 
 = 
  batch_v1 
 
 . 
  AllocationPolicy 
 
 () 
 allocation_policy 
 . 
 instances 
 = 
 [ 
 instances 
 ] 
 # Create a NetworkInterface object to specify network settings for the job 
 network_interface 
 = 
  batch_v1 
 
 . 
  AllocationPolicy 
 
 . 
  NetworkInterface 
 
 () 
 # Set the network to the specified network name within the project 
 network_interface 
 . 
 network 
 = 
 f 
 "projects/ 
 { 
 project_id 
 } 
 /global/networks/ 
 { 
 network_name 
 } 
 " 
 # Set the subnetwork to the specified subnetwork within the region 
 network_interface 
 . 
 subnetwork 
 = 
 ( 
 f 
 "projects/ 
 { 
 project_id 
 } 
 /regions/ 
 { 
 region 
 } 
 /subnetworks/ 
 { 
 subnet_name 
 } 
 " 
 ) 
 allocation_policy 
 . 
 network 
 . 
 network_interfaces 
 = 
 [ 
 network_interface 
 ] 
 job 
 = 
  batch_v1 
 
 . 
  Job 
 
 () 
 job 
 . 
 task_groups 
 = 
 [ 
 group 
 ] 
 job 
 . 
 allocation_policy 
 = 
 allocation_policy 
 # We use Cloud Logging as it's an out of the box available option 
 job 
 . 
 logs_policy 
 = 
  batch_v1 
 
 . 
  LogsPolicy 
 
 () 
 job 
 . 
 logs_policy 
 . 
 destination 
 = 
  batch_v1 
 
 . 
  LogsPolicy 
 
 . 
  Destination 
 
 . 
 CLOUD_LOGGING 
 create_request 
 = 
  batch_v1 
 
 . 
  CreateJobRequest 
 
 () 
 create_request 
 . 
 job 
 = 
 job 
 create_request 
 . 
 job_id 
 = 
 job_name 
 # The job's parent is the region in which the job will run 
 create_request 
 . 
 parent 
 = 
 f 
 "projects/ 
 { 
 project_id 
 } 
 /locations/ 
 { 
 region 
 } 
 " 
 return 
 client 
 . 
  create_job 
 
 ( 
 create_request 
 ) 
 

API

To create a job using the Batch API, use the jobs.create method and specify your job's configuration details. To specify the network for the job, include the network and subnetwork fields .

For example, to create a basic script job that runs on a specific network, make the following POST request:

  POST 
  
 h 
 tt 
 ps 
 : 
 //batch.googleapis.com/v1/projects/ PROJECT_ID 
/locations/ LOCATION 
/jobs?job_id= JOB_NAME 
 
 { 
  
 "taskGroups" 
 : 
  
 [ 
  
 { 
  
 "taskSpec" 
 : 
  
 { 
  
 "runnables" 
 : 
  
 [ 
  
 { 
  
 "script" 
 : 
  
 { 
  
 "text" 
 : 
  
 "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks." 
  
 } 
  
 } 
  
 ] 
  
 }, 
  
 "taskCount" 
 : 
  
 3 
  
 } 
  
 ], 
  
 "allocationPolicy" 
 : 
  
 { 
  
 "network" 
 : 
  
 { 
  
 "networkInterfaces" 
 : 
  
 [ 
  
 { 
  
 "network" 
 : 
  
 "projects/ HOST_PROJECT_ID 
/global/networks/ NETWORK 
" 
 , 
  
 "subnetwork" 
 : 
  
 "projects/ HOST_PROJECT_ID 
/regions/ REGION 
/subnetworks/ SUBNET 
" 
  
 } 
  
 ] 
  
 } 
  
 }, 
  
 "logsPolicy" 
 : 
  
 { 
  
 "destination" 
 : 
  
 "CLOUD_LOGGING" 
  
 } 
 } 
 

Replace the following:

  • PROJECT_ID : the project ID of your project.
  • LOCATION : the location for this job.
  • JOB_NAME : the name for this job.
  • HOST_PROJECT_ID : the project ID of the project for the network you specify:
    • If you are using a Shared VPC network, specify the host project.
    • Otherwise, specify the current project ( PROJECT_ID ).
  • NETWORK : the name of a VPC network in the current project or a Shared VPC network that is hosted by or shared with the current project.
  • REGION : the region where the subnet and the VMs for the job are located:
    • If you include the allowedLocations field to specify the allowed location for the VMs for the job, you must specify the same region here.
    • Otherwise, the region must be the same as the location you select for the job ( LOCATION ).
  • SUBNET : the name of a subnet that is part of the VPC network and is located in the same region as the VMs for the job.

What's next

Create a Mobile Website
View Site in Mobile | Classic
Share by: