Chapter 3 boto3 and Python

This package is built off of boto and reticulate. Once you have successfully installed biggr, you immediately have complete access to AWS, though in a somewhat complicated form. Let’s send ourselves a text message.

library(biggr)
library(reticulate)
library(tidyverse)

Use the boto3 function to gain access to the underlying Python module.

boto = boto3()

Following the tutorial located here, we see how incredibly powerful this package could be.

More or less, to write Python in R all you need to do is replace the .’s with $’s However it’s good to know type conversion between the two languages. This is taken from the reticulate documentation.

Client is the lowest level within the boto module, read more about it here for sns.

client <- boto$client('sns', region_name='us-east-1')
response <- client$publish(
    PhoneNumber="+15555555555",
    Message="Hello World!!!!"
)

The AWS responses are pretty dirty. This is one of the issues I’m working on. We can take the response and clean it up for others.

response 
## $ResponseMetadata
## $ResponseMetadata$RetryAttempts
## [1] 0
## 
## $ResponseMetadata$HTTPStatusCode
## [1] 200
## 
## $ResponseMetadata$RequestId
## [1] "ba8e583b-c841-5296-8eef-0d7d70961d83"
## 
## $ResponseMetadata$HTTPHeaders
## $ResponseMetadata$HTTPHeaders$`x-amzn-requestid`
## [1] "ba8e583b-c841-5296-8eef-0d7d70961d83"
## 
## $ResponseMetadata$HTTPHeaders$date
## [1] "Fri, 03 May 2019 21:45:59 GMT"
## 
## $ResponseMetadata$HTTPHeaders$`content-length`
## [1] "294"
## 
## $ResponseMetadata$HTTPHeaders$`content-type`
## [1] "text/xml"
## 
## 
## 
## $MessageId
## [1] "6614a0c6-dc61-5ca6-9987-f58064b653b3"

While this is simplified, an R user will find this much more intuitive.

send_sms <- function(phone_number = NA, 
                     message = "Hello World!", 
                     region = "us-east-1",
                     message_aws = FALSE) {
  client <- boto$client('sns', region_name=region)
  phone_number <- paste0("+", phone_number)
  response <- client$publish(
      PhoneNumber = phone_number,
      Message     = message
  )
  if(message_aws) {
    return(response)
  } else {
    return(TRUE)
  }
}

Doesn’t this just seem a lot better? Wrapping up the most useful parts of Python code into R is the first step.

send_sms(phone_number = 15555555555, message = "Hi, how are you?")
## [1] TRUE