Managing Cloud APIs and Libraries

Programmatically accessing Google Cloud products and services requires the use of Cloud APIs. These APIs expose a simple JSON REST interface that you can call via client libraries.

With Cloud Code, you can access this consolidated list of Google Cloud services coupled with their corresponding client libraries and documentation, browse through and enable Cloud APIs, and add Cloud Client Libraries to your project, all from your IDE.

Browsing Cloud APIs

To explore all available Google Cloud APIs within your IDE, follow these steps:

  1. Click on the Cloud Code - Cloud APIs icon Cloud Code - Cloud APIs icon from the VS Code Activity bar.
  2. Expand the Google Cloud APIs explorer tree to view all available APIs. The explorer groups Cloud APIs by category.
  3. Screenshot showing the list of Cloud APIs shown in the tree
            view explorer.

  4. Click on an API to view more details, such as its service name, status, installation instructions for its corresponding client libraries, and relevant documentation.

Enabling Cloud APIs

Additionally, you can quickly enable Cloud APIs for a project using the API details page. To do so:

  1. Within the Cloud API details page, choose a project that you'd like to enable the Cloud API for.
  2. Screenshot showing more details about selected Cloud API

  3. Click on the Enable API button.

    Once the API has been enabled you'll see a message confirming this change.

Adding client libraries to your project

In addition to exploring and enabling Cloud APIs using Cloud Code, you can also add a language-specific client library to your project. You'll have to install it; the API details page contains installation instructions for each language.

Screenshot showing install instructions for a client library corresponding
          to a Cloud API

Setting up authentication

After you've enabled the required APIs and added the necessary client libraries, you need to configure your application in order for it to be successfully authenticated. Your configuration depends on your type of development and the platform you're running on.

Once you complete the relevant authentication steps, your application can authenticate and is ready to be deployed.

Local development

Local machine

  1. Cloud Code ensures you have your Application Default Credentials (ADC) set, if you logged into Google Cloud via your IDE.
    If you logged into Google Cloud outside your IDE (for example, via the gcloud command-line tool), you need to run gcloud auth login --update-adc to set your ADC. This also lets Google Cloud client libraries find your ADC to authenticate with.

minikube

  1. Cloud Code ensures you have your Application Default Credentials (ADC) set, if you logged into Google Cloud via your IDE.
    If you logged into Google Cloud outside your IDE (for example, via the gcloud command-line tool), you need to run gcloud auth login --update-adc to set your ADC. This also lets minikube find your ADC to authenticate with.
  2. Start minikube with minikube start --addons gcp-auth. This mounts your ADC in your pods. For a detailed minikube auth guide for Google Cloud, refer to the minikube gcp-auth docs.

Other local K8s clusters

  1. Cloud Code ensures you have your Application Default Credentials (ADC) set, if you logged into Google Cloud via your IDE.
    If you logged into Google Cloud outside your IDE (for example, via the gcloud command-line tool), you need to run gcloud auth login --update-adc to set your ADC.
  2. Mount your local ~/.config/gcloud directory in your Kubernetes pods by editing the deployment manifests, so that the Google Cloud client libraries can find your credentials. You'll also need to set your Google Cloud Project ID as an environment variable named GOOGLE_CLOUD_PROJECT. Kubernetes pod configuration example:
    apiVersion: v1
    kind: Pod
    metadata:
      name: my-app
      labels:
        name: my-app
    spec:
      containers:
      - name: my-app
        image: gcr.io/google-containers/busybox
        ports:
          - containerPort: 8080
        env:
        - name: GOOGLE_CLOUD_PROJECT
          value: my-project-id
        volumeMounts:
          - mountPath: /root/.config/gcloud
            name: gcloud-volume
      volumes:
        - name: gcloud-volume
          hostPath:
            path: /path/to/home/.config/gcloud

Cloud Run

  1. Cloud Code ensures you have your Application Default Credentials (ADC) set, if you logged into Google Cloud via your IDE.
    If you logged into Google Cloud outside your IDE (for example, via the gcloud command-line tool), you need to run gcloud auth login --update-adc to set your ADC. This also lets the Cloud Run local simulated environment find your ADC to authenticate with.

Remote development

Google Kubernetes Engine

Depending on the scope of your project, you can choose how you authenticate Google Cloud services on GKE:

  • (Development only)
    1. Create a GKE cluster with the following settings:
      • Ensure you're using the service account GKE uses by default, the Compute Engine default service account, and that Access scopes is set at Allow full access to all Cloud APIs (both settings accessible within the Node Pools > Security section).
        Since the Compute Engine service account is shared by all workloads deployed on your node, this method overprovisions permissions and should only be used for development.
      • Ensure Workload Identity is not enabled on your cluster (within the Cluster > Security section).
    2. Assign the necessary roles to your service account:
      • If you're trying to access a secret, follow these Secret Manager-specific steps to set up the required roles on your service account.
      • If the Compute Engine default service account is being used, the correct IAM roles may already be applied.
      • The Understanding roles guide describes IAM role types and lists available predefined roles you can grant to identities.
  • (Recommended for production)
    1. Configure your GKE cluster and application with Workload Identity to authenticate Google Cloud services on GKE. This associates your Kubernetes service account with your Google service account.
    2. Configure your Kubernetes Deployment to reference the Kubernetes service account by setting the .spec.serviceAccountName field in your Kubernetes Deployment YAML file.
      If you're working on an app created from a Cloud Code template, this file is located under the kubernetes-manifests folder.
    3. If the Google Cloud service you're trying to access requires additional roles, grant them for the Google service account you're using to develop your app:

Cloud Run

  1. Create a new unique service account for deploying your Cloud Run application through the Service Accounts page. Ensure you select the same project your secret is stored in.
    Go to the Service Accounts page
  2. If the Google Cloud service you're trying to access requires additional roles, grant them for the Google service account you're using to develop your app. You can do so by right-clicking the secret and selecting Edit Permissions in Cloud Console.
    The Understanding roles guide describes IAM role types and lists available predefined roles you can grant to identities.
  3. Add your service account to your deploy configuration. To do so, Choose the Cloud Run: Deploy command using the Cloud Code status bar. Within the Cloud Run Deployment UI, under Revision Settings, fill in the Service Account field with your service account.
    Advanced revision settings section expanded in Cloud Run: Deploy and Service Account field filled in with service account name of the format service-account-name@project-name.iam.gserviceaccount.com

Cloud Run

Depending on the scope of your project, you can choose how you authenticate Google Cloud services on GKE:

  • (Development only)
    1. Create a GKE cluster with the following settings:
      • Ensure you're using the service account GKE uses by default, the Compute Engine default service account, and that Access scopes is set at Allow full access to all Cloud APIs (both settings accessible within the Node Pools > Security section).
        Since the Compute Engine service account is shared by all workloads deployed on your node, this method overprovisions permissions and should only be used for development.
      • Ensure Workload Identity is not enabled on your cluster (within the Cluster > Security section).
    2. Assign the necessary roles to your service account:
      • If you're trying to access a secret, follow these Secret Manager-specific steps to set up the required roles on your service account.
      • If the Compute Engine default service account is being used, the correct IAM roles may already be applied.
      • The Understanding roles guide describes IAM role types and lists available predefined roles you can grant to identities.
  • (Recommended for production)
    1. Configure your GKE cluster and application with Workload Identity to authenticate Google Cloud services on GKE. This associates your Kubernetes service account with your Google service account.
    2. Add your service account to your deploy configuration. To do so, Choose the Cloud Run: Deploy command using the Cloud Code status bar. Within the Cloud Run Deployment UI, under Revision Settings, fill in the Service Account field with your Kubernetes service account.
      Advanced revision settings section expanded in Cloud Run: Deploy and Service Account field filled in with Kubernetes service account name of the format service-account-name@project-name.iam.gserviceaccount.com
    3. If the Google Cloud service you're trying to access requires additional roles, grant them for the Google service account you're using to develop your app:

Remote development with Secret Manager permissions enabled

If you're developing remotely, using a service account for authentication, and your application uses secrets, you need to complete a few more steps in addition to the remote development instructions. These steps assign your Google service account the role required to access a particular Secret Manager secret:

  1. Open the Secret Manager view Secret Manager icon and select the secret you want to access in your code.

    Secret Manager in Cloud Code open with two secrets listed

  2. Right-click the secret and select Edit Permissions in Cloud Console. This launches the Secret Manager configuration page for that secret in your web browser.

    Right-clicked secret in Secret Manager panel

  3. Within the Info Panel in the Cloud Console, select Add member.

    Secret details listed in Console Secret page in the browser; Add member button in the expanded Info Panel highlighted

  4. Assign your service account the Secret Manager Secret Accessor role.

    Within the Add members to test panel, service account listed of the format service-account-name@project-name.iam.gservicesaccount.com in within the New members field and the Select a role dropdown is being filled in such that under the Secret Manager category, role of type 'Secret Manager Accessor' is being selected

    Your service account now has permission to access this particular secret.

Getting Support

To send feedback, report issues on GitHub, or ask a question on Stack Overflow.