Skip to content

Instantly share code, notes, and snippets.

@luvpreetsingh
Created May 16, 2019 06:10
Show Gist options
  • Save luvpreetsingh/2a9324e087e33e0ec6fc4baa6f72e090 to your computer and use it in GitHub Desktop.
Save luvpreetsingh/2a9324e087e33e0ec6fc4baa6f72e090 to your computer and use it in GitHub Desktop.
Simple functions to try aws operations
## Installation of aws-cli,jq is required
# get IP of EC2 instances
getec2ip(){
aws ec2 describe-instances --filters "Name=tag:Name,Values=$1" --region=$2 | jq '.Reservations | .[0] | .Instances | .[0] | .NetworkInterfaces | .[0] | .PrivateIpAddresses | .[0] | .Association | .PublicIp' | cut -d '"' -f 2
}
getec2type(){
aws ec2 describe-instances --filters "Name=tag:Name,Values=$1" --region=$2 | jq '.Reservations | .[0] | .Instances | .[0] | .InstanceType '
}
getec2id(){
aws ec2 describe-instances --filters "Name=tag:Name,Values=$1" --region=$2 | jq '.Reservations | .[0] | .Instances | .[0] | .InstanceId '
}
stopec2(){
aws ec2 stop-instances --instance-ids $(aws ec2 describe-instances --filters "Name=tag:Name,Values=$1" --region=$2 | jq '.Reservations | .[0] | .Instances | .[0] | .InstanceId ') --region $2
}
detachfromalb(){
aws elbv2 deregister-targets --target-group-arn $(aws elbv2 describe-target-groups --region $3 --names "$1" | jq '. | .[] | .[].TargetGroupArn' | cut -d '"' -f 2) --targets Id=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=$2" --region=$3 | jq '.Reservations | .[0] | .Instances | .[0] | .InstanceId ' | cut -d '"' -f 2) --region $3
}
attachtoalb(){
aws elbv2 register-targets --target-group-arn $(aws elbv2 describe-target-groups --region $3 --names "$1" | jq '. | .[] | .[].TargetGroupArn' | cut -d '"' -f 2) --targets Id=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=$2" --region=$3 | jq '.Reservations | .[0] | .Instances | .[0] | .InstanceId ' | cut -d '"' -f 2) --region $3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment