Created
August 1, 2024 20:38
-
-
Save evertonfraga/cb1e9c23faf67482502697a9c4950aae to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# Author: Everton Fraga <[email protected]> | |
# Check if a stack name was provided | |
if [ $# -eq 0 ]; then | |
echo "Please provide a CloudFormation stack name." | |
echo "Usage: $0 <stack-name>" | |
exit 1 | |
fi | |
# Store the stack name | |
STACK_NAME="$1" | |
# Get the list of EC2 instance IDs associated with the stack | |
INSTANCE_IDS=$(aws cloudformation describe-stack-resources \ | |
--stack-name "$STACK_NAME" \ | |
--query "StackResources[?ResourceType=='AWS::EC2::Instance'].PhysicalResourceId" \ | |
--output text) | |
# Check if any instances were found | |
if [ -z "$INSTANCE_IDS" ]; then | |
echo "No EC2 instances found for stack: $STACK_NAME" | |
exit 1 | |
fi | |
# Get the first instance ID | |
FIRST_INSTANCE_ID=$(echo $INSTANCE_IDS | awk '{print $1}') | |
echo "Connecting to instance: $FIRST_INSTANCE_ID" | |
# Start an SSM session with the first instance | |
echo "aws ssm start-session --target \"$FIRST_INSTANCE_ID\"" | |
aws ssm start-session --target "$FIRST_INSTANCE_ID" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment