Created
November 12, 2019 07:17
-
-
Save zxkane/e1d32abc182fc66c3079d14ccdea42ca to your computer and use it in GitHub Desktop.
aws shortcuts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# aws ec2 alias or quick functions | |
alias ec2-instance-list="aws ec2 describe-instances --output table --query 'Reservations[*].Instances[*].{Instance:InstanceId,Type:InstanceType,AZ:Placement.AvailabilityZone,Name:Tags[?Key==\`Name\`]|[0].Value,Dns:NetworkInterfaces[0].Association.PublicDnsName,State:State.Name}' --filters 'Name=instance-state-code,Values=16'" | |
alias ec2-terminate-instance="aws ec2 terminate-instances --instance-ids" | |
function _ec2-launch { | |
templateName='my-instances-launch-template' | |
amiid="$1" | |
if [ -z "$amiid" ]; then | |
echo 'AMI is missing' | |
return | |
fi | |
region="$2" | |
regionopts=() | |
otheropts=() | |
if [ ! -z "$region" ] && [[ ! "$region" =~ ^--.* ]]; then | |
regionopts=(--region $region) | |
otheropts=${@:3} | |
else | |
otheropts=${@:2} | |
fi | |
#echo "amiid: $amiid, regionopts: $regionopts, otheropts: $otheropts" | |
keyname=$(aws ec2 describe-key-pairs --output text --query 'KeyPairs[0].KeyName' ${regionopts[@]}) | |
vpcid=$(aws ec2 describe-vpcs --filters 'Name=isDefault,Values=true' --out text --query 'Vpcs[0].VpcId' ${regionopts[@]}) | |
sgids=$(aws ec2 describe-security-groups --filters "Name=vpc-id,Values=$vpcid" --query 'SecurityGroups[-5:].GroupId' --output text ${regionopts[@]}) | |
templateVersion=$(aws ec2 describe-launch-templates --launch-template-name $templateName --query 'LaunchTemplates[0].LatestVersionNumber' --output text ${regionopts[@]}) | |
#echo "keyname: $keyname, vpcid: $vpcid, sgids: $sgids, templateVersion: $templateVersion" | |
#cmd="aws ec2 run-instances --launch-template LaunchTemplateName=$templateName,Version=$templateVersion --image-id $amiid --associate-public-ip-address --key-name $keyname --security-group-ids $sgids --query 'Instances[*].{Instance:InstanceId,AZ:Placement.AvailabilityZone,Dns:NetworkInterfaces[0].Association.PublicDnsName,State:State.Name}' --output table ${regionopts[@]} ${otheropts[@]}" | |
echo "run command: $cmd" | |
eval $cmd | |
} | |
function ec2-launch-amazon-linux { | |
region="$1" | |
regionopts=() | |
if [ ! -z "$region" ] && [[ ! "$region" =~ ^--.* ]]; then | |
regionopts=(--region $region) | |
fi | |
#echo "regionopts: $regionopts" | |
amiid=$(aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --query 'Parameters[0].[Value]' --output text ${regionopts[@]}) | |
#echo "_ec2-launch $amiid $@" | |
_ec2-launch $amiid $@ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment