Integrate Google Sheets With AWS Lambda (Python) to Send a Recurring Email

Ivhani Maselesele
3 min readApr 20, 2020

In the previous section, We created credentials to access a spreadsheet on google drive.

Now that we’ve have our client_secret.json file, we can use it to access our google drive file. We created two self explanatory functions in our python code:

  • get_email_content
  • get_email_addressess

These functions extract data from the spreadsheet we created in the previous section. see the code below. To test this locally, call the send_email() function at the bottom of the script. You’ll need to run the following commands to install required pip packages:

  • Install Google OAuth packages
pip install google-api-python-client PyOpenSSL oauth2client
  • Install gspread
pip install gspread

The lambda_handler function is required by AWS Lambda to execute your script.

We’re ready to run this code in AWS Lambda, but we just need to package it as a zip and upload it instead of using the code editor on AWS Lambda. We assume that you’ve:

  • Done the first few parts of this tutorial(scroll to the bottom for the links to the other parts)
  • Created an AWS Lambda function with a trigger
  • Created a spreadsheet on google drive with email addresses and content
  • Set up credentials to access a google drive file and downloaded the client_secret.json file
  • Installed Python, pip and have set up Virtualenv

Now, we’ll use Virtualenv to package our application. Most of the content in this section is from the AWS Lambda Deployment Package in Python page. Follow the following steps to do this:

Prepare Virtual Environment (Using Windows Powershell)

  • Create a virtual environment (This should create a “venv” folder).
virtualenv venv
  • Activate the environment.
venv\Scripts\activate
  • Install libraries with pip.
pipenv install gspread
  • Install even more libraries with pip.
pipenv install google-api-python-client PyOpenSSL oauth2client
  • Deactivate the virtual environment.
deactivate

Zip the Package (Windows)

Copy your python script “lambda_function.py” and “client_secret.json” into “venv\Lib\site-packages” folderas shown in the image below

  • Click “select all” in the top right of your window, see also in image below
  • Right click in any one of the files/folders that is selected
  • Navigate to “Send to” and select “Compressed (zipped) folder” to zip
  • Give this zipped file a name e.g. function.zip

Upload Package

  • In the function code section, change code entry type to: Upload a zip file
Change code entry type
  • Navigate to your zip file and select it for upload
  • Click “Save” in the top right corner and then click “test” to send email
Save and Test
  • As shown in the image below, you’ll see the logs and code output at the top of you page to help you debug if you run into any issues
logs

Congratulations, you’ve created an AWS Lambda function that will send a recurring email.

This is tutorial is broken up into 5 parts:

--

--