2018-11-19 18:04:54 +00:00
|
|
|
#
|
|
|
|
# Defines aws cli aliases/functions.
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Daniel Carrillo
|
|
|
|
#
|
|
|
|
|
|
|
|
# Get the output parameter
|
|
|
|
zstyle -s ':prezto:module:aws' output '_aws_output' || _aws_output='table'
|
2020-02-20 17:43:05 +00:00
|
|
|
zstyle -s ':prezto:module:aws' show_region '_aws_region' || _aws_region='false'
|
2018-11-19 18:04:54 +00:00
|
|
|
zstyle -s ':prezto:module:aws' profile '_aws_profile' || _aws_profile='default'
|
|
|
|
|
|
|
|
# Return if requirements are not found.
|
|
|
|
if (( ! $+commands[aws] )); then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
function _get_aws_profile {
|
|
|
|
if [[ -z "${AWS_PROFILE+1}" ]]; then
|
|
|
|
echo ${_aws_profile}
|
|
|
|
else
|
|
|
|
echo $AWS_PROFILE
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function aws_i {
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
aws ec2 describe-instances --profile $profile --output ${_aws_output} \
|
|
|
|
--query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value, LaunchTime, State.Name,
|
|
|
|
InstanceType, VpcId, InstanceId, Placement.AvailabilityZone, PrivateIpAddress, PublicIpAddress]'
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:58:04 +00:00
|
|
|
function aws_ebs {
|
2018-11-20 16:19:28 +00:00
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
aws ec2 describe-volumes --profile $profile --output ${_aws_output} \
|
2018-12-13 19:13:12 +00:00
|
|
|
--query 'Volumes[].[Tags[?Key==`Name`] | [0].Value, VolumeId, Attachments[0].InstanceId, Size, AvailabilityZone]'
|
2018-11-20 14:58:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 18:04:54 +00:00
|
|
|
function aws_elb {
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
aws elb describe-load-balancers --profile $profile --output ${_aws_output} \
|
2019-11-06 18:02:45 +00:00
|
|
|
--query "LoadBalancerDescriptions[*].{type:'elb',scheme:Scheme,dns:DNSName,vpc:VPCId,name:LoadBalancerName,subnets:Subnets[*] | join(',', @)}"
|
2018-11-19 18:04:54 +00:00
|
|
|
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
aws elbv2 describe-load-balancers --profile $profile --output ${_aws_output} \
|
2019-11-06 18:02:45 +00:00
|
|
|
--query "LoadBalancers[*].{type:Type,scheme:Scheme,dns:DNSName,vpc:VpcId,name:LoadBalancerName,subnets:AvailabilityZones[*].SubnetId | join(',', @)}"
|
2018-11-19 18:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function aws_userdata {
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
aws ec2 describe-instance-attribute --profile $profile --output text \
|
|
|
|
--attribute userData --instance-id $1 \
|
2019-11-04 10:05:59 +00:00
|
|
|
--query 'UserData.Value' | base64 -d
|
2018-11-19 18:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function aws_vpc {
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
aws ec2 describe-vpcs --profile $profile --output ${_aws_output} \
|
|
|
|
--query 'Vpcs[*].{id:VpcId,cidr:CidrBlock,tag:Tags[0].Value}'
|
|
|
|
}
|
|
|
|
|
2020-02-24 20:11:09 +00:00
|
|
|
function aws_subnets {
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
aws ec2 describe-subnets --profile $profile --output text \
|
|
|
|
--query 'Subnets[].[Tags[?Key==`Name`] | [0].Value, SubnetId, VpcId, CidrBlock]' | sort -k1
|
|
|
|
}
|
|
|
|
|
2018-11-19 18:04:54 +00:00
|
|
|
function aws_ag {
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
aws autoscaling describe-auto-scaling-groups --profile $profile --output ${_aws_output} \
|
|
|
|
--query 'AutoScalingGroups[*].{name:AutoScalingGroupName,az:VPCZoneIdentifier}'
|
|
|
|
}
|
|
|
|
|
|
|
|
function aws_ami {
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
aws ec2 describe-images --profile $profile --output ${_aws_output} \
|
2019-12-01 17:31:29 +00:00
|
|
|
--owner self --query 'Images[*].{date:CreationDate,id:ImageId,name:Name,virt:VirtualizationType,st:State}'
|
2018-11-19 18:04:54 +00:00
|
|
|
}
|
2019-10-24 12:56:20 +00:00
|
|
|
|
|
|
|
function aws_kms_decrypt {
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
|
|
|
|
if [[ -z $1 ]]; then
|
|
|
|
echo "String is missing"
|
|
|
|
return 1
|
|
|
|
fi
|
2019-11-04 10:05:59 +00:00
|
|
|
aws kms --profile $profile decrypt --ciphertext-blob fileb://<(base64 -d <<<$1) \
|
2019-11-05 10:40:15 +00:00
|
|
|
--output text --query Plaintext | base64 -d
|
2019-10-24 12:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function aws_ssm_session {
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
|
|
|
|
if [[ -z $1 ]]; then
|
2019-11-04 10:05:59 +00:00
|
|
|
echo "Instance id is missing"
|
2019-10-24 12:56:20 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2019-11-04 10:05:59 +00:00
|
|
|
aws ssm start-session --profile $profile --target $1
|
2019-10-24 12:56:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 10:05:59 +00:00
|
|
|
function aws_ssm_session_any {
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
local id
|
|
|
|
|
|
|
|
if [[ -z $1 ]]; then
|
|
|
|
echo "Instance name is missing"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
id=$(aws ec2 describe-instances --profile $profile --output text \
|
2019-11-08 18:44:43 +00:00
|
|
|
--filter "Name=tag:Name,Values=$1" "Name=instance-state-name,Values=running" \
|
2019-11-04 10:05:59 +00:00
|
|
|
--query 'Reservations[0].Instances[0].InstanceId')
|
2019-11-05 11:05:16 +00:00
|
|
|
if [[ $2 == "ssh" ]]; then
|
|
|
|
AWS_PROFILE=$profile ssh $id
|
|
|
|
else
|
|
|
|
aws ssm start-session --profile $profile --target $id
|
|
|
|
fi
|
|
|
|
}
|
2019-11-04 10:05:59 +00:00
|
|
|
|
2020-02-06 18:48:22 +00:00
|
|
|
function aws_cf {
|
|
|
|
local profile=$(_get_aws_profile)
|
|
|
|
|
2020-02-22 15:12:23 +00:00
|
|
|
if ! type "jq" > /dev/null; then
|
|
|
|
echo "ERROR: this function needs jq to be installed"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
aws cloudfront list-distributions --profile $profile --output json \
|
2020-02-06 18:48:22 +00:00
|
|
|
--query "DistributionList.Items[*].{id:Id,domain:DomainName,status:Status,
|
2020-02-22 15:12:23 +00:00
|
|
|
origin:Origins.Items[].DomainName | join(' ', @), aliases:Aliases.Items | join(' ', @)}" \
|
|
|
|
| jq -r ".[] | [.id, .domain, .aliases, .status, .origin] | @csv" | tr -d '"' | column --separator="," --table
|
2020-02-06 18:48:22 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 11:05:16 +00:00
|
|
|
# ~/.ssh/config
|
|
|
|
#
|
|
|
|
# Host i-*
|
|
|
|
# ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
|
|
|
|
# User <your_user>
|
|
|
|
function ssh_aws_any {
|
|
|
|
aws_ssm_session_any $1 ssh
|
2019-11-04 10:05:59 +00:00
|
|
|
}
|
2020-02-22 15:11:38 +00:00
|
|
|
|
|
|
|
function aws_switch_profile {
|
2020-02-22 18:41:14 +00:00
|
|
|
local region
|
2020-02-22 15:11:38 +00:00
|
|
|
|
2020-02-22 19:04:40 +00:00
|
|
|
if [[ -z $1 ]]; then
|
|
|
|
echo "Profile can't be an empty string"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Activating profile $1..."
|
2020-02-22 18:41:14 +00:00
|
|
|
export AWS_PROFILE=$1
|
2020-02-22 15:11:38 +00:00
|
|
|
if [[ -f ~/.aws/credentials ]]; then #&& (( ! ${+AWS_DEFAULT_REGION} ))
|
2020-02-22 18:41:14 +00:00
|
|
|
region=$(aws configure get region)
|
|
|
|
if [[ ! -z $region ]]; then
|
|
|
|
export AWS_DEFAULT_REGION=$region
|
|
|
|
else
|
|
|
|
unset AWS_DEFAULT_REGION
|
|
|
|
fi
|
2020-02-22 15:11:38 +00:00
|
|
|
fi
|
|
|
|
}
|
2020-02-22 19:04:40 +00:00
|
|
|
|
|
|
|
function aws_deactivate_profile {
|
|
|
|
echo "Deactivating aws profile..."
|
|
|
|
unset AWS_PROFILE
|
|
|
|
unset AWS_DEFAULT_REGION
|
|
|
|
}
|