Skip to content

Instantly share code, notes, and snippets.

View mjaromi's full-sized avatar

Mateusz Jaromi mjaromi

View GitHub Profile
@mjaromi
mjaromi / pwsh.stay.available
Created October 15, 2021 09:50
Simple ps1 function to stay available, always
function Stay-Available ($seconds) {
while ($True) { Start-Sleep -Seconds $seconds ; $(New-Object -ComObject 'Wscript.Shell').sendkeys('{SCROLLLOCK}') }
}
The problem is that if you have only one definition of a cluster, context or user in the `~/.kube/config` file
and you delete one of them then `kubectl` will set deleted type to `null` instead of `[]`.
Then if you try to `update-kubeconfig` using aws cli command you will see an error:
* if `contexts: null`: Tried to insert into contexts,which is a <class 'NoneType'> not a <class 'list'>
* if `users: null`: Tried to insert into users,which is a <class 'NoneType'> not a <class 'list'>
* if `clusters: null`: 'NoneType' object is not iterable
Simple workaround for above cases is to replace all `null` with `[]` then run aws cli `update-kubeconfig` command, like this:
@mjaromi
mjaromi / terraform.eks.kubernetes_config_map.aws_auth
Created February 24, 2021 14:52
Dirty fix for 'terragrunt destroy' for EKS cluster
Issue:
# terragrunt destroy
Error: Delete "http://localhost/api/v1/namespaces/kube-system/configmaps/aws-auth": dial tcp 127.0.0.1:80: connect: connection refused
Fix:
# terragrunt state rm module.eks.kubernetes_config_map.aws_auth
Removed module.eks.kubernetes_config_map.aws_auth[0]
Successfully removed 1 resource instance(s).
@mjaromi
mjaromi / aws.ecs.pause
Created February 19, 2021 08:53
Pull amazon/amazon-ecs-pause:0.1.0 image
docker pull k8s.gcr.io/pause:3.1; docker tag k8s.gcr.io/pause:3.1 amazon/amazon-ecs-pause:0.1.0
@mjaromi
mjaromi / aws.ec2.subnets.private
Created February 18, 2021 12:28
Shows private subnets for provided VPC ID
aws ec2 describe-subnets --filter Name=vpc-id,Values=vpc-1234567890 --query 'Subnets[?MapPublicIpOnLaunch==`false`].SubnetId'
@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}
@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.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 / 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 / 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}