0
0
GCPcloud~5 mins

State management in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create cloud resources, you need to keep track of what exists and how it is configured. State management helps you remember the current setup so you can update or delete resources safely without losing track.
When you want to deploy a virtual machine and later update its settings without recreating it.
When you manage multiple cloud resources and want to keep their configurations consistent.
When you need to share the current setup with your team to avoid conflicts.
When you want to roll back changes if something goes wrong during deployment.
When you automate cloud infrastructure updates and need to know what changed.
Commands
This command creates a deployment in Google Cloud using the specified configuration file. It saves the state of the deployed resources so you can manage them later.
Terminal
gcloud deployment-manager deployments create example-deployment --config example-config.yaml
Expected OutputExpected
Create operation operation-1234567890abcdef started...
--config - Specifies the configuration file describing the resources to deploy
This command shows the current state of the deployment, including what resources exist and their status.
Terminal
gcloud deployment-manager deployments describe example-deployment
Expected OutputExpected
name: example-deployment id: 1234567890123456789 operation: status: DONE operationType: insert targetLink: https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/example-instance ...
This command updates the existing deployment with changes from the configuration file, using the saved state to apply only necessary changes.
Terminal
gcloud deployment-manager deployments update example-deployment --config example-config.yaml
Expected OutputExpected
Update operation operation-abcdef1234567890 started...
--config - Specifies the updated configuration file
This command deletes the deployment and all its resources, cleaning up the state so nothing is left behind.
Terminal
gcloud deployment-manager deployments delete example-deployment
Expected OutputExpected
Delete operation operation-0987654321fedcba started...
Key Concept

If you remember nothing else from this pattern, remember: state management keeps track of your cloud resources so updates and deletions happen safely and correctly.

Common Mistakes
Trying to update a deployment without saving or using the correct configuration file.
The deployment manager cannot know what changed and may fail or overwrite resources incorrectly.
Always use the latest configuration file that matches the current state before updating.
Deleting resources manually without deleting the deployment.
The deployment state still thinks the resources exist, causing errors in future operations.
Delete resources through the deployment manager to keep state consistent.
Summary
Create a deployment with a configuration file to save the state of your cloud resources.
Check the deployment state to see what resources exist and their status.
Update the deployment using the saved state to apply only necessary changes.
Delete the deployment to remove all resources and clear the state.