Reputation: 47
I am a beginner to cloud.I have a GCP account with multiple projects in it,I have a gcf.Now i am deploying same function again and again individually for each projects from console.So is there any way i Can deploy one cloud function in all projects by just looping the projectIDs using terraform or anyother platforms?
Upvotes: 2
Views: 1895
Reputation: 3898
Assuming you have your function code in Google Cloud Source Repositories, and you just want to deploy the same code in all projects, you can create a simple BASH script to do so.
First, you need to recover all the projects you have:
gcloud projects list --format 'value(projectId)'
Then, for each project, deploy the function (I'm assuming Nodejs 12 and an HTTP trigger, but edit at your convenience):
for project in $(gcloud projects list --format 'value(projectId)');
do gcloud functions deploy <FUNCTION_NAME> \
--source https://source.developers.google.com/projects/<PROJECT_ID>/repos/<REPOSITORY_ID>/ \
--runtime nodejs12 \
--trigger-http \
--project $project;
done
To do anything fancier, check the other answer from wisp.
Upvotes: 1
Reputation: 456
You can define your function and everything that repeats in each project in a module and then use this module in each project definition. To do it you'll need to explicitly define each of your project in terraform configuration.
It might be worth doing if you can utilize other terraform feature e.g. tracking state, keeping infrastructure as a code, transparency, reusability and increase infrastructure complexity without making everything confusing.
Otherwise if you not going to do anything complex but instead all you need to do it deploy the same function over multiple projects and nothing more complex is planned for the observable future then Bash scripting with GCP CLI tool is your Swiss knife. You can check this as a reference: https://cloud.google.com/functions/docs/quickstart
Upvotes: 1