This project is designed to help organizations manage multiple sandbox environments on Google Cloud Platform (GCP) for training their employees. It uses Infrastructure as Code (IaC) through Pulumi, which allows the setup and management of cloud resources using code.
-
Automated Project Creation:
- The project automatically creates sandbox environments (projects) on GCP. These environments are isolated spaces where employees can experiment, learn, and develop without affecting the main production environment.
-
Budget Alerts:
- Each sandbox project has a budget alert set up. This means that if the spending in a sandbox project reaches a certain limit, an alert is triggered. This helps in monitoring and controlling costs.
-
Cost Cap Mechanism:
- To prevent overspending, the project includes a mechanism to automatically stop billing when the budget limit is reached. This is crucial for organizations with strict budget constraints, such as students, researchers, or developers working in test environments.
-
Cloud Function Trigger:
- When a budget alert is triggered, a Cloud Function is automatically executed. This function acts as a
killswitch
and can perform actions like stopping billing or shutting down services to ensure that costs do not exceed the set budget.
- When a budget alert is triggered, a Cloud Function is automatically executed. This function acts as a
-
Pub/Sub Integration:
- The project uses Google Cloud Pub/Sub, a messaging service, to handle budget alerts. When an alert is triggered, a message is published to a Pub/Sub topic, which then triggers the Cloud Function.
-
Cost Management: By setting up budget alerts and automatic cost caps, organizations can ensure that their spending on sandbox projects does not exceed predefined limits. This is particularly useful for managing training environments where costs can quickly escalate if not monitored.
-
Scalability: The use of IaC with Pulumi allows organizations to easily scale the number of sandbox projects. New projects can be created with consistent configurations and budget controls, ensuring uniformity and ease of management.
-
Automation: The entire process, from project creation to budget monitoring and cost control, is automated. This reduces the administrative overhead and allows IT teams to focus on more strategic tasks.
-
Training and Development: Employees can use these sandbox environments to learn and experiment with GCP services without the risk of affecting production systems or incurring unexpected costs.
The provided diagram illustrates the workflow of the cost control mechanism:
- Budget Alert: When the spending in a sandbox project reaches a predefined limit, a budget alert is triggered.
- Cloud Pub/Sub: The alert is sent as a message to a Pub/Sub topic.
- Cloud Functions: The message triggers a Cloud Function.
- Billing API: The Cloud Function interacts with the Billing API to take necessary actions, such as stopping billing.
- Cap Spending: As a result, the spending is capped, preventing any further costs from being incurred.
We would use a master project
approach where the core infratsructure will be hosted on this project like the pubsub topic and cloud function etc.
log in using your email account using
gcloud auth application-default login
Make sure that the environment variable GOOGLE_APPLICATION_CREDENTIALS
is unset. if it is set by default then you can unset it:
# powershell
Remove-Item -Path Env:GOOGLE_APPLICATION_CREDENTIALS
# bash
unset GOOGLE_APPLICATION_CREDENTIALS
Make sure to enable all necessary api for your master project
gcloud services enable storage.googleapis.com
gcloud services enable serviceusage.googleapis.com
gcloud services enable pubsub.googleapis.com
gcloud services enable cloudfunctions.googleapis.com
gcloud services enable cloudbilling.googleapis.com
cd killswitch-function
zip -r ../function.zip .
The Quota Project Specification is required to create Budget Resource
# powershell
$env:GOOGLE_CLOUD_QUOTA_PROJECT="any-existing-project-id"
# bash
export GOOGLE_CLOUD_QUOTA_PROJECT="any-existing-project-id"
npm i -g pnpm
pnpm i
pulumi config set env:billingAccountId {your-billing-acc-id}
pulumi config set env:organizationId {your-org-id}
pulumi config set gcp:project {name-of-your-main-project-for-hosting-pubsub-and-killswitch-function}
pulumi up
The first time you run pulumi up
It will use the function.zip archive to deploy the function but if you want update it you need to explicitly set the updateFunctionFlag
to true
.
pulumi config set env:updateFunctionFlag true
See Why Disable Billing to understand the approach for sandbox environments.