2
votes

I have a need to use a Google Cloud Platform (GCP) Service Account to create Projects programmatically through the Resource Manager REST API. I want to create Firebase projects in a headless manner.

Our current solution involves imitating a user and browser to achieve this because it does not appear possible to create a Project as a Service Account through the GCP Resource Manager REST API without an Organization (or a Folder, which must be under an Organization).

When I attempt to create a Project as a Service Account, I receive a 403 Bad Request response saying, "Service accounts cannot create projects without a parent".

Attempting to create a Project as a Service Account with an empty Parent produces a 400 Bad Request response saying, "Request contains an invalid argument".

Attempting to create a Project as a Service Account with a Parent whose Type is "organization" without an Id produces a 400 Bad Request response saying, "Parent id must be numeric".

Since I don't have an Organization (calls to organizations.list return {}) and I can't create one without a domain, I'm unsure how to proceed down this path.

1
To give you a great answer, it might help us if you have a glance at How to Ask if you haven't already. It might be also useful if you could provide a minimal reproducible example. - Mat

1 Answers

5
votes

Q: How do you create Google Cloud Platform (GCP) projects programmatically using a service account without having a GCP Organization?

A: You can't.


An actual gmail.com user can create projects in No Organization, but a Service Account must specify an organization or a folder as the parent at project creation time.

Your current solution of doing it as a user is probably the only real workaround. For that, you would use an OAuth2 ClientID and Secret to obtain offline credentials for the user with the https://www.googleapis.com/auth/cloud-platform scope. Then, you can use that refresh_token to generate access_tokens that you use to call the Cloud Resource Manager API to create projects.

However, I would make a strong case for the Organization. If you can obtain a domain, you can use that domain to setup Cloud Identity and get an Organization resource. Then, you can grant the Service Account the Project Creator role on your Organization resource and it will be able to programmatically create projects.

The win here is that all the projects you create with this Service Account will be under a single Organization so you can easily grant permissions and manage access across all of the projects in a single place. A new person joins your team, and instead having to grant them access to each of your Firebase or GCP projects one at a time, you can do it by adding a single permission to the Organization resource.

More: Granting, Changing, and Revoking Access to Resources