Last active
March 22, 2021 20:49
-
-
Save DavisVaughan/5aac4a2757c0947a499d25d28a8ca89b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#devtools::install_github("cloudyr/aws.ec2") | |
# devtools::install_github("cloudyr/aws.ec2", ref = devtools::github_pull(38)) | |
# ^ Must actually install PR 38. Some broken code in the main repo | |
#devtools::install_github("cloudyr/aws.signature") | |
library(aws.signature) | |
library(aws.ec2) | |
library(furrr) | |
aws.signature::use_credentials() | |
# Louis's AMI | |
image <- "ami-fd2ffe87" | |
describe_images(image) | |
# Check your VPC and Security Group settings | |
#s <- describe_subnets() | |
g <- describe_sgroups("sg-7497f03c") # <- Your preferred security group here | |
kp <- describe_keypairs("dvaughan") # <- Your keypair here | |
# Launch the instance using appropriate settings | |
i <- run_instances(image = image, | |
type = "t2.medium", | |
sgroup = g, | |
min = 2L, # <- Launching 2 medium instances | |
keypair = kp$dvaughan) | |
##### Start an as.cluster.ec2_instance function around here | |
# It should accept the i and the .pem file path | |
# Below is the possible implementation | |
# Ensure we are running and initialized | |
is_running <- vector("logical", length(i)) | |
is_initialized <- vector("logical", length(i)) | |
while(!all(is_initialized)) { | |
for(ii in seq_along(i)) { | |
# Current instance | |
i_ii <- instance_status(i[[ii]]) | |
# Initially, we don't get any information | |
if(length(i_ii) == 0) { | |
next() | |
} | |
# First check if we are at least running | |
if(!is_running[ii]) { | |
if(unlist(i_ii$item$instanceState$name) == "running") { | |
is_running[ii] <- TRUE | |
message("Instance ", ii, " is running. Now initialzing.") | |
} | |
} | |
# Then check if we are initialized | |
if(!is_initialized[ii]) { | |
if(unlist(i_ii$item$instanceStatus$status) == "ok") { | |
is_initialized[ii] <- TRUE | |
message("Instance ", ii, " is initialized.") | |
} | |
} | |
} | |
} | |
# Get the public IPs | |
ips <- vapply( | |
i, | |
function(i_ii) { | |
i_di <- describe_instances(i_ii) | |
i_di[[1]]$instancesSet[[1]]$networkInterfaceSet$privateIpAddressesSet$association$publicIp | |
}, | |
FUN.VALUE = character(1) | |
) | |
public_ip <- ips | |
# This is where my pem file lives (password to connect essentially). | |
ssh_private_key_file <- "~/Desktop/programming/AWS/key-pair/dvaughan.pem" | |
# Connect! | |
cl <- makeClusterPSOCK( | |
## Public IP number of EC2 instance | |
public_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 | |
), | |
## 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("install.packages(c('future', 'purrr', 'furrr'))") | |
), | |
dryrun = FALSE | |
) | |
############## Now we have a cluster object we can use with future | |
# # Run all future / furrr code here! | |
plan(cluster, workers = cl) | |
future_map(1:2, ~.x) | |
#future_map(1:2, ~Sys.sleep(10)) | |
# stop_instances(i) | |
# start_instances(i) | |
# Shutdown permanently | |
parallel::stopCluster(cl) | |
terminate_instances(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment