aws.ec2-package: AWS EC2 Client Package

Description Details Author(s) See Also Examples

Description

A simple client package for the Amazon Web Services (AWS) Elastic Cloud Compute (EC2) REST API.

Details

The examples below provide a complete workflow for setting up EC2 via this package and launching an instance.

Author(s)

Thomas J. Leeper thosjleeper@gmail.com

See Also

account_attrs() run_instances()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
## Not run: 
# check credentials
aws.signature::locate_credentials()

# check EC2 account settings
account_attrs()

# create an SSH keypair in EC2 and save locally
my_keypair <- create_keypair("r-ec2-example")
pem_file <- tempfile(fileext = ".pem")
cat(my_keypair$keyMaterial, file = pem_file)

# create a "security group" allowing access from current IP
my_sg <- create_sgroup("r-ec2-sg", "Allow my IP")
## authorize access from this IP
try(authorize_ingress(my_sg))
try(authorize_egress(my_sg))

# find an EC2 AMI, like: http://www.louisaslett.com/RStudio_AMI/
my_image <- "ami-3b0c205e"

# Launch the instance using appropriate settings
my_inst <- run_instances(image = my_image,
                         type = "t2.nano",
                         sgroup = my_sg,
                         keypair = my_keypair)
Sys.sleep(5L) # wait for instance to boot

# allocate an IPv4 address
ip <- allocate_ip("vpc")

# associate IP with instane
associate_ip(my_inst, ip)


# login to instance through browser
utils::browseURL(ip)

# login to instance through R
library("ssh")
session <- ssh::ssh_connect(paste0("ubuntu@", instance_ip), keyfile = pem_file, passwd = "rstudio")

# write a quick little R script to execute
cat("'hello world!'\nsprintf('2+2 is %d', 2+2)\n", file = "helloworld.R")
# upload it to instance
invisible(ssh::scp_upload(session, "helloworld.R"))

# execute script on instance
x <- ssh::ssh_exec_wait(session, "Rscript helloworld.R")

## disconnect from instance
ssh:ssh_disconnect(session)


# create an image from the instance to reuse later
my_image <- create_image(my_inst, "My Rstudio Image")


# when finished, completely cleanup all EC2 material
## stop and terminate the instance
stop_instances(i[[1L]])
terminate_instances(i[[1L]])

## revoke access from this IP to security group
try(revoke_ingress(my_sg))
try(revoke_egress(my_sg))

## delete keypair
delete_keypair(my_keypair)

## release IP address
release_ip(ips[[1L]])


## End(Not run)

cloudyr/aws.ec2 documentation built on Jan. 14, 2020, 4:55 a.m.