|
| 1 | +# Alternative GCP Account Setup Guide |
| 2 | + |
| 3 | +In a number of countries, it is not possible to setup a GCP billing account as an individual. |
| 4 | + |
| 5 | +If instructed by your teaching crew, follow the steps below to setup your GCP account. |
| 6 | + |
| 7 | +## Creating and setting project & billing account |
| 8 | + |
| 9 | +Visit the website console.cloud.google.com and create an account as in the picture below. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +Click on **AGREE AND CONTINUE**. |
| 14 | + |
| 15 | + |
| 16 | +### Configuration |
| 17 | + |
| 18 | +First, let's connect to GCP. Run the following command and follow the steps: |
| 19 | + |
| 20 | +```bash |
| 21 | +gcloud auth login |
| 22 | +``` |
| 23 | + |
| 24 | +Run the command below to save your GitHub username in an environment variable that we'll use later: |
| 25 | + |
| 26 | +```bash |
| 27 | +echo "export GITHUB_USERNAME='$(gh api user | jq -r .login)'" >> ~/.zshrc |
| 28 | +``` |
| 29 | + |
| 30 | +<br> |
| 31 | + |
| 32 | +Next, we will save the Billing Account ID you were provided with in an environment variable. Change `BILLING_ACCOUNT_ID` into the ID you received 👇 |
| 33 | + |
| 34 | +```bash |
| 35 | +echo "export BILLING_ACCOUNT='BILLING_ACCOUNT_ID'" >> ~/.zshrc |
| 36 | +exec zsh |
| 37 | +``` |
| 38 | + |
| 39 | +Finally, let's also store a name for your project in an environment variable. The project name will be `lewagon-yourgithubusername-ds`. Run this command: |
| 40 | + |
| 41 | +```bash |
| 42 | +echo "export MY_GPROJECT='lewagon-${(L)GITHUB_USERNAME}-ds'" >> ~/.zshrc |
| 43 | +exec zsh |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +❌ Depending on your GitHub username, you may face an issue. If it's the case please contact a TA 🙋♂️ |
| 49 | + |
| 50 | +## Create the project, set up billing, and budget alerts |
| 51 | + |
| 52 | +Run the commands below. ❌ If an error appears, call the BM / TA 🙋♂️ |
| 53 | + |
| 54 | +Create project: |
| 55 | + |
| 56 | +```bash |
| 57 | +gcloud projects create "${MY_GPROJECT}" |
| 58 | +``` |
| 59 | + |
| 60 | +Set the project as your current project: |
| 61 | + |
| 62 | +```bash |
| 63 | +gcloud config set project "${MY_GPROJECT}" |
| 64 | +``` |
| 65 | + |
| 66 | +Link the billing account: |
| 67 | +```bash |
| 68 | +gcloud billing projects link "${MY_GPROJECT}" --billing-account ${BILLING_ACCOUNT} |
| 69 | +``` |
| 70 | + |
| 71 | +Set budget alerts per 1$ |
| 72 | +```bash |
| 73 | +gcloud billing budgets create \ |
| 74 | + --billing-account="${BILLING_ACCOUNT}" \ |
| 75 | + --display-name="${MY_GPROJECT}" \ |
| 76 | + --filter-projects=projects/"${MY_GPROJECT}" \ |
| 77 | + --budget-amount=5 \ |
| 78 | + --threshold-rule=percent=0.20 \ |
| 79 | + --threshold-rule=percent=0.40 \ |
| 80 | + --threshold-rule=percent=0.60 \ |
| 81 | + --threshold-rule=percent=0.80 |
| 82 | +``` |
| 83 | + |
| 84 | +❌ If an error appeared, call the BM / TA 🙋♂️ |
| 85 | + |
| 86 | + |
| 87 | +## Set up a Service Account |
| 88 | + |
| 89 | +Last part of the setup, we need to create a service account and configure it on your computer so GCP can identify you and you are able to launch gcloud command from your machine ! 💻 |
| 90 | + |
| 91 | +Check the current project: |
| 92 | + |
| 93 | +```bash |
| 94 | +gcloud config get-value project |
| 95 | +``` |
| 96 | + |
| 97 | +This command retrieves the currently set project ID in your gcloud configuration. |
| 98 | +It's important to ensure you're working in the correct project before proceeding with other commands. It should be `lewagon-yourgithubusername-ds`. |
| 99 | + |
| 100 | +If at this point you have an error ❌, call a TA. 🙋♂️ |
| 101 | +If the project is not the one you've set up, call a TA. 🙋♂️ |
| 102 | + |
| 103 | +Create a service account: |
| 104 | + |
| 105 | +```bash |
| 106 | +gcloud iam service-accounts create my-service-account --display-name "My Service Account" |
| 107 | +``` |
| 108 | + |
| 109 | +This command creates a new service account named "my-service-account" with the display name "My Service Account". |
| 110 | +Service accounts are used to authenticate applications and services to Google Cloud resources. |
| 111 | + |
| 112 | +Grant the `owner` role to the service account: |
| 113 | + |
| 114 | +```bash |
| 115 | +gcloud projects add-iam-policy-binding ${MY_GPROJECT} --member="serviceAccount:my-service-account@${MY_GPROJECT}.iam.gserviceaccount.com" --role="roles/owner" |
| 116 | +``` |
| 117 | + |
| 118 | +This command adds the `owner` role to the newly created service account for the specified projects. |
| 119 | +This grants full access to all resources in the project, so use with caution. |
| 120 | + |
| 121 | +Create a key for the service account: |
| 122 | + |
| 123 | +```bash |
| 124 | +gcloud iam service-accounts keys create key.json --iam-account=my-service-account@${MY_GPROJECT}.iam.gserviceaccount.com |
| 125 | +``` |
| 126 | + |
| 127 | +This generates a new private key for the service account and saves it as `key.json`. This key file is used to authenticate as the service account. |
| 128 | + |
| 129 | +Move the key file: |
| 130 | + |
| 131 | +```bash |
| 132 | +mkdir ~/code/${GITHUB_USERNAME}/gcp |
| 133 | +mv key.json ~/code/${GITHUB_USERNAME}/gcp/ |
| 134 | +``` |
| 135 | + |
| 136 | +This command moves the `key.json` file to a specific directory in your home folder. |
| 137 | + |
| 138 | +Set the GOOGLE_APPLICATION_CREDENTIALS environment variable: |
| 139 | + |
| 140 | +```bash |
| 141 | +echo "export GOOGLE_APPLICATION_CREDENTIALS='/~/code/${GITHUB_USERNAME}/gcp/key.json'" >> ~/.zshrc |
| 142 | +``` |
| 143 | +This adds an export command to your `.zshrc` file, setting the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path of your key file. This allows Google Cloud client libraries to automatically find and use the credentials for authentication. |
| 144 | + |
| 145 | + |
| 146 | +## Check |
| 147 | + |
| 148 | +Reset the terminal: |
| 149 | + |
| 150 | +```bash |
| 151 | +exec zsh |
| 152 | +``` |
| 153 | + |
| 154 | +Check your project is set up and has a billing account : |
| 155 | + |
| 156 | +```bash |
| 157 | +bash <(curl -s https://raw.githubusercontent.com/lewagon/data-setup/refs/heads/nogcp/checks/gcp_check.sh) |
| 158 | +``` |
| 159 | + |
| 160 | +If at this point you have an error ❌, call a TA. 🙋♂️ |
| 161 | + |
| 162 | +Otherwise, congratulations! You're all set! 🎉🎉 |
0 commit comments