Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new Google Cloud project using gcloud

As per the documentation at https://cloud.google.com/sdk/gcloud/reference/init gcloud init myproject command does not work.

google-cloud> gcloud init myproject Initialized gcloud directory in [/Users/arungupta/workspaces/google-cloud/myproject/.gcloud]. Cloning [https://source.developers.google.com/p/myproject/r/default] into [default]. Cloning into '/Users/arungupta/workspaces/google-cloud/myproject/default'... fatal: remote error: Repository not found. You may need to create a repository for this project using the Source Code tab at https://console.developers.google.com ERROR: Command '['git', 'clone', 'https://source.developers.google.com/p/myproject/r/default', '/Users/arungupta/workspaces/google-cloud/myproject/default', '--config', 'credential.helper=gcloud.sh']' returned non-zero exit status 128 ERROR: Unable to initialize project [myproject], cleaning up [/Users/arungupta/workspaces/google-cloud/myproject]. ERROR: (gcloud.init) Unable to initialize project [myproject].

Creating a project using gcloud init minecraft-server --project minecraft-server-183 creates the project with the name minecraft-server-183.

The project so created is then not visible at https://console.developers.google.com/project.

What is the correct gcloud command to create a new project, without going to the console?

like image 870
Arun Gupta Avatar asked Mar 23 '15 14:03

Arun Gupta


People also ask

How many gcloud projects can you create in GCP portal?

Project limitsOnce your quota is reached, you can request an increase. If you have less than 30 projects remaining in your quota, you can see the number of projects you have remaining in your quota on the New Project page. For more information, see Managing project quotas.


3 Answers

Here's a script that will create a project that is editable by a user (for many reasons, such as for auditability of service accounts, you might want to create per-user projects):

#!/bin/bash
if [ "$#" -lt 3 ]; then
   echo "Usage:  ./create_projects.sh billingid project-prefix  email1 [email2 [email3 ...]]]"
   echo "   eg:  ./create_projects.sh 0X0X0X-0X0X0X-0X0X0X learnml-20170106  [email protected] [email protected]"
   exit
fi
ACCOUNT_ID=$1
shift
PROJECT_PREFIX=$1
shift
EMAILS=$@
gcloud components update
gcloud components install alpha
for EMAIL in $EMAILS; do
   PROJECT_ID=$(echo "${PROJECT_PREFIX}-${EMAIL}" | sed 's/@/-/g' | sed 's/\./-/g' | cut -c 1-30)
   echo "Creating project $PROJECT_ID for $EMAIL ... "
   # Create project
   gcloud alpha projects create $PROJECT_ID
   # Add user to project
   gcloud alpha projects get-iam-policy $PROJECT_ID --format=json > iam.json.orig
   cat iam.json.orig | sed s'/"bindings": \[/"bindings": \[ \{"members": \["user:'$EMAIL'"\],"role": "roles\/editor"\},/g' > iam.json.new
   gcloud alpha projects set-iam-policy $PROJECT_ID iam.json.new
   # Set billing id of project
   gcloud alpha billing accounts projects link $PROJECT_ID --account-id=$ACCOUNT_ID
done

Explanation of the script is on medium: https://medium.com/google-cloud/how-to-automate-project-creation-using-gcloud-4e71d9a70047#.t58mss3co and a github link to the above code (I'll update it to remove the alpha when it goes beta/GA, for example) is here: https://github.com/GoogleCloudPlatform/training-data-analyst/blob/master/blogs/gcloudprojects/create_projects.sh

like image 92
Lak Avatar answered Oct 27 '22 00:10

Lak


It is now possible with the gcloud alpha projects create command.

For more information see: https://cloud.google.com/resource-manager/

like image 33
Stephen Weinberg Avatar answered Oct 26 '22 23:10

Stephen Weinberg


Just wanted to complete the circle here.

Google Cloud CLI tool 'gcloud' supports creating of projects without the need for the 'alpha' component installed from the version 147.0.0 (March 15, 2017) onwards.

Official Reference Link: https://cloud.google.com/sdk/gcloud/reference/projects/create

Release Notes for v147.0.0: https://cloud.google.com/sdk/docs/release-notes#14700_2017-03-15

It is mentioned under subheading of Google Cloud Resource Manager

For quick reference

Synopsis

gcloud projects create [PROJECT_ID] [--no-enable-cloud-apis] [--folder=FOLDER_ID] [--labels=[KEY=VALUE,…]] [--name=NAME] [--organization=ORGANIZATION_ID] [--set-as-default] [GCLOUD_WIDE_FLAG …]

Description

Creates a new project with the given project ID. By default, projects are not created under a parent resource. To do so, use either the --organization or --folder flag.

Sample Code

gcloud projects create example-foo-bar-1 --name="Happy project" --labels=type=happy
like image 25
Eagnir Avatar answered Oct 27 '22 00:10

Eagnir