RobTheRobot16
RobTheRobot16

Reputation: 341

How To Email Results of Script Within Google Cloud Infrastructure (Design Query Regarding Pub/Sub & Emailing & Other Options)

This is more of a design question on what to use within Google Cloud's infrastructure to obtain the results from a Python script.

Take the following scenario: we have over 60 projects and one central project for Stackdriver logging and the such.

It is from this central project I want to run a Python script (using Cloud Scheduler which then triggers the Cloud Function) to obtain a list of disks that haven't had their snapshot taken in the past 24 hours, those that aren't assigned to a snapshot schedule, and the snapshot schedules that have names that do not match our naming convention. I have the script already prepared, and it works very well from my workstation (producing a list of dictionaries of the desired results per project).

However, my question is: where should I send the results to? And how could I then have an email sent out to the appropriate people to action it?

I've played about with sending the object attributes to Pub/Sub within the central project, but this requires me to manually pull the messages, and I can't see any way of scheduling the pull request. I also don't see an option of sending out an email from Pub/Sub to an email address, and so the only option seems to be to create an email Cloud Function which is then triggered whenever one of the Subscriptions receives a new message from the first Cloud Function containing the original script.

I suppose I could simply set this up on one of our Windows VM instances and convert the script to PowerShell, but I was rather hoping to keep it out of a VM if at all possible.

Has anyone done this before? And if so, what did you use to get the desired results?

Upvotes: 0

Views: 259

Answers (1)

Puteri
Puteri

Reputation: 3789

I think you can use Sendgrid API to send emails from your Cloud Function. It's very easy to set up it has a free plan which includes 12,000 per month and has an API for Python :D.

You can signup using the Google Cloud Marketplace selecting the free plan.

Then create an API key for your code here. If you only need to send mails I suggest you can select the option Restricted Access and for Mail Send give Full Access or the level you think will work for you.

Here's a code snippet for you:

import logging
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Email
from python_http_client.exceptions import HTTPError

def send_mail(request):
    log = logging.getLogger(__name__)

    SENDGRID_API_KEY = 'SG.blahblahblah'
    sg = SendGridAPIClient(SENDGRID_API_KEY)


    """
    Maybe here goes the code you use to check what you need
    """


    APP_NAME = "Testing"

    html_content = f"""
    Here goes your mail body in HTML format
    """

    message = Mail(
        to_emails="[email protected]",
        from_email=Email('[email protected]', "Your name or your app name"),
        subject=f"Warning!!!!",
        html_content=html_content
        )

    try:
        response = sg.send(message)
        log.info(f"email.status_code={response.status_code}")
        return f'Your mail was sent!'
    except HTTPError as e:
        log.error(e)

And don't forget to add the sendgrid lib to your requirements.txt file:

# Function dependencies, for example:
# package>=version
sendgrid

Hope this can help you.

Upvotes: 2

Related Questions