-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-ec2.yaml
246 lines (199 loc) · 8.72 KB
/
deploy-ec2.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
AWSTemplateFormatVersion: "2010-09-09"
Description: CloudFormation template to deploy an EC2 instance, clone a private Git repo, and set up a MAAP app on Docker.
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair for SSH access.
Type: String
InstanceType:
Description: EC2 instance type.
Type: String
Default: t3.medium
AllowedValues: [t2.micro, t3.medium, t3.large, t3.xlarge]
AMIId:
Description: AMI ID for the instance.
Type: AWS::EC2::Image::Id
Default: ami-005fc0f236362e99f
SubnetId:
Description: Subnet ID to deploy the instance.
Type: AWS::EC2::Subnet::Id
SecurityGroupId:
Description: Security Group ID to associate with the instance.
Type: AWS::EC2::SecurityGroup::Id
VolumeSize:
Description: Root volume size in GiB.
Type: Number
Default: 100
TagName:
Description: Name tag for the EC2 instance.
Type: String
Default: MAAP-Anthropic
IAMInstanceProfile:
Description: IAM instance profile ARN.
Type: String
GitRepoURL:
Description: SSH URL for the private Git repository.
Type: String
MongoDBClusterName:
Type: String
Description: The name of the MongoDB Cluster
Default: "MongoDBCluster-Anthropic"
MongoDBUserName:
Type: String
Description: MongoDB User Name
MongoDBPassword:
Type: String
Description: MongoDB Password
APIPUBLICKEY:
Type: String
Description: MongoDB Atlas API_PUBLIC_KEY
APIPRIVATEKEY:
Type: String
Description: MongoDB Atlas API_PRIVATE_KEY
GROUPID:
Type: String
Description: MongoDB Atlas GROUP_ID
AWSRegion:
Description: AWS Region for the EC2 instance.
Type: String
Default: us-east-1
AvailabilityZone:
Type: String
Description: EC2 Availability Zone
LoggerReplicas:
Description: Logger Service Replicas.
Type: Number
Default: 3
LoaderReplicas:
Description: Loader Service Replicas.
Type: Number
Default: 3
MainReplicas:
Description: Main Service Replicas.
Type: Number
Default: 3
UIReplicas:
Description: UI Service Replicas.
Type: Number
Default: 3
Resources:
EC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
KeyName: !Ref KeyName
ImageId: !Ref AMIId
SubnetId: !Ref SubnetId
AvailabilityZone: !Ref AvailabilityZone
SecurityGroupIds:
- !Ref SecurityGroupId
BlockDeviceMappings:
- DeviceName: "/dev/sda1"
Ebs:
VolumeSize: !Ref VolumeSize
VolumeType: gp2
DeleteOnTermination: true
Tags:
- Key: Name
Value: !Ref TagName
IamInstanceProfile: !Ref IAMInstanceProfile
UserData:
Fn::Base64: !Sub |
#!/bin/bash
set -euo pipefail
LOG_FILE="/home/ubuntu/deployment.log"
exec > >(tee -a $LOG_FILE) 2>&1
echo "Starting instance initialization at $(date)..."
# Update and install necessary packages
echo "Updating system packages..."
if ! sudo apt update -y; then
echo "System update failed. Continuing..."
fi
echo "Installing required packages..."
if ! sudo apt install -y python3 python3-pip git docker.io docker-compose ksh python3-venv; then
echo "Package installation failed. Continuing..."
fi
# Add current user to the docker group
echo "Configuring Docker..."
if ! sudo usermod -aG docker ubuntu; then
echo "Failed to add user to Docker group. Continuing..."
fi
# Clone the private Git repository
echo "Cloning Git repository..."
if ! sudo -u ubuntu git clone ${GitRepoURL} /home/ubuntu/MAAP-Files; then
echo "Git clone failed. Continuing..."
fi
# Navigate to the repository
cd /home/ubuntu/MAAP-Files || { echo "Repository directory not found. Exiting."; exit 1; }
# Set environment variables
echo "Setting up environment variables..."
{
echo "AWS_REGION='${AWSRegion}'"
echo "API_PUBLIC_KEY='${APIPUBLICKEY}'"
echo "API_PRIVATE_KEY='${APIPRIVATEKEY}'"
echo "GROUP_ID='${GROUPID}'"
} > /home/ubuntu/MAAP-Files/.env
# Deploy MongoDB cluster
echo "Deploying MongoDB cluster..."
chmod +x mongodb_cluster_manager.ksh || echo "Failed to set executable permission for mongodb_cluster_manager.ksh."
if ! ./mongodb_cluster_manager.ksh deploy "${MongoDBClusterName}" "${MongoDBUserName}" "${MongoDBPassword}"; then
echo "MongoDB cluster deployment failed. Continuing..."
fi
# Deploy MongoDB Vector Index
echo "Deploying MongoDB Vector Index and inserting data..."
chmod +x mongodb_create_vectorindex.ksh || echo "Failed to set executable permission for mongodb_create_vectorindex.ksh."
if ! ./mongodb_create_vectorindex.ksh; then
echo "MongoDB Vector Index deployment failed. Continuing..."
fi
# Copy environment file
echo "Copying environment file..."
if [ -f /home/ubuntu/MAAP-Files/.env ]; then
sudo cp /home/ubuntu/MAAP-Files/.env /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/ui || { echo "Failed to copy .env to UI directory"; exit 1; }
sudo cp /home/ubuntu/MAAP-Files/.env /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/loader || { echo "Failed to copy .env to Loader directory"; exit 1; }
sudo cp /home/ubuntu/MAAP-Files/.env /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/logger || { echo "Failed to copy .env to Logger directory"; exit 1; }
sudo cp /home/ubuntu/MAAP-Files/.env /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/main/app || { echo "Failed to copy .env to Main App directory"; exit 1; }
# Change ownership and permissions to ensure accessibility
echo "Setting ownership and permissions for .env files..."
sudo chown ubuntu:ubuntu /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/ui/.env || { echo "Failed to set ownership for UI .env"; exit 1; }
sudo chmod 644 /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/ui/.env || { echo "Failed to set permissions for UI .env"; exit 1; }
sudo chown ubuntu:ubuntu /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/loader/.env || { echo "Failed to set ownership for Loader .env"; exit 1; }
sudo chmod 644 /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/loader/.env || { echo "Failed to set permissions for Loader .env"; exit 1; }
sudo chown ubuntu:ubuntu /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/logger/.env || { echo "Failed to set ownership for Logger .env"; exit 1; }
sudo chmod 644 /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/logger/.env || { echo "Failed to set permissions for Logger .env"; exit 1; }
sudo chown ubuntu:ubuntu /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/main/app/.env || { echo "Failed to set ownership for Main App .env"; exit 1; }
sudo chmod 644 /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic/main/app/.env || { echo "Failed to set permissions for Main App .env"; exit 1; }
echo "Environment files configured successfully."
else
echo ".env file not found, aborting!"
exit 1
fi
# Build Docker images and start services
echo "Building Docker images and starting services..."
cd /home/ubuntu/MAAP-Files/MAAP-AWS-Anthropic || { echo "MAAP-AWS-Anthropic directory not found. Exiting."; exit 1; }
chmod +x build-images.ksh || echo "Failed to set executable permission for build-images.ksh."
if ! ./build-images.ksh; then
echo "Docker image build failed. Continuing..."
fi
LOGGER_REPLICAS=${LoggerReplicas}
LOADER_REPLICAS=${LoaderReplicas}
MAIN_REPLICAS=${MainReplicas}
UI_REPLICAS=${UIReplicas}
export LOGGER_REPLICAS LOADER_REPLICAS MAIN_REPLICAS UI_REPLICAS
echo "Setting replicas:"
echo "Logger: $LOGGER_REPLICAS"
echo "Loader: $LOADER_REPLICAS"
echo "Main: $MAIN_REPLICAS"
echo "UI: $UI_REPLICAS"
if ! docker-compose up -d; then
echo "Docker Compose failed to start services. Continuing..."
fi
echo "Instance initialization completed at $(date)."
Outputs:
InstanceId:
Description: Instance ID of the deployed EC2 instance.
Value: !Ref EC2Instance
PublicDNS:
Description: Public DNS of the deployed EC2 instance.
Value: !GetAtt EC2Instance.PublicDnsName
EC2PublicIP:
Description: Public IPv4 address of the EC2 instance
Value: !GetAtt EC2Instance.PublicIp