Skip to content

Instantly share code, notes, and snippets.

@jmlrt
Created December 13, 2018 10:32
Show Gist options
  • Save jmlrt/c829a16474004db2dc4d8540e879d565 to your computer and use it in GitHub Desktop.
Save jmlrt/c829a16474004db2dc4d8540e879d565 to your computer and use it in GitHub Desktop.
AWS Helpers functions
# AWS Helpers functions
#
# prerequisites:
# - awscli
# - jq
# parse aws arn
parse_arn() {
echo $1 | sed 's/\"//g;s/^.*\///'
}
# get hostname from instance tag name
hostname_from_instance() {
echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].PublicDnsName' | tr -d '"')
}
# get ip from instance tag name
ip_from_instance() {
echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].PublicIpAddress' | tr -d '"')
}
# ssh to instance using tag name
ssh_aws() {
ssh $(hostname_from_instance $1)
}
# get cloudformation stack status
stack_status() {
aws cloudformation describe-stacks --stack-name $1 | jq '.Stacks[0].StackStatus'
}
# get cloudformation stacks and status
stacks() {
stacks=$(aws cloudformation list-stacks --stack-status-filter CREATE_IN_PROGRESS CREATE_COMPLETE ROLLBACK_IN_PROGRESS ROLLBACK_FAILED ROLLBACK_COMPLETE DELETE_IN_PROGRESS DELETE_FAILED UPDATE_IN_PROGRESS UPDATE_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_COMPLETE UPDATE_ROLLBACK_IN_PROGRESS UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_ROLLBACK_COMPLETE REVIEW_IN_PROGRESS --query 'StackSummaries[*].[StackStatus,StackName]' --output text | sort -k 2 | column -t)
if [[ $# -ge 1 ]]
then
echo $stacks | grep -i $1
else
echo $stacks | less -FS
fi
}
# get cloudformation stack resources and status
stack_resources() {
aws cloudformation describe-stack-resources --stack-name $1 --query 'StackResources[*].[ResourceStatus,ResourceType,LogicalResourceId,PhysicalResourceId]' --output text | sort -k2 | column -t | less -FS
}
# get cloudformation stack parameters
stack_params() {
aws cloudformation describe-stacks --stack-name $1 --query 'Stacks[*].Parameters[*].[ParameterKey,ParameterValue]' #--output text | sort | column -t
}
# get cloudformation stack outputs
stack_outputs() {
aws cloudformation describe-stacks --stack-name $1 --query 'Stacks[*].Outputs[*].[OutputKey,OutputValue]' --output text | sort | column -t
}
# get ecs clusters
ecs_clusters() {
clusters="
for c in $(aws ecs list-clusters | jq .clusterArns[])
do
parse_arn $c
done)"
if [[ $# -ge 1 ]]
then
echo $clusters | sed 's/ /\n/g' | grep -i $1
else
echo $clusters | sed 's/ /\n/g'
fi
}
# get ecs services from cluster
ecs_services() {
services="
for s in $(aws ecs list-services --cluster $1 | jq .serviceArns[])
do
parse_arn $s
done)"
if [[ $# -ge 2 ]]
then
echo $services | sed 's/ /\n/g;s/\"//' | grep -i $2
else
echo $services | sed 's/ /\n/g;s/\"//'
fi
}
# get ecs tasks from cluster and service name
ecs_service_tasks() {
aws ecs list-tasks --cluster $1 --service-name $2
}
# stop ecs task from cluster and service name
ecs_stop_task() {
aws ecs stop-task --cluster $1 --task $2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment