forked from awslabs/awscli-aliases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalias
158 lines (132 loc) · 4.71 KB
/
alias
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
[toplevel]
whoami = sts get-caller-identity
ssh =
!f() {
aws ssm start-session --target "${1}"
}; f
create-assume-role =
!f() {
aws iam create-role --role-name "${1}" \
--assume-role-policy-document \
"{\"Statement\":[{\
\"Action\":\"sts:AssumeRole\",\
\"Effect\":\"Allow\",\
\"Principal\":{\"Service\":\""${2}".amazonaws.com\"},\
\"Sid\":\"\"\
}],\
\"Version\":\"2012-10-17\"\
}";
}; f
running-instances = ec2 describe-instances \
--filter Name=instance-state-name,Values=running \
--output table \
--query 'Reservations[].Instances[].{ID: InstanceId,Hostname: PublicDnsName,Name: Tags[?Key==`Name`].Value | [0],Type: InstanceType, Platform: Platform || `Linux`}'
ebs-volumes= ec2 describe-volumes \
--query 'Volumes[].{VolumeId: VolumeId,State: State,Size: Size,Name: Tags[0].Value,AZ: AvailabilityZone}' \
--output table
amazon-linux-amis = ec2 describe-images \
--filter \
Name=owner-alias,Values=amazon \
Name=name,Values="amzn-ami-hvm-*" \
Name=architecture,Values=x86_64 \
Name=virtualization-type,Values=hvm \
Name=root-device-type,Values=ebs \
Name=block-device-mapping.volume-type,Values=gp2 \
--query "reverse(sort_by(Images, &CreationDate))[*].[ImageId,Name,Description]" \
--output text
list-sgs = ec2 describe-security-groups --query "SecurityGroups[].[GroupId, GroupName]" --output text
sg-rules = !f() { aws ec2 describe-security-groups \
--query "SecurityGroups[].IpPermissions[].[FromPort,ToPort,IpProtocol,join(',',IpRanges[].CidrIp)]" \
--group-id "$1" --output text; }; f
tostring =
!f() {
jp -f "${1}" 'to_string(@)'
}; f
tostring-with-jq =
!f() {
cat "${1}" | jq 'tostring'
}; f
authorize-my-ip =
!f() {
ip=$(aws myip)
aws ec2 authorize-security-group-ingress --group-id ${1} --cidr $ip/32 --protocol tcp --port 22
}; f
get-group-id =
!f() {
aws ec2 describe-security-groups --filters Name=group-name,Values=${1} --query SecurityGroups[0].GroupId --output text
}; f
authorize-my-ip-by-name =
!f() {
group_id=$(aws get-group-id "${1}")
aws authorize-my-ip "$group_id"
}; f
# list all security group port ranges open to 0.0.0.0/0
public-ports = ec2 describe-security-groups \
--filters Name=ip-permission.cidr,Values=0.0.0.0/0 \
--query 'SecurityGroups[].{
GroupName:GroupName,
GroupId:GroupId,
PortRanges:
IpPermissions[?contains(IpRanges[].CidrIp, `0.0.0.0/0`)].[
join(`:`, [IpProtocol, join(`-`, [to_string(FromPort), to_string(ToPort)])])
][]
}'
# List or set your region
region = !f() { [[ $# -eq 1 ]] && aws configure set region "$1" || aws configure get region; }; f
find-access-key = !f() {
clear_to_eol=$(tput el)
for i in $(aws iam list-users --query "Users[].UserName" --output text); do
printf "\r%sSearching...$i" "${clear_to_eol}"
result=$(aws iam list-access-keys --output text --user-name "${i}" --query "AccessKeyMetadata[?AccessKeyId=='${1}'].UserName";)
if [ -n "${result}" ]; then
printf "\r%s%s is owned by %s.\n" "${lear_to_eol}" "$1" "${result}"
break
fi
done
if [ -z "${result}" ]; then
printf "\r%sKey not found." "${clear_to_eol}"
fi
}; f
docker-ecr-login =
!f() {
region=$(aws configure get region)
endpoint=$(aws ecr get-authorization-token --region $region --output text --query authorizationData[].proxyEndpoint)
passwd=$(aws ecr get-authorization-token --region $region --output text --query authorizationData[].authorizationToken | base64 --decode | cut -d: -f2)
docker login -u AWS -p $passwd $endpoint
}; f
myip =
!f() {
dig +short myip.opendns.com @resolver1.opendns.com
}; f
allow-my-ip =
!f() {
my_ip=$(aws myip)
aws ec2 authorize-security-group-ingress --group-name ${1} --protocol ${2} --port ${3} --cidr $my_ip/32
}; f
revoke-my-ip =
!f() {
my_ip=$(aws myip)
aws ec2 revoke-security-group-ingress --group-name ${1} --protocol ${2} --port ${3} --cidr $my_ip/32
}; f
allow-my-ip-all =
!f() {
aws allow-my-ip ${1} all all
}; f
revoke-my-ip-all =
!f() {
aws revoke-my-ip ${1} all all
}; f
ecr-retag =
!f() {
manifest=$(aws ecr batch-get-image --repository-name ${1} --image-ids imageTag=${2} --query "images[].imageManifest" --output text)
aws ecr put-image --repository-name ${1} --image-tag ${3} --image-manifest "$manifest"
}; f
delete-ami =
!f() {
snapshots=($(aws ec2 describe-images --image-ids $1 --query "Images[].BlockDeviceMappings[].Ebs.SnapshotId" --output text))
aws ec2 deregister-image --image-id $1
for id in $snapshots
do
aws ec2 delete-snapshot --snapshot-id ${id}
done
}; f