项目作者: cloudyr

项目描述 :
AWS EC2 Client Package
高级语言: R
项目地址: git://github.com/cloudyr/aws.ec2.git
创建时间: 2014-12-24T14:41:08Z
项目社区:https://github.com/cloudyr/aws.ec2

开源协议:

下载


AWS EC2 Client Package

aws.ec2 is a simple client package for the Amazon Web Services (AWS) Elastic Cloud Compute (EC2) REST API, which can be used to monitor use of AWS web services.

To use the package, you will need an AWS account and to enter your credentials into R. Your keypair can be generated on the IAM Management Console under the heading Access Keys. Note that you only have access to your secret key once. After it is generated, you need to save it in a secure location. New keypairs can be generated at any time if yours has been lost, stolen, or forgotten. The aws.iam package profiles tools for working with IAM, including creating roles, users, groups, and credentials programmatically; it is not needed to use IAM credentials.

To use the package, you will need an AWS account and to enter your credentials into R. Your keypair can be generated on the IAM Management Console under the heading Access Keys. Note that you only have access to your secret key once. After it is generated, you need to save it in a secure location. New keypairs can be generated at any time if yours has been lost, stolen, or forgotten. The aws.iam package profiles tools for working with IAM, including creating roles, users, groups, and credentials programmatically; it is not needed to use IAM credentials.

A detailed description of how credentials can be specified is provided at: https://github.com/cloudyr/aws.signature/. The easiest way is to simply set environment variables on the command line prior to starting R or via an Renviron.site or .Renviron file, which are used to set environment variables in R during startup (see ? Startup). They can be also set within R:

  1. Sys.setenv("AWS_ACCESS_KEY_ID" = "mykey",
  2. "AWS_SECRET_ACCESS_KEY" = "mysecretkey",
  3. "AWS_DEFAULT_REGION" = "us-east-1",
  4. "AWS_SESSION_TOKEN" = "mytoken")

Code Examples

The basic idea of the package is to be able to launch and control EC2 instances. You can read this blog post from AWS about how to run R on EC2.

A really simple example is to launch an instance that comes preloaded with an RStudio Server Amazon Machine Image (AMI):

  1. # Describe the AMI (from: http://www.louisaslett.com/RStudio_AMI/)
  2. image <- "ami-3b0c205e" # us-east-1
  3. #image <- "ami-93805fea" # eu-west-1
  4. describe_images(image)
  5. # Check your VPC and Security Group settings
  6. ## subnet
  7. subnets <- describe_subnets()
  8. ## security group
  9. my_sg <- create_sgroup("r-ec2-sg", "Allow my IP", vpc = subnets[[1L]])
  10. ## use existing ip address or create a new one
  11. ips <- describe_ips()
  12. if (!length(ips)) {
  13. ips[[1L]] <- allocate_ip("vpc")
  14. }
  15. # create an SSH keypair
  16. my_keypair <- create_keypair("r-ec2-example")
  17. pem_file <- tempfile(fileext = ".pem")
  18. cat(my_keypair$keyMaterial, file = pem_file)
  19. # Launch the instance using appropriate settings
  20. i <- run_instances(image = image,
  21. type = "t2.micro", # <- you might want to change this
  22. subnet = subnets[[1L]],
  23. sgroup = my_sg,
  24. keypair = my_keypair)
  25. Sys.sleep(5L) # wait for instance to boot
  26. # associate IP address with instance
  27. instance_ip <- get_instance_public_ip(i)
  28. if (is.na(instance_ip)) {
  29. instance_ip <- associate_ip(i, ips[[1L]])$publicIp
  30. }
  31. # authorize access from this IP
  32. try(authorize_ingress(my_sg))
  33. try(authorize_egress(my_sg))

With the instance up and running, you can now access it a number of ways. Given it’s an RStudio Server instance, you can just navigate to the public IP address via your browser:

  1. # open RStudio server in browser
  2. browseURL(paste0("http://", instance_ip))

Or you can login via ssh or putty on the command line. Thanks to the ssh package, you can also create an SSH connection directly in R:

  1. # log in to instance
  2. library("ssh")
  3. session <- ssh::ssh_connect(paste0("ubuntu@", instance_ip), keyfile = pem_file, passwd = "rstudio")
  4. # write a quick little R script to execute
  5. cat("'hello world!'\nsprintf('2+2 is %d', 2+2)\n", file = "helloworld.R")
  6. # upload it to instance
  7. invisible(ssh::scp_upload(session, "helloworld.R"))
  8. # execute script on instance
  9. x <- ssh::ssh_exec_wait(session, "Rscript helloworld.R")
  10. ## disconnect from instance
  11. ssh_disconnect(session)

Another option is to again use ssh to setup an interactive session that will be usable with remoter. This requires running

  1. remoter::server()

on the instance and then logging into it locally with remoter::client(). This is left as an exercise for advanced users.

Regardless of how you access the instance, make sure to turn it off and clean up after yourself:

  1. ## stop and terminate the instance
  2. stop_instances(i[[1L]])
  3. terminate_instances(i[[1L]])
  4. ## revoke access from this IP to security group
  5. try(revoke_ingress(my_sg))
  6. try(revoke_egress(my_sg))
  7. ## delete keypair
  8. delete_keypair(my_keypair)
  9. ## release IP address
  10. release_ip(ips[[1L]])

Installation

CRAN
Downloads
Travis Build Status
codecov.io

This package is not yet on CRAN. To install the latest development version you can install from the cloudyr drat repository:

  1. # latest stable version
  2. install.packages("aws.ec2", repos = c(getOption("repos"), "http://cloudyr.github.io/drat"))

Or, to pull a potentially unstable version directly from GitHub:

  1. if(!require("remotes")){
  2. install.packages("remotes")
  3. }
  4. remotes::install_github("cloudyr/aws.ec2")

cloudyr project logo