2

I have written a deployment manager template which will deploy cloud scheduler, pubsub, bigquery, bucket, cloud build, cloud function inside a new project. So when we are deploying for the first time, it is giving an error because by default in a new project, the API of those services are disabled. So my question is how to Enable cloud scheduler API, pubsub API, bigquery API, bucket API, cloud build API and cloud function in GCP using deployment manager?

Thanks in advance.

Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230
Arghya Roy
  • 429
  • 3
  • 13
  • This old article seems to use an undocumented mechanism to enable services in deployment manager ... https://medium.com/google-cloud/google-cloud-deployment-manager-865931dd6880 Is there a reason to use Deployment Manager as opposed to Terraform? – Kolban Jun 14 '22 at 04:06
  • As part of theStack Overflow culture, if the answer below has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Hector Martinez Rodriguez Jun 22 '22 at 18:52

1 Answers1

0

First, you need to know the URL of all the api endpoints that you want to enable; you can search for them in the Google APIs Explorer. Usually the URL has the form $api.googleapis.com; as an example, for PubSub API the URL is pubsub.googleapis.com.

The Deployment Manager's template to enable the PubSub API would be something like this:

Using Python:

def GenerateConfig(context):
  """Generate configuration."""

  resources = []

  resources.append({
    'name': 'enable_pubsub',
    'type': 'deploymentmanager.v2.virtual.enableService',
    'properties': {
      'consumerId': 'project:' + context.properties['project'],
      'serviceName': 'pubsub.googleapis.com'
    }
        })
  return {'resources': resources}

Using Jinja:

resources:
- name: enable_pubsub
  type: deploymentmanager.v2.virtual.enableService
  properties:
    consumerId: "project: {{ properties['project'] }}"
    serviceName: pubsub.googleapis.com

And to use this template to enable a particular API, you could use gcloud command:

gcloud deployment-manager deployments create $name --template $template_file --properties project:$project_id

If you want to enable multiple APIs at the same time, you can use the extend method in python and create a list of dictionaries with all the APIs you need to enable, here is the code example to enable pubsub and bigquery APIs in one run:

def GenerateConfig(context):
  """Generate configuration."""

  resources = []

  resources.extend([{
    'name': 'enable_pubsub',
    'type': 'deploymentmanager.v2.virtual.enableService',
    'properties': {
      'consumerId': 'project:' + context.properties['project'],
      'serviceName': 'pubsub.googleapis.com'
    }},
    {
    'name': 'enable_bigquery',
    'type': 'deploymentmanager.v2.virtual.enableService',
    'properties': {
      'consumerId': 'project:' + context.properties['project'],
      'serviceName': 'bigquery.googleapis.com'
    }}]
)
  return {'resources': resources}

Using Jinja:

resources:
- name: enable_pubsub
  type: deploymentmanager.v2.virtual.enableService
  properties:
    consumerId: "project: {{ properties['project'] }}"
    serviceName: pubsub.googleapis.com
- name: enable_bigquery
  type: deploymentmanager.v2.virtual.enableService
  properties:
    consumerId: "project: {{ properties['project'] }}"
    serviceName: bigquery.googleapis.com
  • without using python, is there any jinja templete or yaml template available for that? Please let me know. – Arghya Roy Jul 06 '22 at 13:46
  • @ArghyaRoy take a look at the edited answer. – Gabriel Robledo Ahumada Jul 06 '22 at 15:17
  • Thanks a lot, I solved my problem using this. I am also facing a another issue for deployment manager, It will be great if you give a look into that. My issue is https://stackoverflow.com/questions/72900882/unable-to-delete-and-upload-object-containg-bucket-in-gcp-using-deployment-manag – Arghya Roy Jul 07 '22 at 15:56
  • @ArghyaRoy if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution – Gabriel Robledo Ahumada Jul 07 '22 at 17:09