Buy Bitcoin Everyday with Coinbase Pro and AWS Lambda Functions

Table of Contents*

*Not financial advice

Background

I got into Bitcoin in 2017 and had a consistent monthly dollar cost averaging schedule set up.

When the price tanked in 2018 so did my interest.

I played all sorts of little psychological games with myself convincing myself why it wasn’t urgent that I make my Bitcoin purchase this month.

There was no hype, and there was very little media attention.

My interest was renewed in 2019 (fueled by the hype of reading Ben Mezrich’s book Bitcoin Billionaires) and I decided that automating my purchases was my best defense against myself and the FUD (fear uncertainty and doubt) circulating around Bitcoin.

I chronicled my first attempt at automation on the blog here: https://rhett.blog/2020/10/21/bitcoin-everyday/

And on YouTube (in a long complicated video) here: https://www.youtube.com/watch?v=UIP6_5G3v5I

This original design was difficult to test and update (involved pushing up to git, logging into AWS Lightsail, pulling from git, and hoping nothing broke) and there was a ~$2 a month fee for the Lightsail instance – yuck!

My smarter than me girlfriend was taking an AWS course, stumbled upon lambda functions and Cloudwatch automation (which are free).

This infrastructure also allows for a more modular design that is a lot easier to maintain (and is less coding to get started).

Hold on to your hats, you’re going to be blown away by how elegant this is.

Coinbase Pro API

This is the same as last time. You’ll still need an API passphrase / secret / key to automate trades.

To sign up for Coinbase Pro you’ll need proof of ID, name, email address, phone number and some other info depending on your region.

Full instructions to sign up for Coinbase Pro are here

Once you’ve signed up for Coinbase Pro, you can activate your API.

Click API under your User Name in the top right
Click +New API Key and record your Passphrase

Under Permissions I’ve Selected View Transfer and Trade because:

If you want your Python script to automatically dump the trade details into your Google Sheet you’ll need to check the View box

If you want your Python script to automate withdraws + deposits you’ll need to check the Transfer box

If you want your Python script to automate purchases you’ll need to check the Trade box

Record your API Passphrase (ray0ddnl6j in the screenshot) in a text file, we’ll need that for our Python Script to access your Coinbase information.

*** It is important not to share your API passphrase / secret / key with anyone you don’t trust and consider setting up 2 Factor Authentication to further protect your Coinbase Pro account.***

***The API credentials in these screenshots are just demonstrations of what yours might look like.***

Record your API Secret
HIghlighted Under Default Portfolio above nickname will be your API Key

Google Sheets API

This Google Sheets functionality is a nice to have, it will allow you to pull your transaction data into Google Sheets to understand how much money you’ve made.

I created this template (The Definitive Bitcoin Spreadsheet) based off of the spreadsheet I use that is compatible with the code I’ll be sharing later.

This is also the same as last time. This video does a great job of explaining.

You’ll end up with a .json file with credentials and you’ll need to include in your Lambda Function later on.

AWS Lambda Functions

Here’s where the magic happens: within your AWS console you’ll want to navigate to Lambda

And then before anything else you’ll want to navigate to layers and hit create layer in the top right

Now head over to this Notion link I’ve set up to host the code you’ll need for these Lambda Functions

https://www.notion.so/rhettre/Buy-Bitcoin-Everyday-Lambda-Functions-Cloudwatch-abe92c33a13b4be19d232d5c91edfccf

The first thing we’ll need from Notion is the layer.zip download

Back in AWS on the create layer screen upload the layers.zip and name it whatever you want and leave the runtime and license fields empty.

This is going to allow our AWS hosted Lambda function to use the cbpro and gsheets Python packages.

Once you’ve set up the layer you can head over to the functions tab on the side of the AWS Lambda screen and click create function in the top right.

In Author from Scratch, name the function whatever you want, select Python 3.8 in runtime and hit create function in the bottom right.

First, within the function we’re going to add our layer. Click on layer underneath your function name and then add a layer in the bottom right.

Choose Custom Layers and add the layer you created earlier, this will allow our script to use those included Python libraries.

If you want to include other Python libraries here is the tutorial I used to generate layers.zip: https://towardsdatascience.com/how-to-install-python-packages-for-aws-lambda-layer-74e193c76a91

Back in your Lambda Function base you’re going to want to copy and paste the code over from my Notion page into your Lambda Function

Once you’ve copied over the code you’ll want to fill in your key secret and password from the Coinbase Pro API step above

Additionally, in the Sheets updater code you’ll need to include your sheets_creds.json in the package at the same level as the lambda_function.py. You can do this by right clicking and adding new file at the folder level.

Deploy the code to save the changes and then hit test and enter any name in “event name” you can leave your test event code at the hello-world template. Once you hit create you’ll be able to run the script using the test button

***BE CAREFUL USING THE TEST BUTTON, THIS WILL CREATE A MARKET BUY ORDER IN THE EXAMPLE ABOVE***

AWS Cloudwatch

Clicking Test will now run your script once, but you want it to run once a day/week/month etc.

Cloudwatch makes this really easy.

Get into CloudWatch from the AWS Management Console and then go to Rules on the left nav

Click the blue (purple? I’m colorblind) Create Rule button

In this section select schedule and you can set either a fixed rate or for more detail you can give it a Cron expression

If you need help configuring your Cron this site is really helpful: https://crontab.guru/ (note that Cron expressions from this site are missing the ? parameter that AWS uses before the last * so you’ll need to re-add that ? expression to your Cron if you generate it from this site)

Select your lambda function from the dropdown and click configure details

Name your rule and give it a description and you’re all done!!!!

Closing

Let me know in comments here or on YouTube or email me at Rhett@rhett.blog if you have any questions or get stuck.

The beauty of this is that you can set different scripts to different automated cron schedules very easily through the AWS UI.

Additionally there is basically no programming involved in this whole process, you don’t even need a local environment since all the code is being handled in AWS.

Now you can stop worrying about the Bitcoin price, sit back, relax, and stack sats.