Skip to content

Commit 952b613

Browse files
authored
Add backend deploy script (#89)
* Add backend deploy script To save costs, backend services are spun down most of the time. This was manually done by changing the desired number of tasks to 0 manually. Let's create a bash script to simplify the process. * Document backend deploy * Elaborate on access key process * Fix formatting * Clarify expired env variables
1 parent c49d008 commit 952b613

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

devops/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Backend Deployment
2+
3+
This script allows you to set the desired number of tasks for each backend service.
4+
5+
## Prerequisites
6+
7+
Before using this script, ensure that you have the following:
8+
9+
1. **AWS CLI**: You must have the AWS CLI installed on your machine to interact with AWS services. You may install AWS CLI [here](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).
10+
11+
2. **Access Keys**: You may obtain the AWS environment variables from the AWS Access Portal.
12+
13+
## Running the Script
14+
15+
1. Open a terminal and navigate to the `devops/` directory where the script is located.
16+
17+
2. Obtain your AWS Access Keys:
18+
- Log in to the **AWS Access Console**.
19+
- Click on **Access keys**.
20+
- Copy the AWS environment variables into your terminal:
21+
22+
```bash
23+
export AWS_ACCESS_KEY_ID=<AccessKeyId>
24+
export AWS_SECRET_ACCESS_KEY=<SecretAccessKey>
25+
export AWS_SESSION_TOKEN=<SessionToken>
26+
```
27+
28+
3. To scale the services to **1 task** (i.e., start the services), run the following command:
29+
30+
```bash
31+
./deploy-backend.sh 1
32+
```
33+
34+
4. To scale the services to **0 tasks** (i.e., stop the services), run the following command:
35+
36+
```bash
37+
./deploy-backend.sh 0
38+
```
39+
40+
## Troubleshooting
41+
42+
**Q: Why doesn't the script work when I try to run it?**
43+
44+
**A:** Ensure you are using the correct IAM user with sufficient permissions to update ECS services.
45+
46+
---
47+
48+
**Q: The script won't run due to a permission error**
49+
50+
**A:** This could be due to the script not having the correct execution permissions. Run the following command to give the script execute permissions:
51+
52+
```bash
53+
chmod +x deploy-backend.sh
54+
```
55+
56+
---
57+
58+
**Q: The script worked before but doesn't work anymore**
59+
60+
**A:** This could be due to the environment variables expiring. Copy a new set of environment variables as shown above.

devops/deploy-backend.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
# Configuration
4+
AWS_REGION="ap-southeast-1"
5+
ECS_CLUSTER="backend-cluster"
6+
SERVICES=("question" "user" "match" "collaboration" "history")
7+
8+
# Validate the user input for desired number of tasks
9+
if [[ "$1" != "0" && "$1" != "1" ]]; then
10+
echo "Error: The desired number of tasks must be either 0 or 1."
11+
echo "Usage: $0 <desired_tasks>"
12+
echo "Example: $0 1 # Scale each service to 1 task"
13+
echo "Example: $0 0 # Scale each service to 0 tasks"
14+
exit 1
15+
fi
16+
17+
DESIRED_TASKS=$1 # Desired number of tasks (0 or 1)
18+
19+
# Function to update ECS service
20+
update_service() {
21+
local service=$1
22+
23+
echo "Updating ECS service for $service..."
24+
25+
# Update ECS Service to trigger deployment with the desired number of tasks
26+
aws ecs update-service \
27+
--cluster "$ECS_CLUSTER" \
28+
--service "$service-service" \
29+
--desired-count "$DESIRED_TASKS" \
30+
--force-new-deployment \
31+
--region "$AWS_REGION" \
32+
--output text > /dev/null 2>&1
33+
34+
if [[ $? -eq 0 ]]; then
35+
echo "Service $service updated successfully with desired task count: $DESIRED_TASKS"
36+
else
37+
echo "Error updating service $service"
38+
exit 1
39+
fi
40+
}
41+
42+
# Main script execution
43+
for service in "${SERVICES[@]}"; do
44+
update_service "$service"
45+
done
46+
47+
echo "All services have been updated."

0 commit comments

Comments
 (0)