0
0
GCPcloud~7 mins

Resource definitions for GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to create and manage cloud resources on Google Cloud Platform, you define them using configuration files. These files describe what resources you want, like virtual machines or storage buckets, so GCP can set them up for you automatically.
When you want to create a virtual machine instance with specific settings like CPU and memory.
When you need to set up a storage bucket to save files and data.
When you want to define a network with firewall rules to control traffic.
When you want to automate resource creation to avoid manual setup errors.
When you want to keep your infrastructure setup as code for easy updates and sharing.
Config File - compute_instance.yaml
compute_instance.yaml
resources:
- name: example-instance
  type: compute.v1.instance
  properties:
    zone: us-central1-a
    machineType: zones/us-central1-a/machineTypes/n1-standard-1
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: projects/debian-cloud/global/images/family/debian-11
    networkInterfaces:
    - network: global/networks/default
      accessConfigs:
      - name: External NAT
        type: ONE_TO_ONE_NAT

This YAML file defines a virtual machine instance named example-instance.

resources: The list of resources to create.

name: The unique name for the instance.

type: The kind of resource, here a Compute Engine VM.

properties: Settings for the VM like zone, machine type, disk, and network.

This file tells GCP to create a VM in us-central1-a zone with a Debian 11 image and default network access.

Commands
This command tells Google Cloud to create a deployment named 'example-deployment' using the resource definitions in the 'compute_instance.yaml' file. It sets up the VM as described in the file.
Terminal
gcloud deployment-manager deployments create example-deployment --config compute_instance.yaml
Expected OutputExpected
Create operation operation-1234567890abcdef started... Waiting for operation to complete... Create operation operation-1234567890abcdef completed successfully.
--config - Specifies the configuration file with resource definitions.
This command checks if the VM named 'example-instance' was created successfully by listing instances with that name.
Terminal
gcloud compute instances list --filter="name=example-instance"
Expected OutputExpected
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS example-instance us-central1-a n1-standard-1 10.128.0.2 34.68.123.45 RUNNING
--filter - Filters the list to show only the instance with the specified name.
This command deletes the deployment named 'example-deployment' and all resources it created, cleaning up your cloud environment.
Terminal
gcloud deployment-manager deployments delete example-deployment
Expected OutputExpected
Delete operation operation-abcdef1234567890 started... Waiting for operation to complete... Delete operation operation-abcdef1234567890 completed successfully.
Key Concept

If you remember nothing else from this pattern, remember: resource definitions describe what you want in GCP, and deployment commands make it real.

Common Mistakes
Using incorrect indentation or syntax in the YAML file.
YAML is sensitive to spaces and indentation; errors cause deployment failures.
Use a YAML validator or editor to ensure correct formatting before deploying.
Not specifying the correct zone or machine type in the resource properties.
GCP will reject or create resources in unexpected locations or with wrong specs.
Double-check zone and machineType values match available GCP options.
Trying to create a deployment with a name that already exists without deleting it first.
Deployment Manager will fail because deployment names must be unique.
Delete or update existing deployments before creating new ones with the same name.
Summary
Write resource definitions in a YAML file to describe your GCP resources.
Use 'gcloud deployment-manager deployments create' with the config file to create resources.
Verify resource creation with 'gcloud compute instances list' filtered by name.
Clean up resources by deleting the deployment with 'gcloud deployment-manager deployments delete'.