This script will assist with the creation of a GCP Project. Please review all steps that are taken to ensure that when the script is executed you are aware of all actions that will be taken place.
Table of Contents
There are a few steps that need to be taken to ensure that you have the appropriate permissions, configurations, and libraries prior to executing the script.
Requirements:
-
- virtualenv is a tool to create isolated Python environments. Be sure to use Python 3.11
-
Install python dependencies
-
Verify code and execute
-
Have the permissions to create projects within GCP
This is an example of how to sign into Cloud SDK and create a virtualenv.
Project Creator role is required at the Org or Folder level. The Billing User role is required on the billing account itself.
roles/resourcemanager.projectCreator
roles/billing.userIf you are needing to create a project that needs to have access to a Shared VPC, the account running this script will need to have the following IAM Role here
roles/compute.xpnAdminAPIs must be enabled on the project you are using to authenticate to when executing this script.
cloudbilling.googleapis.com-
Download one of the following:
-
Initialize gcloud
it is recommended that you already have a project created within GCP which you use to test and use as a playground since this will help with authentication if you are running this script locally
gcloud init
-
Create local authentication credentials for your Google Account
gcloud auth application-default login
A login screen is displayed. After you log in, your credentials are stored in the local credential file used by ADC. For more information about working with ADC in a local environment, see Local development environment.
pip3 install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install google-api-python-client
pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-api-python-client
Within the working directory there is a .env which contains the following that need to be filled out prior to execution.
# Specify if you are creating a new project or not
CREATE_PROJECT=<True/False>
# Attach project to shared VPC
SHARED_VPC_ACCESS=<True/False>
# GCP Folder ID
GCP_FOLDER_ID=<folder-id>
# GCP Project Name not unique
GCP_PROJECT_NAME=<project-name>
# GCP Project ID please leave blank if you do not have this yet! Used if you are not creating a new project
GCP_PROJECT_ID=<project-id>
# GCP Billing ID
GCP_BILLING_ID=<billing-id>
# List of Principals e.g. "user:user@examples.com,roles/storage.admin;group:group@examples.com,roles/viewer"
IAM_PRINCIPALS_AND_ROLES="user:user@example.com,roles/editor;serviceAccount:svc@example.com,roles/appengine.appAdmin;group:group@example.com,roles/viewer"
# List of APIs to enable e.g. "compute.googleapis.com,run.googleapis.com"
ENABLE_API="compute.googleapis.com,run.googleapis.com"
# List of users to be able to access the Shared VPC e.g. "user:user@examples.com,roles/storage.admin;group:group@examples.com,roles/viewer"
SHARED_VPC_USERS="user:user@example.com,roles/editor;serviceAccount:svc@example.com,roles/appengine.appAdmin;group:group@example.com,roles/viewer"
# The Region of the Shared VPC Subnet
SHARED_VPC_REGION=<region>
# The Subnet of the Shared VPC
SHARED_VPC_SUBNET=<subnet-name>
pip install -r requirements.txtAfter all the Prerequities have been completed, execute the script:
python3 create_project.pyFor now the script will be passed around but it is highly recommended to place it within its own repository.