-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_tunnel.sh
executable file
·46 lines (35 loc) · 1.45 KB
/
db_tunnel.sh
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
#!/bin/bash
# Check if an environment is passed as an argument
if [ -z "$1" ]; then
echo "Error: No environment provided."
echo "Usage: $0 <environment>"
echo "Example: $0 staging"
exit 1
fi
# Set the environment variable
ENVIRONMENT=$1
# Variables
ECS_CLUSTER_NAME="musiclistings-${ENVIRONMENT}"
TARGET_RDS_ENDPOINT="musiclistings-${ENVIRONMENT}-database.chnsm0veqbkc.us-east-1.rds.amazonaws.com"
# Fetch the ECS Task ARN
AWS_TASK_ARN=$(aws ecs list-tasks --cluster "${ECS_CLUSTER_NAME}" | jq -r '.taskArns[]')
# Check if a task ARN was returned
if [ -z "${AWS_TASK_ARN}" ]; then
echo "Error: No tasks found for cluster '${ECS_CLUSTER_NAME}'."
exit 1
fi
# Describe the ECS Task
AWS_TASK=$(aws ecs describe-tasks --cluster "${ECS_CLUSTER_NAME}" --tasks "${AWS_TASK_ARN}")
# Extract runtime ID and task ID
AWS_TASK_RUNTIME_ID=$(echo "${AWS_TASK}" | jq -r '.tasks[0].containers[0].runtimeId')
if [ -z "${AWS_TASK_RUNTIME_ID}" ]; then
echo "Error: Could not retrieve runtime ID for task '${AWS_TASK_ARN}'."
exit 1
fi
AWS_TASK_ID=$(echo "${AWS_TASK_RUNTIME_ID}" | cut -d "-" -f 1)
# Construct the target reference for SSM
TARGET_REFERENCE="ecs:${ECS_CLUSTER_NAME}_${AWS_TASK_ID}_${AWS_TASK_RUNTIME_ID}"
# Start the SSM session
aws ssm start-session --target "${TARGET_REFERENCE}" \
--document-name AWS-StartPortForwardingSessionToRemoteHost \
--parameters "{\"portNumber\":[\"5432\"], \"host\":[\"${TARGET_RDS_ENDPOINT}\"], \"localPortNumber\":[\"5433\"]}"