GoogleCloudPlatform / python-docs-samples

Code samples used on cloud.google.com
Apache License 2.0
7.31k stars 6.38k forks source link

Unable to disable billing using python cloud function #9008

Open mutineer612 opened 1 year ago

mutineer612 commented 1 year ago

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

The issue you're having must be related to a file in this repository. We are unable to provide assistance for issues unrelated to samples in this repository.

Please include as much information as possible:

In which file did you encounter the issue?

https://github.com/GoogleCloudPlatform/python-docs-samples/blob/HEAD/functions/billing/main.py

Did you change the file? If so, how?

No

Describe the issue

I'm testing out the process and have a billing alert triggering a cloud function using sample python code. I've created a group with the requested permissions and added the service account for the cloud function. The billing alert works, triggers the function, however the python code in the function is failing to disable billing on the target project. Specifically, the codes last 2 functions to check if billing are enabled and to disable billing are returning the Exceptions from the try block.

The permissions I have used to create the custom role for a group at the org level: billing.resourceAssociations.list resourcemanager.projects.get billing.resourceAssociations.delete

Please let me know if you have any ideas. Thanks.

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

dandhlee commented 1 year ago

Hey @mutineer612, can you tell us a little bit more about the issue?

Thanks!

mutineer612 commented 1 year ago

The steps were to follow the process I linked above in describing the issue. The exception message is noted in the code line 144 'Failed to disable billing, possibly check permissions'. This output is logged in Cloud Logging. I'm not sure how else to describe the problem.

dandhlee commented 1 year ago

I don't have much leads to follow if I don't know which exact steps you took or if there's anything else that the error message is showing. Running history on your command console or equivalent would help as well.

I'll give the tutorial a try myself, then see what could be wrong.

mutineer612 commented 1 year ago

Dan,

I appreciate you taking a look. Hopefully if you follow the process you should encounter the same issue. I have 1 project for the pubsub topic and funtion to run and another target project that is triggering the billing alert that I’m attempting to disable. I can see in the logs for the function, the script runs showing the project spend increases and no action being taken, then when the threshold is exceeded the script attempts to check if billing is disabled, this fails and it moves on to disable billing which also fails. At this point it loops as the function is re-triggered about every 30min. Let me know what your results are and I can setup another test if needed.

Thanks, Joe

On Fri, Feb 3, 2023 at 6:34 PM Dan Lee @.***> wrote:

I don't have much leads to follow if I don't know which exact steps you took or if there's anything else that the error message is showing. Running history on your command console or equivalent would help as well.

I'll give the tutorial a try myself, then see what could be wrong.

— Reply to this email directly, view it on GitHub https://github.com/GoogleCloudPlatform/python-docs-samples/issues/9008#issuecomment-1416558356, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKE34R2WVKRSL4GDJXERIDDWVWPXZANCNFSM6AAAAAAT7FJEWU . You are receiving this because you were assigned.Message ID: @.*** com>

dandhlee commented 1 year ago

Thanks Joe. I'll take a look when I have time in Q2, unfortunately I won't have time to look into it by then. In the meanwhile, feel free to update this thread if you get any more info, and reach out to Cloud Support if you need any troubleshooting / billing / other help for your Google Cloud project.

dandhlee commented 1 year ago

I was able to get the function to run:

Billing disabled: {"name": "projects/dandhlee-project/billingInfo", "projectId": "dandhlee-project", "billingAccountName": "", "billingEnabled": false}

After going through the tutorial, I think a possible reason would be if you've either (1) not enabled the API on the right project and configured it, or (2) your project belongs to an organization and it can be a bit tricky to work it out.

Could you try looking into what the error message is actually running into? If you modify the code slightly for __disable_billing_for_project() from:

    except Exception:
        print('Failed to disable billing, possibly check permissions')

to:

    except Exception as e:
        print('Failed to disable billing, possibly check permissions', e)

This will help show what the actual error being thrown will be.

If you're still having trouble, please let me know what the issue is and whether you were able to resolve it or not.

mutineer612 commented 1 year ago

Dan,

I was able to get this working after adding a line to print the error to see the error in the logs. I ended up using python runtime 3.9 and everything worked correctly.

except Exception as e: print('Failed to disable billing, possibly check permissions') print (e)

Thanks, Joe

On Mon, May 29, 2023 at 9:56 PM Dan Lee @.***> wrote:

Assigned #9008 https://github.com/GoogleCloudPlatform/python-docs-samples/issues/9008 to @mutineer612 https://github.com/mutineer612.

— Reply to this email directly, view it on GitHub https://github.com/GoogleCloudPlatform/python-docs-samples/issues/9008#event-9373193539, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKE34R2WCTBADBPR4RWIX2LXIVOWHANCNFSM6AAAAAAT7FJEWU . You are receiving this because you were assigned.Message ID: <GoogleCloudPlatform/python-docs-samples/issue/9008/issue_event/9373193539 @github.com>

dandhlee commented 1 year ago

That's good to hear! For posterity, what was the issue? How did you end up fixing it?

mutineer612 commented 1 year ago

Dan,

It was the python runtime. It would not work with anything newer than 3.9, then with error logging working I could see there was a permissions issue. Ended up using the Billing Admin role for the service account executing the function to get it working. I was not able to use a custom role with the permissions described in the docs.

I don't have a copy of the exact error from the logs, however it should be something that can be reproduced.

Appreciate you for following up!

Thanks, Joe

On Tue, May 30, 2023 at 11:11 AM Dan Lee @.***> wrote:

That's good to hear! For posterity, what was the issue? How did you end up fixing it?

— Reply to this email directly, view it on GitHub https://github.com/GoogleCloudPlatform/python-docs-samples/issues/9008#issuecomment-1568707933, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKE34RZPDTFXNSCAQTABJ63XIYL3TANCNFSM6AAAAAAT7FJEWU . You are receiving this because you were mentioned.Message ID: @.*** com>

dandhlee commented 1 year ago

The logs are fine, thank you for coming back to provide the valuable feedback! I'll keep this issue open to further assess how we can better improve the docs. Thank you Joe :)

Nimish, can you help take a look at this?

earamendi-globant commented 1 year ago

If it helps, I got the exact same error. After adding this for troubleshoot I noticed that I didn't enable the Billing API

After enabling it, waiting some minutes, it worked.