Set variables for AWS commands
First I move my metadata.json to
Either add to script and source, or add to bashrc and export
metadata=~/.metadata.json
source <(jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" $metadata)
region=$(jq -r ".aws.region" $metadata )
identifier=$(jq -r ".aws.identifier[0] | keys | flatten[]" $metadata)
vpcID=$(aws ec2 describe-vpcs --region ${region} --filter "Name=tag:Name,Values=$infraID-vpc" --query "Vpcs[0].VpcId")
subnetID=$(aws ec2 describe-subnets --region ${region} --filter "Name=vpc-id,Values=$vpcID" --filter "Name=tag:Name,Values=$infraID-public-*" --query "Subnets[1].SubnetId")
imageID=`aws ec2 describe-instances --region ${region} --filters "Name=tag:Name,Values=*$infraID*" --query 'Reservations[1].Instances[*].[ImageId]' --output text`
Function that can be used for quick ec2 describe* commands
function awsocp {
metadata=~/.metadata.json
region=$(jq -r ".aws.region" $metadata)
source <(jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" $metadata)
aws ec2 describe-${@} --region ${region} --filters "Name=tag:Name,Values=$infraID-*"
}
Using bashrc function from above
awsocp volumes
awsocp subnets
awsocp images
awsocp route-tables
awsocp security-groups
# VPC
aws ec2 describe-vpcs --region ${region} --filter "Name=tag:Name,Values=$infraID-*"
aws ec2 describe-vpcs --region ${region} --filter "Name=tag:Name,Values=$infraID-*" --query "Vpcs[0].VpcId"
# Elastic IPs
aws ec2 describe-addresses --region ${region} --filters "Name=tag-value,Values=$infraID-*"
# Instances
aws ec2 describe-instances --region ${region} --filters "Name=tag:Name,Values=*$infraID-*"
aws ec2 describe-instances --region ${region} --filters "Name=tag:Name,Values=$infraID-*" --query "Reservations[*].Instances[*].[InstanceId]" --output text
aws ec2 describe-instances --region ${region} --filters Name=vpc-id,Values=$vpcID --query 'Reservations[*].Instances[*].{Id:InstanceId,Pub:PublicIpAddress,Pri:PrivateIpAddress,State:State.Name}' --output table
# All resources
aws resourcegroupstaggingapi get-resources --region ${region} --tag-filters "Key=kubernetes.io/cluster/$infraID"
# ELB
aws elb describe-load-balancers --region ${region} --query "LoadBalancerDescriptions[?VPCId==\`$vpcID\`]"
# Network interfaces
aws ec2 describe-network-interfaces --region ${region} --filters "Name=tag:Name,Values=$infraID-*" --query "NetworkInterfaces[*].NetworkInterfaceId"
# Volumes
aws ec2 describe-volumes --region ${region} --filters "Name=tag:Name,Values=$infraID-*" --query "Volumes[*].VolumeId" --output text
# AMI
aws iam --region ${region} list-users | jq -r --arg infraID "$infraID" '.Users[] | select(.UserName | startswith($infraID))'
aws iam --region ${region} get-user --user-name <NAME>
# Route53
aws route53 --region ${region} list-hosted-zones | jq --arg clusterName "$clusterName" '.HostedZones[] | select(.Name | startswith($clusterName))'
aws route53 --region ${region} list-resource-record-sets --hosted-zone-id `aws route53 --region ${region} list-hosted-zones | jq -r --arg clusterName "$clusterName" '.HostedZones[] | select(.Name | startswith($clusterName)) | .Id'`