Skip to content

Instantly share code, notes, and snippets.

@mijndert
Created June 1, 2021 11:40
Show Gist options
  • Select an option

  • Save mijndert/6ea3edc80300a6185dfee88111e46b0c to your computer and use it in GitHub Desktop.

Select an option

Save mijndert/6ea3edc80300a6185dfee88111e46b0c to your computer and use it in GitHub Desktop.

Revisions

  1. mijndert created this gist Jun 1, 2021.
    22 changes: 22 additions & 0 deletions instance-connect.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #!/bin/bash

    green=`tput setaf 2`
    reset=`tput sgr0`

    echo "${green}Hostname of the resource you want to connect to:${reset}"
    read -e hostname
    echo "${green}Port of the thing you want to connect to:${reset}"
    read -e port
    echo "${green}Connecting (quit with ctrl+c)${reset}"

    export ID=$(aws ec2 describe-instances --output=text --query "Reservations[*].Instances[*].InstanceId" --filters Name=tag:Name,Values="BastionStack*")
    export AZ=$(aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[Placement.AvailabilityZone]' --filters Name=tag:Name,Values="BastionStack*")

    aws ec2-instance-connect \
    send-ssh-public-key \
    --availability-zone $AZ \
    --instance-id $ID \
    --instance-os-user ssm-user \
    --ssh-public-key file://$HOME/.ssh/id_rsa.pub &>/dev/null & disown;

    ssh ssm-user@$ID -NL $port\:$hostname\:$port