Last active
July 24, 2021 15:46
-
-
Save DavisVaughan/ef544e6ef2228920e7e7100c48def93e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(furrr) | |
# localhost -> AWS EC2 linux -> Docker running on that -> R | |
# dm_create() and dm_ip() are from an unreleased R pkg I whipped up, dockermachinery | |
# https://github.com/DavisVaughan/dockermachinery | |
# Creates 1 t2.micro EC2 instance | |
dm_create("amazonec2", "dockertest") | |
# Get the ip | |
ip <- dm_ip("dockertest") | |
# Sadly, it shoves the pem file here, and we don't have any option to use our own. | |
# (technically there is a way, but it's broken according to the internet) | |
ssh_private_key_file <- "/Users/davisvaughan/.docker/machine/machines/dockertest/id_rsa" | |
# This is the rscript command necessary to run a future command | |
# It downloads rocker/r-base and starts up a fresh container each time | |
# net host is necessary to be able to connect back up to my localhost. | |
rscript <- c("sudo", "docker", "run", "--net=host", "rocker/r-base", "Rscript") | |
# Connect! | |
cl <- makeClusterPSOCK( | |
## Public IP number of EC2 instance | |
ip, | |
## User name (always 'ubuntu') | |
user = "ubuntu", | |
## Use private SSH key registered with AWS | |
rshopts = c( | |
"-o", "StrictHostKeyChecking=no", | |
"-o", "IdentitiesOnly=yes", | |
"-i", ssh_private_key_file | |
), | |
rscript = rscript, | |
## Set up .libPaths() for the 'ubuntu' user and | |
## install future/purrr/furrr packages | |
rscript_args = c( | |
"-e", shQuote("local({p <- Sys.getenv('R_LIBS_USER'); dir.create(p, recursive = TRUE, showWarnings = FALSE); .libPaths(p)})"), | |
"-e", shQuote("if (!requireNamespace('furrr', quietly = TRUE)) install.packages('furrr')") | |
), | |
dryrun = FALSE | |
) | |
plan(cluster, workers = cl) | |
future_map(1:5, ~.x) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment