-
Notifications
You must be signed in to change notification settings - Fork 4
/
cloudformation-template-cn.yaml
282 lines (254 loc) · 10.4 KB
/
cloudformation-template-cn.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
AWSTemplateFormatVersion: '2010-09-09'
Description: 'CloudFormation template for EC2 instance for ModelHubStack'
Metadata:
AWS::CloudFormation::Interface:
StackName:
Default: "ModelHubStack"
Parameters:
InstanceType:
Type: String
Default: m5.xlarge
Description: EC2 instance type
AMIId:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/canonical/ubuntu/server-minimal/24.04/stable/20241002/amd64/hvm/ebs-gp3/ami-id
Description: Ubuntu 24.04 AMI ID
HuggingFaceHubToken:
Type: String
Description: Optional Hugging Face Hub Token
Default: ""
WandbApiKey:
Type: String
Description: Optional WANDB API Key for view W&B in wandb.ai
Default: ""
WandbBaseUrl:
Type: String
Description: Optional WANDB Base URL for view W&B own Wandb portal
Default: ""
Resources:
EC2Instance:
Type: AWS::EC2::Instance
CreationPolicy:
ResourceSignal:
Timeout: PT30M
Properties:
Tags:
- Key: Name
Value: ModelHubServer
InstanceType: !Ref InstanceType
ImageId: !Ref AMIId
SecurityGroupIds:
- !Ref EC2SecurityGroup
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
VolumeSize: 500
VolumeType: gp3
IamInstanceProfile: !Ref EC2InstanceProfile
UserData:
Fn::Base64:
!Sub
- |
#!/bin/bash
echo "Starting UserData script execution" >> /var/log/user-data.log
sudo apt update
sudo apt install -y git
if ! command -v aws &> /dev/null; then
#安装awscli
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
fi
#install nodejs
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
sudo npm install --global yarn
# download file
sudo su ubuntu
cd /home/ubuntu/
git clone --recurse-submodule https://github.com/aws-samples/llm_model_hub.git
cd /home/ubuntu/llm_model_hub
yarn install
#install pm2
sudo yarn global add pm2
# 等待一段时间以确保实例已完全启动
sleep 30
#如果是中国区则执行
if [[ "$AWS_REGION" == cn* ]]; then
echo "AWS Region is in China: $AWS_REGION"
cd /home/ubuntu/llm_model_hub/backend/
sudo -u ubuntu bash 0.setup-cn.sh
fi
# 尝试使用 IMDSv2 获取 token
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
# Get the EC2 instance's public IP
EC2_IP=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/public-ipv4)
# Get the current region and write it to the backend .env file
REGION=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/placement/region)
echo "Get IP:$EC2_IP and Region:REGION " >> /var/log/user-data.log
# Generate a random string key
RANDOM_KEY=$(openssl rand -base64 32 | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
# Write the EC2_IP and SageMakerRoleArn to frontend .env file
echo "REACT_APP_API_ENDPOINT=http://$EC2_IP:8000/v1" > /home/ubuntu/llm_model_hub/.env
echo "REACT_APP_API_KEY=$RANDOM_KEY" >> /home/ubuntu/llm_model_hub/.env
echo "REACT_APP_CALCULATOR=https://aws-gpu-memory-caculator.streamlit.app/" >> /home/ubuntu/llm_model_hub/.env
## write sagemaker role
echo "AK=" >> /home/ubuntu/llm_model_hub/backend/.env
echo "SK=" >> /home/ubuntu/llm_model_hub/backend/.env
echo "role=${SageMakerRoleArn}" >> /home/ubuntu/llm_model_hub/backend/.env
echo "region=$REGION" >> /home/ubuntu/llm_model_hub/backend/.env
echo "db_host=127.0.0.1" >> /home/ubuntu/llm_model_hub/backend/.env
echo "db_name=llm" >> /home/ubuntu/llm_model_hub/backend/.env
echo "db_user=llmdata" >> /home/ubuntu/llm_model_hub/backend/.env
echo "db_password=llmdata" >> /home/ubuntu/llm_model_hub/backend/.env
echo "api_keys=$RANDOM_KEY" >> /home/ubuntu/llm_model_hub/backend/.env
echo "HUGGING_FACE_HUB_TOKEN=${HuggingFaceHubToken}" >> /home/ubuntu/llm_model_hub/backend/.env
echo "WANDB_API_KEY=${WandbApiKey}" >> /home/ubuntu/llm_model_hub/backend/.env
echo "WANDB_BASE_URL=${WandbBaseUrl}" >> /home/ubuntu/llm_model_hub/backend/.env
echo "MAX_MODEL_LEN=4096" >> /home/ubuntu/llm_model_hub/backend/.env
# Set proper permissions
sudo chown -R ubuntu:ubuntu /home/ubuntu/
RANDOM_PASSWORD=$(openssl rand -base64 12 | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
aws ssm put-parameter --name "/${AWS::StackName}/RandomPassword" --value "$RANDOM_PASSWORD" --type "SecureString" --overwrite --region ${AWS::Region}
cd /home/ubuntu/llm_model_hub/backend
sudo -u ubuntu bash 01.setup.sh
sleep 30
#add user in db
source ../miniconda3/bin/activate py311
conda activate py311
python3 users/add_user.py demo_user $RANDOM_PASSWORD default
#build image
cd /home/ubuntu/llm_model_hub/backend/byoc
sudo -u ubuntu bash build_and_push.sh
sleep 5
#upload dummy tar.gz
sudo -u ubuntu ../../miniconda3/envs/py311/bin/python startup.py
#start backend
cd /home/ubuntu/llm_model_hub/backend/
sudo -u ubuntu bash 02.start_backend.sh
sleep 15
#start frontend
cd /home/ubuntu/llm_model_hub/
sudo -u ubuntu pm2 start pm2run.config.js
# 安装 cfn-signal
sudo pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
# 发送成功信号
/usr/local/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region}
echo "RandomPassword=$RANDOM_PASSWORD" >> /etc/environment
echo "Run User Data Script Done! " >> /var/log/user-data.log
- SageMakerRoleArn: !GetAtt SageMakerExecutionRole.Arn
HuggingFaceHubToken: !Ref HuggingFaceHubToken
WandbApiKey: !Ref WandbApiKey
WandbBaseUrl: !Ref WandbBaseUrl
EC2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow SSH, port 8000 and 3000
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 8000
ToPort: 8000
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 3000
ToPort: 3000
CidrIp: 0.0.0.0/0
EC2Role:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws-cn:iam::aws:policy/CloudWatchLogsFullAccess
- arn:aws-cn:iam::aws:policy/AmazonEC2ReadOnlyAccess
- arn:aws-cn:iam::aws:policy/AmazonSageMakerFullAccess
Policies:
- PolicyName: CloudFormationSignalPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: cloudformation:SignalResource
Resource: !Sub 'arn:aws-cn:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${AWS::StackName}/*'
- PolicyName: SSMParameterAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ssm:PutParameter
Resource: !Sub 'arn:aws-cn:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${AWS::StackName}/*'
EC2InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: "/"
Roles:
- !Ref EC2Role
SageMakerExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: sagemaker.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws-cn:iam::aws:policy/AmazonSageMakerFullAccess
Policies:
- PolicyName: S3AccessPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
- s3:DeleteObject
- s3:ListBucket
- s3:CreateBucket
Resource:
- arn:aws-cn:s3:::*
SSMConsoleAccessRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws-cn:iam::aws:policy/AmazonSSMFullAccess
Policies:
- PolicyName: SSMConsoleAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ssm:StartSession
- ssm:DescribeSessions
- ssm:TerminateSession
- ssm:ResumeSession
Resource: '*'
Outputs:
InstanceId:
Description: ID of the EC2 instance
Value: !Ref EC2Instance
PublicIP:
Description: Public IP of the EC2 instance
Value: !GetAtt EC2Instance.PublicIp
SageMakerRoleArn:
Description: ARN of the SageMaker Execution Role
Value: !GetAtt SageMakerExecutionRole.Arn
RandomPasswordParameter:
Description: AWS Systems Manager Parameter name for the random password
Value: !Sub '/${AWS::StackName}/RandomPassword'