-
Notifications
You must be signed in to change notification settings - Fork 37
/
batch.R
128 lines (107 loc) · 3.12 KB
/
batch.R
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Batch examples
# Note: In some cases, you'll need to wait for a step to complete in your AWS
# account before you can successfully run the next step.
# To set up the Batch compute environment, get security group and subnet info
# for the default VPC.
ec2 <- paws::ec2()
default_vpc <- ec2$describe_vpcs(
Filters = "isDefault=true"
)$Vpcs[[1]]
security_group <- ec2$describe_security_groups(
Filters = sprintf("vpc-id=%s", default_vpc$VpcId),
GroupNames = "default"
)$SecurityGroups[[1]]
subnets <- ec2$describe_subnets(
Filters = sprintf("vpc-id=%s", default_vpc$VpcId)
)$Subnets
#-------------------------------------------------------------------------------
# Set up an IAM role for Batch.
role_name <- "TestBatchServiceRole"
policy_arn <- "arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole"
trust_policy <- list(
Version = "2012-10-17",
Statement = list(
list(
Effect = "Allow",
Principal = list(
Service = "batch.amazonaws.com"
),
Action = "sts:AssumeRole"
)
)
)
iam <- paws::iam()
role <- iam$create_role(
RoleName = role_name,
AssumeRolePolicyDocument = jsonlite::toJSON(trust_policy, auto_unbox = TRUE)
)
iam$attach_role_policy(
RoleName = role_name,
PolicyArn = policy_arn
)
#-------------------------------------------------------------------------------
batch <- paws::batch()
# Set up a compute environment: the resources on which Batch jobs will run.
batch$create_compute_environment(
type = "MANAGED",
computeEnvironmentName = "TestComputeEnvironment",
computeResources = list(
type = "EC2",
desiredvCpus = 1L,
ec2KeyPair = "default",
instanceRole = "ecsInstanceRole",
instanceTypes = "optimal",
maxvCpus = 128L,
minvCpus = 0L,
securityGroupIds = security_group$GroupId,
subnets = sapply(subnets, function(x) x$SubnetId)
),
serviceRole = role$Role$Arn,
state = "ENABLED"
)
# Set up a job queue for the compute environment.
batch$create_job_queue(
computeEnvironmentOrder = list(
list(
computeEnvironment = "TestComputeEnvironment",
order = 1L
)
),
jobQueueName = "TestJobQueue",
priority = 1L,
state = "ENABLED"
)
# Add an example job definition -- sleep 10 seconds.
job_def <- batch$register_job_definition(
type = "container",
containerProperties = list(
command = list(
"sleep",
"10"
),
image = "busybox",
memory = 128L,
vcpus = 1L
),
jobDefinitionName = "sleep10"
)
# Submit a job.
job <- batch$submit_job(
jobDefinition = "sleep10",
jobName = "Example",
jobQueue = "TestJobQueue"
)
print(job)
# List the submitted job(s).
batch$list_jobs(
jobQueue = "TestJobQueue",
jobStatus = "SUBMITTED"
)
# Clean up. You may have to wait for some steps to complete.
batch$deregister_job_definition(jobDefinition = job_def$jobDefinitionArn)
batch$update_job_queue("TestJobQueue", state = "DISABLED")
batch$delete_job_queue("TestJobQueue")
batch$update_compute_environment("TestComputeEnvironment", state = "DISABLED")
batch$delete_compute_environment(computeEnvironment = "TestComputeEnvironment")
iam$detach_role_policy(role_name, policy_arn)
iam$delete_role(role_name)