Created
April 12, 2017 12:45
-
-
Save serkanh/677a0d0bd14f0db8c45ab1de60a6f8e1 to your computer and use it in GitHub Desktop.
Generate ssh config file for ECS clusters
This file contains hidden or 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
#/bin/bash | |
#set -x | |
#set -o | |
CLUSTER_NAME=${1:-"dev1"} | |
BASTION_NAME=${2:-"ha-bastion"} | |
PROFILE_NAME=${3:-"HA"} | |
CONTAINER_INSTANCE=$(aws --profile="${PROFILE_NAME}" ecs list-container-instances --cluster "${CLUSTER_NAME}" --query [containerInstanceArns][0][*] --output text | xargs -n1 -I{} echo {} | cut -d '/' -f 2) | |
if [ "$#" -ne 3 ]; | |
then | |
echo "generate-ssh-config <cluster-name> <bastion-name> <profile>" | |
exit 1 | |
fi | |
getInternalip() | |
{ | |
aws --profile="${PROFILE_NAME}" ec2 describe-instances --instance-ids $1 --query 'Reservations[*].Instances[*].NetworkInterfaces[*].PrivateIpAddresses[0].PrivateIpAddress' --output text | |
} | |
CONTAINER_INSTANCE_IDS=(); | |
#creates an array with container instance ids of given cluster | |
for CI in $CONTAINER_INSTANCE; do | |
CONTAINER_INSTANCE_IDS+=(`aws --profile="${PROFILE_NAME}" ecs describe-container-instances --cluster "${CLUSTER_NAME}" --container-instances $CI --query 'containerInstances[0].ec2InstanceId' --output text`) | |
done | |
CONTAINER_INSTANCE_IPS=() | |
for instance_id in ${CONTAINER_INSTANCE_IDS[*]}; do | |
CONTAINER_INSTANCE_IPS+=(`getInternalip $instance_id`) | |
done | |
id=0 | |
for ip in ${CONTAINER_INSTANCE_IPS[*]}; do | |
((id++)) | |
cat << EOF | |
Host ${PROFILE_NAME}-${CLUSTER_NAME}-${id} | |
User ec2-user | |
HostName ${ip} | |
ProxyCommand ssh -A ${BASTION_NAME} nc %h %p | |
EOF | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment