Skip to content

Instantly share code, notes, and snippets.

@eruvanos
Last active December 10, 2018 05:11
Show Gist options
  • Save eruvanos/c9911bbaa8da64ab78c42a1b86280872 to your computer and use it in GitHub Desktop.
Save eruvanos/c9911bbaa8da64ab78c42a1b86280872 to your computer and use it in GitHub Desktop.
Useful aws utils for cli
#!/usr/bin/env bash
function load_service_credentials {
service_name=$1
# Read credentials for queue
SERVICE_KEY_JSON=$(cf service-key $service_name key | grep -v "Getting")
export AWS_ACCESS_KEY_ID=$(echo ${SERVICE_KEY_JSON} | jq '.access_key' -r)
export AWS_SECRET_ACCESS_KEY=$(echo ${SERVICE_KEY_JSON} | jq '.secret_access_key' -r)
export AWS_DEFAULT_REGION=$(echo ${SERVICE_KEY_JSON} | jq '.region' -r)
export AWS_URI=$(echo ${SERVICE_KEY_JSON} | jq '.uri' -r)
export AWS_BUCKET=$(echo ${SERVICE_KEY_JSON} | jq '.bucket' -r)
export AWS_TABLE=$(echo ${SERVICE_KEY_JSON} | jq '.table' -r)
export AWS_LOGGROUP=$(echo ${SERVICE_KEY_JSON} | jq '.log_group' -r)
echo $SERVICE_KEY_JSON
}
function send_message {
message=$1
aws sqs send-message --queue-url ${AWS_URI} --message-body "${message}"
}
function send_message_to {
service_name=$1
message=$2
load_service_credentials $service_name
send_message $message
}
function send_message_batch {
count=$1
for ((i=1;i<=$count;i++)); do
send_message "test-message-$i" &
last=$!
echo "Send message $i of $count"
if ! (( i % 100 )); then
wait
sleep 5
fi
done
wait
}
function purge {
service_name=$1
load_service_credentials $service_name
aws sqs purge-queue --queue-url $AWS_URI
}
function approximated_number_of_messages_of {
service_name=$1
load_service_credentials $service_name
attributes=$(aws sqs get-queue-attributes --queue-url $AWS_URI --attribute-names ApproximateNumberOfMessages)
echo $(echo "$attributes"|jq .Attributes.ApproximateNumberOfMessages)
}
function send_message_batch_to {
service_name=$1
count=$2
load_service_credentials $service_name
send_message_batch $count
}
function write_file_to {
service_name=$1
key=$2
load_service_credentials $service_name
aws s3 cp - $AWS_URI/$key
}
function delete_file_from {
service_name=$1
key=$2
load_service_credentials $service_name
aws s3 rm $AWS_URI/$key
}
function delete_folder_from {
service_name=$1
key=$2
load_service_credentials $service_name
aws s3 rm --recursive $AWS_URI/$key
}
function list_files_of {
service_name=$1
load_service_credentials $service_name
aws s3 ls $AWS_BUCKET --recursive|cut -c 32-
}
function cat_file_of {
service_name=$1
file=$2
load_service_credentials $service_name
aws s3 cp $AWS_URI/$file -
}
function empty_bucket {
service_name=$1
load_service_credentials $service_name
aws s3 rm $AWS_URI --recursive
}
function init_key {
service_name=$1
cf csk $service_name key
load_service_credentials $service_name
}
function delete_key {
service_name=$1
cf dsk -f $service_name key
}
function delete_all_versions {
service_name=$1
load_service_credentials $service_name
export bucket=$AWS_BUCKET
echo "Removing all versions from $bucket"
versions=`aws s3api list-object-versions --bucket $bucket |jq '.Versions'`
markers=`aws s3api list-object-versions --bucket $bucket |jq '.DeleteMarkers'`
echo "removing files"
for version in $(echo "${versions}" | jq -r '.[] | @base64'); do
version=$(echo ${version} | base64 --decode)
key=`echo $version | jq -r .Key`
versionId=`echo $version | jq -r .VersionId `
cmd="aws s3api delete-object --bucket $bucket --key $key --version-id $versionId"
echo $cmd
$cmd
done
echo "removing delete markers"
for marker in $(echo "${markers}" | jq -r '.[] | @base64'); do
marker=$(echo ${marker} | base64 --decode)
key=`echo $marker | jq -r .Key`
versionId=`echo $marker | jq -r .VersionId `
cmd="aws s3api delete-object --bucket $bucket --key $key --version-id $versionId"
echo $cmd
$cmd
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment