Pause AWS Resources #43
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Pause AWS Resources | |
on: | |
schedule: | |
- cron: "0 2 * * 2-6" # Runs every day at 6PM PST, Monday to Friday | |
workflow_dispatch: | |
permissions: | |
id-token: write # This is required for requesting the JWT | |
contents: write # This is required for actions/checkout | |
jobs: | |
stack-prefix: | |
name: Stack Prefix | |
uses: ./.github/workflows/.stack-prefix.yml | |
pause-resources-dev: | |
name: Pause Resources Dev | |
needs: [stack-prefix] | |
runs-on: ubuntu-24.04 | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: ${{ secrets.AWS_DEPLOY_ROLE_ARN }} | |
role-session-name: gha-pause-resources | |
aws-region: ca-central-1 | |
- name: Pause AWS Resources | |
shell: bash | |
run: | | |
CLUSTER_NAME="ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev" | |
SERVICE_NAME="${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-dev-service" | |
CLUSTER_EXISTS=$(aws ecs describe-clusters --clusters $CLUSTER_NAME --query 'clusters[0].status' --output text) | |
if [ "$CLUSTER_EXISTS" = "ACTIVE" ]; then | |
SERVICE_EXISTS=$(aws ecs describe-services --cluster $CLUSTER_NAME --services $SERVICE_NAME --query 'services[0].status' --output text) | |
if [ "$SERVICE_EXISTS" = "ACTIVE" ]; then | |
aws application-autoscaling register-scalable-target --service-namespace ecs --resource-id service/$CLUSTER_NAME/$SERVICE_NAME --scalable-dimension ecs:service:DesiredCount --min-capacity 0 --max-capacity 0 --no-cli-pager --output json | |
else | |
echo "ECS service $SERVICE_NAME does not exist in cluster $CLUSTER_NAME." | |
fi | |
else | |
echo "ECS cluster $CLUSTER_NAME does not exist." | |
fi | |
DB_CLUSTER_STATUS=$(aws rds describe-db-clusters --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-dev --query 'DBClusters[0].Status' --output text) | |
if [ "$DB_CLUSTER_STATUS" = "available" ]; then | |
aws rds stop-db-cluster --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-dev --no-cli-pager --output json | |
else | |
echo "DB cluster is not in an available state. Current state: $DB_CLUSTER_STATUS" | |
fi | |
pause-resources-test: | |
name: Pause Resources Test | |
environment: test | |
needs: [stack-prefix] | |
runs-on: ubuntu-24.04 | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: ${{ secrets.AWS_DEPLOY_ROLE_ARN }} | |
role-session-name: gha-pause-resources | |
aws-region: ca-central-1 | |
- name: Pause AWS Resources | |
shell: bash | |
run: | | |
CLUSTER_NAME="ecs-cluster-${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test" | |
SERVICE_NAME="${{ needs.stack-prefix.outputs.stack_prefix }}-node-api-test-service" | |
CLUSTER_EXISTS=$(aws ecs describe-clusters --clusters $CLUSTER_NAME --query 'clusters[0].status' --output text) | |
if [ "$CLUSTER_EXISTS" = "ACTIVE" ]; then | |
SERVICE_EXISTS=$(aws ecs describe-services --cluster $CLUSTER_NAME --services $SERVICE_NAME --query 'services[0].status' --output text) | |
if [ "$SERVICE_EXISTS" = "ACTIVE" ]; then | |
aws application-autoscaling register-scalable-target --service-namespace ecs --resource-id service/$CLUSTER_NAME/$SERVICE_NAME --scalable-dimension ecs:service:DesiredCount --min-capacity 0 --max-capacity 0 --no-cli-pager --output json | |
else | |
echo "ECS service $SERVICE_NAME does not exist in cluster $CLUSTER_NAME." | |
fi | |
else | |
echo "ECS cluster $CLUSTER_NAME does not exist." | |
fi | |
DB_CLUSTER_STATUS=$(aws rds describe-db-clusters --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-test --query 'DBClusters[0].Status' --output text) | |
if [ "$DB_CLUSTER_STATUS" = "available" ]; then | |
aws rds stop-db-cluster --db-cluster-identifier ${{ needs.stack-prefix.outputs.stack_prefix }}-aurora-test --no-cli-pager --output json | |
else | |
echo "DB cluster is not in an available state. Current state: $DB_CLUSTER_STATUS" | |
fi |