Resource definitions for GCP - Time & Space Complexity
When defining resources in GCP, it is important to understand how the time to create or update these resources changes as you add more resources.
We want to know how the number of resource definitions affects the total time taken to apply them.
Analyze the time complexity of creating multiple GCP resources using a deployment configuration.
resources:
- name: vm-instance-1
type: compute.v1.instance
properties:
zone: us-central1-a
machineType: zones/us-central1-a/machineTypes/n1-standard-1
- name: vm-instance-2
type: compute.v1.instance
properties:
zone: us-central1-a
machineType: zones/us-central1-a/machineTypes/n1-standard-1
# ... more resources defined similarly
This sequence defines multiple virtual machine instances in GCP using a resource configuration file.
Each resource definition triggers an API call to create or update that resource.
- Primary operation: API call to create or update a single resource
- How many times: Once per resource defined in the configuration
As you add more resource definitions, the number of API calls grows directly with the number of resources.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 API calls |
| 100 | 100 API calls |
| 1000 | 1000 API calls |
Pattern observation: The total operations increase in a straight line as you add more resources.
Time Complexity: O(n)
This means the time to apply resource definitions grows directly in proportion to the number of resources.
[X] Wrong: "Adding more resources will not increase the total time much because they are created together."
[OK] Correct: Each resource requires its own API call and processing time, so more resources mean more total time.
Understanding how resource definitions scale helps you design efficient cloud deployments and shows you can think about system growth clearly.
"What if we grouped multiple resource creations into a single batch API call? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of resource definitions
Resource definitions specify what cloud resources to create and their settings.Step 2: Compare options to the definition role
Only To tell GCP what cloud parts to create and how to configure them describes creating and configuring cloud parts, which matches resource definitions.Final Answer:
To tell GCP what cloud parts to create and how to configure them -> Option DQuick Check:
Resource definitions = create and configure cloud parts [OK]
- Confusing resource definitions with application code
- Thinking resource definitions monitor usage
- Believing resource definitions manually control VMs
Solution
Step 1: Check the required keys in GCP resource definitions
GCP expects 'resources' list with each item having 'name', 'type', and 'properties'.Step 2: Validate each option
resources:\n - name: my-vm\n type: compute.v1.instance\n properties:\n zone: us-central1-a uses correct keys and valid type 'compute.v1.instance'. Others use wrong keys like 'id', 'kind', or wrong root key 'resource_list'. resources:\n - name: my-vm\n type: compute.instance\n properties:\n location: us-central1-a uses incorrect type 'compute.instance'.Final Answer:
resources:\n - name: my-vm\n type: compute.v1.instance\n properties:\n zone: us-central1-a -> Option CQuick Check:
Correct keys and type = resources:\n - name: my-vm\n type: compute.v1.instance\n properties:\n zone: us-central1-a [OK]
- Using 'id' instead of 'name'
- Using 'kind' instead of 'type'
- Wrong root key like 'resource_list'
- Incorrect resource type format
resources:
- name: test-vm
type: compute.v1.instance
properties:
zone: europe-west1-b
machineType: n1-standard-1
disks:
- deviceName: boot
boot: true
autoDelete: true
initializeParams:
sourceImage: projects/debian-cloud/global/images/family/debian-11
Solution
Step 1: Locate the zone property in the resource definition
The 'zone' property is set to 'europe-west1-b' under 'properties'.Step 2: Understand zone usage in VM creation
The VM will be created in the specified zone 'europe-west1-b'.Final Answer:
europe-west1-b -> Option AQuick Check:
Zone property value = europe-west1-b [OK]
- Assuming default zone if specified
- Confusing machineType with zone
- Thinking deployment fails without explicit zone
resources:
- name: my-bucket
type: storage.v1.bucket
properties:
location: us-east1
storageClass: STANDARD
- name: my-bucket
type: storage.v1.bucket
properties:
location: us-west1
storageClass: NEARLINE
Solution
Step 1: Check resource names for uniqueness
Both resources have the same name 'my-bucket', which is not allowed.Step 2: Verify other fields
Both have 'type' and valid 'storageClass' and 'location'. Only duplicate names cause error.Final Answer:
Duplicate resource names cause deployment failure -> Option BQuick Check:
Unique resource names required = Duplicate resource names cause deployment failure [OK]
- Ignoring duplicate names
- Thinking storageClass values are invalid
- Believing location is disallowed
Solution
Step 1: Check network interface property format
Correct format uses 'networkInterfaces' list with 'network' as full resource path or relative path like 'global/networks/default'.Step 2: Check metadata startup script format
Metadata must have 'items' list with 'key' and 'value' pairs. The key for startup script is 'startup-script'.Step 3: Validate each option
resources: - name: vm-with-script type: compute.v1.instance properties: zone: us-central1-a machineType: n1-standard-1 networkInterfaces: - network: global/networks/default metadata: items: - key: startup-script value: |- #!/bin/bash echo Hello World > /var/log/startup.log correctly uses 'networkInterfaces' with full network path and metadata with 'items' list and proper startup script. Others have incorrect keys or missing 'items' list.Final Answer:
Option A correctly defines network interface and startup script -> Option AQuick Check:
Correct networkInterfaces and metadata items = resources: - name: vm-with-script type: compute.v1.instance properties: zone: us-central1-a machineType: n1-standard-1 networkInterfaces: - network: global/networks/default metadata: items: - key: startup-script value: |- #!/bin/bash echo Hello World > /var/log/startup.log [OK]
- Using 'network' instead of 'networkInterfaces'
- Missing 'items' list in metadata
- Wrong key name for startup script
- Incorrect network path format
Ready to prove it?
Pass the test and this pattern moves unknown
