I sent regular emails from sendgrid on heroku, on python

Background

I run a python script on EC2 that sends the release date of the manga by email on a weekly basis. However, the free period of aws has passed and it costs a lot of money.

So I tried to move it to heroku, which seems to be free. I will summarize what I have tried.

1. 1. Simple flow to deploy on heroku


###heroku environment preparation(centos premise)

##Install heroku toolbelt
$ sudo wget -qO- https://toolbelt.heroku.com/install.sh | sh
$ sudo ln -s /usr/local/heroku/bin/heroku /usr/local/bin/heroku

##heroku command execution confirmation
$ heroku version
heroku-toolbelt/3.32.0 (x86_64-linux) ruby/2.0.0
You have no installed plugins.

##Authentication
$ heroku login
Enter your Heroku credentials.
Email: [email protected]
Password (typing will be hidden):
Authentication successful.

###APP preparation

$ cd myAPP

#Library installation with pip
#Write code as vi
#To test

$ git init
$ git add .
$ git commit -m "my first commit"

###Deploy to heroku

##APP registration
$ heroku create
#I don't think heroku create my APP is straightforward here
#Otherwise the APP name will be properly named on heroku

##Specify the required modules
$ pip freeze > requirements.txt
$ git add requirements.txt
$ git commit requirements.txt

##Deploy
$ git push heroku master

##View log
$ heroku logs -t

Around here, I went through the getting start listed on heroku and found out. I hate centos, when I follow the official installation of toolbelt, only debian is written, so I can install it from standalone.

reference: Getting Started with Python on Heroku | Heroku Dev Center [vagrant 1.5] How to use Heroku from CentOS 6.5 on Vagrant. --Qiita

2. Register secret information with heroku and deploy it


#Set each
$ heroku config:set SENDGRID_USER_ID="hogehoge"
$ heroku config:set SENDGRID_PASS="fugafuga"
$ heroku config:set TO_ADDRESS="[email protected]"
$ heroku config:set FROM_ADDRESS="[email protected]"

#Verification
$ heroku config
=== hogehoge Config Vars
FROM_ADDRESS:     [email protected]
SENDGRID_PASS:    fugafuga
SENDGRID_USER_ID: hogehoge
TO_ADDRESS:       [email protected]


For example

sample.py



import os
print os.environ["SENDGRID_USER_ID"]

Then you can set environment variables for each APP. This can also be set from the heroku web screen. (In that case, you can refer to it immediately from local)

Heroku is deployed via git, but if you deploy via git.heroku.com? Or github connection, the code will be published, so I want to hide the email address and path.

So, if you set the environment variable associated with the application from cli or web, it will be hidden, so I tried it.

I felt that there was a more appropriate method, but once heroku toolbelt is included, the local test seems to be no problem, so this is it.

3. 3. A python script that sends an email via sendgrid

Put sendgrid with pip and almost follow the tutorial.

However, it is as follows with some changes to use the above environment variables.

sg_test.py


import sendgrid
import os

sg = sendgrid.SendGridClient(os.environ["SENDGRID_USER_ID"], os.environ["SENDGRID_PASS"])
message = sendgrid.Mail()

message.add_to(os.environ["TO_ADDRESS"])
message.set_from(os.environ["FROM_ADDRESS"])
message.set_subject("Sending with SendGrid is Fun")
message.set_html("and easy to do anywhere, even with Python")

sg.send(message)

If you look at this tutorial that looks like heroku official, there are api_key and api_user. It's like having to issue an api token. However, it was confusing because the account id and pass used when logging in to sendgrid were all right.

Also, it took a few days to create an account with sendgrid, so I had to prepare in advance to get it done.

reference: sendgrid/sendgrid-python Python - SendGrid Documentation | SendGrid

Four. Run like cron on heroku scheduler

After deploying and checking the operation on heroku,


$ heroku run python sg_test.py

Add add-on for heroku scheduler.


$ heroku addons:add scheduler:standard 

Then you can check it from the APP screen of heroku, so plugin.png

Enter cron-like settings from the add-on management screen heroku scheduler.png

After that, you can check the operation by looking at log etc.

By the way, I was doing various things while installing the heroku scheduler, and the process scheduler was also registered. It seems that you can change the resource set of Heroku on an hourly basis in a week for resource management. Since there is a calendar GUI, I wish I could do this setting collectively with heroku scheduler ...

reference: Heroku Scheduler | Add-ons | Heroku Heroku Scheduler | Heroku Dev Center How to use heroku scheduler (Rails, sinatra) --Qiita

Summary

I was able to implement the subject matter ... but I couldn't do what I wanted to do because I wanted to send emails in batches on a weekly basis ... (I noticed it on the way, but I wanted to play with it or touch heroku lightly)

Heroku scheduer could only be set every 10 minutes, every hour, every day ... I wonder if it can be solved by adding such logic on the APP side. Also, in some cases, there is paas, and if you do not access it for 1 hour, the process will sleep, and you will need to poke with curl regularly, so it will be barren ...

After all, I felt it was easier to build one VM for 500 yen a month per digital ocean than to stick to Murikuri free. After all, I found that local / remote testing and run-time problems were difficult with paas.

Is there any paas for batch or specialized for cron execution? Maybe there is, but I don't think I can find it.

By the way, there seems to be a paas? That makes a docker container and puts it on the web Dokku alternative, so I felt that my wish could be fulfilled there. .. But is it already paas? And this is a story about making paas on your own. .. Reference: Docker-based PaaS that is easy to use like Heroku | KRAY Inc

that's all.

Recommended Posts

I sent regular emails from sendgrid on heroku, on python
Scraping with Python, posting on TwitterBot, regular execution on Heroku
I tried python on heroku for the first time
I ran python on windows
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
Notes on using MeCab from Python
I can't remember Python regular expressions
python + django + scikit-learn + mecab (1) on heroku
I sent an SMS with Python
python + django + scikit-learn + mecab (2) on heroku
Python json.loads () returns str on Heroku
[Python] Looking back on what I taught programming beginners from functions
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
Notes on accessing dashDB from python
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
Call C / C ++ from Python on Mac
Periodically run Python on Heroku Scheduler
Update Python on Mac from 2 to 3
Deploy Python face recognition model on Heroku and use it from Flutter ②
Deploy Python face recognition model on Heroku and use it from Flutter ①
I want to use jar from python
Connecting from python to MySQL on CentOS 6.4
I tried using UnityCloudBuild API from Python
What I got from Python Boot Camp
I looked back on Python classes again
[IBM Cloud] I tried to access the Db2 on Cloud table from Cloud Funtions (python)
A memo of a tutorial on running python on heroku
I want to email from Gmail using Python.
I tried Python! ] I graduated today from "What is Python! Python!"!
I tried debugging from Python via System Console
I tried launching jupyter nteract on heroku server
A story about running Python on PHP on Heroku
PHP and Python integration from scratch on Laravel
A memorandum for touching python Flask on heroku
I want to use ceres solver from python
[Node-RED] Execute Python on Anaconda virtual environment from Node-RED [Anaconda] [Python]
What I did when updating from Python 2.6 to 2.7
[Python] I want to manage 7DaysToDie from Discord! 2/3
Until you use PhantomJS with Python on Heroku
I tried sending an email with SendGrid + Python
I want to make C ++ code from Python code!
Create a Python execution environment on IBM i
Notes on oct2py calling Octave scripts from Python