I am using to update a project with IAM policies. in GCP deployment manager's templates, they are using python Jinja file, but I would like to add IAM policy (assign a user/service account some role). Can someone modify the Jinja/ config file and pinpoint how I can modify?
You need to make changes to the below part of the config.yaml file and add the users or service accounts according to your need under the members line.
For example: You can add
-user:foo@bar.com
under members tab in proper section to make it owner or viewer of the project.Here's a jinja snippet that creates a new service account and adds it as an owner to an existing project. This requires assigning deployment manager the proper access to manage IAM for the project.
Please follow Adam Ocsvari's example to assign IAM policy. The old method was to get all the IAM binding policies, add a few role -> members bindings, then set all the bindings. He's providing a new method using
'type': 'gcp-types/cloudresourcemanager-v1:virtual.projects.iamMemberBinding'
. I used one of the links he provided to find the python template that assigned IAM policy bindings. The code there has a nested loop. I only needed to create a single service account and assign 1 binding:service-accounts.py
service-accounts.yaml
this example creates a k8s-service-account and assigns Dataflow admin role to it. Make sure you Grant Deployment Manager permission to set IAM policies before you start.
My code to add permissions to a service account.
Please avoid using these solutions:
It can cause concurrent IAM policy update errors. The Deployment Manager team is providing a new type binding this 2 actions together:
Check out the following implementations as part of the Cloud Foundation Toolkit provided by Google Cloud:
Cloud Foundation Toolkit NEW repo - IAM binding
Cloud Foundation Toolkit OLD repo - IAM binding
Cloud Foundation Toolkit NEW repo - Project Creation Factory