-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsagemaker_template.yml
202 lines (176 loc) · 5.28 KB
/
sagemaker_template.yml
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
AWSTemplateFormatVersion: "2010-09-09"
Description: SageMaker Workshop - CloudFormation Template
Mappings:
AWSRegionToAMI:
us-east-1: {AMI: ami-0080e4c5bc078760e}
us-east-2: {AMI: ami-0cd3dfa4e37921605}
us-west-2: {AMI: ami-01e24be29428c15b2}
eu-west-1: {AMI: ami-08935252a36e25f85}
Parameters:
VPCCIDR:
Description: "VPC IP Range (Example: 192.168.0.0/24)"
Type: String
Default: "192.168.0.0/24"
PublicSubnetCIDR:
Description: "Public subnet IP Range (Example: 192.168.0.0/28)"
Type: String
Default: "192.168.0.0/28"
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VPCCIDR
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: 'SageMaker Workshop VPC'
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: 'SageMaker Workshop IGW'
InternetGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
PublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: ""
CidrBlock: !Ref PublicSubnetCIDR
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: 'SageMaker Workshop Public Subnet'
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: 'SageMaker Workshop Route Table'
DefaultPublicRoute:
Type: AWS::EC2::Route
DependsOn: InternetGatewayAttachment
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet1
EC2S3Role:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "ec2.amazonaws.com"
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3FullAccess
RoleName: 'EC2FullAccessToS3'
EC2RoleProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref EC2S3Role
InstanceProfileName: 'EC2FullAccessToS3'
EC2ForOpenCVLambdaLayer:
Type: AWS::EC2::Instance
DependsOn:
- EC2RoleProfile
- MammographyBucket
Properties:
InstanceType: t2.micro
IamInstanceProfile: !Ref EC2RoleProfile
InstanceInitiatedShutdownBehavior: terminate
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
cd /home/ec2-user
yum -y update
yum install -y gcc openssl-devel bzip2-devel libffi-devel
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
tar xzf Python-3.7.4.tgz
cd Python-3.7.4
./configure --enable-optimizations
make altinstall
sudo sed -i 's+Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin+Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin+g' /etc/sudoers
export PATH="/usr/local/bin:$PATH"
python3.7 -m pip install boto3 opencv-python numpy --target ./opencv-python37-layer
chmod -R 777 opencv-python37-layer/
cd opencv-python37-layer/
rm -r *.dist-info __pycache__
zip -r opencv-python37-layer.zip .
chmod 777 opencv-python37-layer.zip
aws s3 cp opencv-python37-layer.zip s3://${MammographyBucket}/opencv-layer/opencv-python37-layer.zip
sudo init 0
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: ""
ImageId:
Fn::FindInMap:
- AWSRegionToAMI
- {Ref: 'AWS::Region'}
- AMI
SubnetId: !Ref PublicSubnet1
Tags:
- Key: Name
Value: 'OpenCVLambdaLayerGenerator'
NotebookRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "sagemaker.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action: "*"
Resource: "*"
NotebookInstance:
Type: "AWS::SageMaker::NotebookInstance"
Properties:
InstanceType: "ml.t2.large"
RoleArn: !GetAtt NotebookRole.Arn
MammographyBucket:
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Sub "mammography-workshop-files-${AWS::Region}-${AWS::AccountId}"
Outputs:
Instance:
Description: SageMaker Notebook Instance.
Value: !Ref NotebookInstance
Role:
Description: SageMaker Notebook Instance Role.
Value: !GetAtt NotebookRole.Arn
MammographyBucket:
Description: Bucket for storing the files needed and produced by this workshop.
Value: !Ref MammographyBucket