-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrstudio_sagemaker.yaml
183 lines (167 loc) · 4.6 KB
/
rstudio_sagemaker.yaml
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
Parameters:
KeyName:
Description: SSH key pair to use for instance login
Type: AWS::EC2::KeyPair::KeyName
InstanceTypeParameter:
Type: String
Default: t2.medium
AllowedValues:
- t2.medium
- m2.large
Description: Enter t2.medium or t2.large. Default is t2.medium.
Mappings:
Region:
us-east-1:
HostAmi: ami-04505e74c0741db8d
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsHostnames: true
Tags:
- Key: Name
Value: RStudio SageMaker VPC
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: RStudio SageMaker Internet Gateway
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.0.0/24
MapPublicIpOnLaunch: true
AvailabilityZone: !Select
- '0'
- !GetAZs ''
Tags:
- Key: Name
Value: RStudio SageMaker Public Subnet
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: RStudio SageMaker Public Route Table
PublicRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet
RouteTableId: !Ref PublicRouteTable
Instance:
Type: AWS::EC2::Instance
Properties:
KeyName: !Ref KeyName
InstanceType:
Ref: InstanceTypeParameter
ImageId: !FindInMap [Region, !Ref "AWS::Region", HostAmi]
IamInstanceProfile: !Ref InstanceProfile
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
VolumeSize: '30'
Encrypted: 'true'
SubnetId: !Ref PublicSubnet
SecurityGroupIds:
- Ref: PublicSubnetSecurityGroup
Tags:
- Key: Name
Value: RStudio Server
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
sudo apt-get update -y
sudo apt-get install -y libcurl-devel openssl-devel libxml2-devel python3
sudo apt-get install docker.io -y
sudo docker pull rocker/rstudio:4.2
CPUAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: true
AlarmActions:
- arn:aws:swf:us-east-1:068454867054:action/actions/AWS_EC2.InstanceId.Stop/1.0
OKActions: []
InsufficientDataActions: []
MetricName: CPUUtilization
Namespace: AWS/EC2
Statistic: Average
Dimensions:
- Name: InstanceId
Value: !Ref Instance
Period: 3600
EvaluationPeriods: 1
DatapointsToAlarm: 1
Threshold: 10
ComparisonOperator: LessThanOrEqualToThreshold
PublicSubnetSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: rstudio-sagemaker-sg
GroupDescription: RStudio SageMaker Instance Security Group
VpcId: !Ref VPC
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: '22'
IpProtocol: tcp
ToPort: '22'
- CidrIp: 0.0.0.0/0
FromPort: '8787'
IpProtocol: tcp
ToPort: '8787'
EC2Role:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/IAMReadOnlyAccess
- arn:aws:iam::aws:policy/AmazonSageMakerFullAccess
RoleName: ec2-rstudio-sagemaker
SageMakerRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- sagemaker.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSageMakerFullAccess
RoleName: sagemaker-service-role
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: "/"
Roles:
- Ref: EC2Role
InstanceProfileName: rstudio-sagemaker-instance
Outputs:
RStudio:
Description: RStudio SSH command
Value: !Sub "ssh -L 8787:localhost:8787 -i ${KeyName}.pem ec2-user@${Instance.PublicDnsName}"