Skip to content

Instantly share code, notes, and snippets.

View mjaromi's full-sized avatar

Mateusz Jaromi mjaromi

View GitHub Profile
@mjaromi
mjaromi / ecs-taskDefinition-memory
Last active February 9, 2021 16:59
Show name / memory / memoryReservation for every ECS taskDefinition
aws ecs list-task-definitions --status active | jq -r '.taskDefinitionArns[]' | cut -d'/' -f2 | while read taskDefinition; do
echo -ne "$taskDefinition - " ; aws ecs describe-task-definition --task-definition $taskDefinition | jq -r '.taskDefinition.containerDefinitions[] | "\(.name) - \(.memory) - \(.memoryReservation)"'
done
@mjaromi
mjaromi / ecs-deploymentConfiguration
Created September 23, 2020 11:30
Show serviceName / desiredCount / minimum/maximumHealthyPercent for ECS services running on $ECS_CLUSTER_NAME cluster
aws ecs list-services --cluster $ECS_CLUSTER_NAME | jq -r '.serviceArns[]' | while read service ; do
aws ecs describe-services --services $service --cluster $ECS_CLUSTER_NAME | jq -r '.services[] | "\(.serviceName) - \(.desiredCount) - \(.deploymentConfiguration.minimumHealthyPercent),\(.deploymentConfiguration.maximumPercent)"'
done
@mjaromi
mjaromi / ecs-placementConstraints
Created September 23, 2020 11:30
Show serviceName / placementConstraints.type/expression for ECS services running on $ECS_CLUSTER_NAME cluster
aws ecs list-services --cluster $ECS_CLUSTER_NAME | jq -r '.serviceArns[]' | while read service ; do
aws ecs describe-services --services $service --cluster $ECS_CLUSTER_NAME | jq -r '.services[] | "\(.serviceName) - \(.placementConstraints[].type) - \(.placementConstraints[].expression)"'
done
@mjaromi
mjaromi / ecs-terraform-task_definition-diff
Last active February 9, 2021 17:01
Show diff between current and new task_definition
terragrunt plan | egrep '(new resource required)|container_definitions' | sed -n '$!N;s/\x1b\[[0-9;]*m//g;s/\n/;/;s/.*aws_ecs_task_definition.//g;s/ *(new resource required)//g;s/ *container_definitions: *"//gp;;s/" *(forces new resource)//g' | while read task_definition; do
echo -e "\nDIFF for: \033[0;31m$(echo $task_definition | cut -d';' -f1)\033[0m\n$(diff <(echo $task_definition | cut -d';' -f2- | sed 's/\\"/"/g;s/^ //g;s/ => /\n/g;s/"\[/\[/g;s/\]"/\]/g;s/ *(forces new resource)//g' | head -1 | jq) <(echo $task_definition | cut -d';' -f2- | sed 's/\\"/"/g;s/^ //g;s/ => /\n/g;s/"\[/\[/g;s/\]"/\]/g;s/ *(forces new resource)//g' | tail -1 | jq))" | egrep -v '\+\+\+|\-\-\-' | egrep '^(\+|\-|DIFF for)'
done
@mjaromi
mjaromi / aws.ecs.ec2.ssh
Last active February 9, 2021 17:04
Connect via ssh to EC2 instance in $environment on which $ecsServiceName container is running
#!/bin/bash
ecsServiceName=$1
environment=$2
taskNameEcsCluster=$(aws ecs list-clusters | jq -r '.clusterArns[]' | grep $environment | while read cluster; do aws ecs list-services --cluster $cluster | jq -r '.serviceArns[]' | grep "$ecsServiceName$" && echo $cluster; done | rev | cut -d'/' -f1 | rev | sed 'N;s/\n/ /')
echo "taskNameEcsCluster: ${taskNameEcsCluster}"
taskName=$(echo $taskNameEcsCluster | cut -d' ' -f1)
echo "taskName: ${taskName}"
ecsCluster=$(echo $taskNameEcsCluster | cut -d' ' -f2)
echo "ecsCluster: ${ecsCluster}"
ec2Address=$(aws ecs describe-tasks --task $(aws ecs list-tasks --cluster $ecsCluster | jq -r '.taskArns[]' | grep $(aws ecs describe-services --services $taskName --cluster $ecsCluster | jq -r '.services[].events[] | "\(.id) \(.message)"' | grep 'has started' | head -1 | grep -o "[0-9a-f]\{32\}")) --cluster $ecsCluster | jq -r '.tasks[].containers[] | (.networkInterfaces[0].privateIpv4Address, .networkBindings[0].bindIP) | select(length > 0)')
@mjaromi
mjaromi / ecr-number-of-untagged-images
Created October 13, 2020 21:25
Show number of UNTAGGED images per ECR repository
totalNumberOfUntagged=0
while read repository; do
numberOfUntagged=$(aws ecr list-images --repository-name ${repository} --filter tagStatus=UNTAGGED --query 'imageIds[*]' | jq -r '.[].imageDigest' | wc -l)
if [[ ${numberOfUntagged} -gt 0 ]]; then
echo "${repository} - ${numberOfUntagged}"
totalNumberOfUntagged=$((totalNumberOfUntagged + numberOfUntagged))
fi
done < <(aws ecr describe-repositories | jq -r '.repositories[].repositoryName' | sort)
echo ${totalNumberOfUntagged}
@mjaromi
mjaromi / ecr-size-of-untagged-images
Last active February 9, 2021 17:02
Show size of UNTAGGED images per ECR repository
aws ecr describe-repositories | jq -r '.repositories[].repositoryName' | sort | while read repository; do
totalSizeOfUntagged=0
while read imageSizeInBytes; do
totalSizeOfUntagged=$((totalSizeOfUntagged + imageSizeInBytes))
done < <(aws ecr describe-images --repository-name ${repository} --filter tagStatus=UNTAGGED | jq -r '.imageDetails[].imageSizeInBytes')
if [[ ${totalSizeOfUntagged} -gt 0 ]]; then
echo -ne "${repository} - "
echo ${totalSizeOfUntagged} | awk '{ print $1/1024/1024 " MB" }'
fi
done
@mjaromi
mjaromi / aws.ec2.ondemand.price
Created October 22, 2020 10:54
Show Amazon EC2 On-Demand Pricing
#!/bin/bash
awsRegion=$1
instanceType=$2
url=https://banzaicloud.com/cloudinfo/api/v1/providers/amazon/services/compute/regions/${awsRegion}/products
curl -ksLX GET ${url} | jq ".products[] | select(.type | contains(\"${instanceType}\")) | .onDemandPrice"
@mjaromi
mjaromi / aws.es.cluster.health
Created February 18, 2021 10:45
AWS ElasticSearch Cluster Health
aws es list-domain-names | jq -r .DomainNames[].DomainName | while read dn; do echo -ne "$dn - " ; curl -sk https://$(aws es describe-elasticsearch-domain --domain-name ${dn} | jq -r .DomainStatus.Endpoints.vpc)/_cluster/health | jq .status ; done
@mjaromi
mjaromi / aws.eks.configmap
Created February 18, 2021 10:51
AWS EKS ConfigMap
AWS_PROFILE=${AWS_PROFILE:-default}
EKS_CLUSTER_NAME=
KUBECONFIG=/root/.kube/config
TF_STATE=$(terragrunt output -json config_map_aws_auth | jq '.[].metadata[]')
CONFIG_MAP_NAMESPACE=$(echo "${TF_STATE}" | jq -r '.namespace')
CONFIG_MAP_NAME=$(echo "${TF_STATE}" | jq -r '.name')
curl -k \
-H "Authorization: Bearer $(AWS_PROFILE=${AWS_PROFILE} aws eks get-token --cluster-name ${EKS_CLUSTER_NAME} | jq -r .status.token)" \
-H 'Accept: application/json' \
$(yq e '.clusters[].cluster.server' ${KUBECONFIG})/api/v1/namespaces/${CONFIG_MAP_NAMESPACE}/configmaps/${CONFIG_MAP_NAME}