Skip to content

Commit

Permalink
Add backend deploy script (#89)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
samuelim01 authored Nov 12, 2024
1 parent c49d008 commit 952b613
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
60 changes: 60 additions & 0 deletions devops/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Backend Deployment

This script allows you to set the desired number of tasks for each backend service.

## Prerequisites

Before using this script, ensure that you have the following:

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).

2. **Access Keys**: You may obtain the AWS environment variables from the AWS Access Portal.

## Running the Script

1. Open a terminal and navigate to the `devops/` directory where the script is located.

2. Obtain your AWS Access Keys:
- Log in to the **AWS Access Console**.
- Click on **Access keys**.
- Copy the AWS environment variables into your terminal:

```bash
export AWS_ACCESS_KEY_ID=<AccessKeyId>
export AWS_SECRET_ACCESS_KEY=<SecretAccessKey>
export AWS_SESSION_TOKEN=<SessionToken>
```

3. To scale the services to **1 task** (i.e., start the services), run the following command:

```bash
./deploy-backend.sh 1
```

4. To scale the services to **0 tasks** (i.e., stop the services), run the following command:

```bash
./deploy-backend.sh 0
```

## Troubleshooting

**Q: Why doesn't the script work when I try to run it?**

**A:** Ensure you are using the correct IAM user with sufficient permissions to update ECS services.

---

**Q: The script won't run due to a permission error**

**A:** This could be due to the script not having the correct execution permissions. Run the following command to give the script execute permissions:

```bash
chmod +x deploy-backend.sh
```

---

**Q: The script worked before but doesn't work anymore**

**A:** This could be due to the environment variables expiring. Copy a new set of environment variables as shown above.
47 changes: 47 additions & 0 deletions devops/deploy-backend.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# Configuration
AWS_REGION="ap-southeast-1"
ECS_CLUSTER="backend-cluster"
SERVICES=("question" "user" "match" "collaboration" "history")

# Validate the user input for desired number of tasks
if [[ "$1" != "0" && "$1" != "1" ]]; then
echo "Error: The desired number of tasks must be either 0 or 1."
echo "Usage: $0 <desired_tasks>"
echo "Example: $0 1 # Scale each service to 1 task"
echo "Example: $0 0 # Scale each service to 0 tasks"
exit 1
fi

DESIRED_TASKS=$1 # Desired number of tasks (0 or 1)

# Function to update ECS service
update_service() {
local service=$1

echo "Updating ECS service for $service..."

# Update ECS Service to trigger deployment with the desired number of tasks
aws ecs update-service \
--cluster "$ECS_CLUSTER" \
--service "$service-service" \
--desired-count "$DESIRED_TASKS" \
--force-new-deployment \
--region "$AWS_REGION" \
--output text > /dev/null 2>&1

if [[ $? -eq 0 ]]; then
echo "Service $service updated successfully with desired task count: $DESIRED_TASKS"
else
echo "Error updating service $service"
exit 1
fi
}

# Main script execution
for service in "${SERVICES[@]}"; do
update_service "$service"
done

echo "All services have been updated."

0 comments on commit 952b613

Please sign in to comment.