-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.mx
129 lines (99 loc) · 3.17 KB
/
aws.mx
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
function aws {
# wrapper around AWS CLI to set JSON output and data type and read from profile
cast json
exec aws --output json --no-paginate --no-cli-pager --color auto @PARAMS
/#config: set proc strict-vars false
if { $ENV.AWS_PROFILE == "" } then {
exec aws --output json --no-paginate --no-cli-pager --color auto @PARAMS
} else {
exec aws --output json --profile $ENV.AWS_PROFILE --no-paginate --no-cli-pager --color auto @PARAMS
}#/
}
function aws-list-profiles {
# list all AWS profiles stored in ~/.aws/config
cast json
cat ~/.aws/config -> :str: regexp 'f,\[profile (.*?)],' -> format json
}
function aws-read-config {
# reads ~/.aws/config and returns that the profiles as YAML
cast yaml
profile = ""
cat ~/.aws/config -> :str: foreach line {
switch {
case { $line =~ '\[profile .*?\]' } {
$line -> regexp 'f,\[profile (.*?)],' -> set profile
out "$(profile):"
}
case { $line =~ '\[.*?\]' } {
profile = ""
}
default {
if { $profile == "" || $line !~ "=" } then { continue switch }
$line -> regexp 'f/\s*(.*?)\s*=\s*(.*?)\s*$/' -> format json -> set kv
out %( $(kv.0): "$(kv.1)")
}
}
}
}
/#function aws-acc-name {
}#/
function aws-switch-profile {
read --prompt "Please select which AWS profile: " \
--variable ENV.AWS_PROFILE \
--autocomplete ${aws-list-profiles}
}
function foreach-aws-profile {
aws-list-profiles -> foreach ENV.AWS_PROFILE $1
}
autocomplete set aws-global ${autocomplete get aws}
function aws-global {
# behaves like `aws` except runs against all AWS profiles
cast json
merged = %{}
aws-list-profiles -> foreach ENV.AWS_PROFILE {
merged <~ ${ aws @PARAMS }
}
$merged
}
function ec2-describe-instances {
aws ec2 describe-instances -> [Reservations] -> foreach --jmap res {
$res.Instances.0.NetworkInterfaces.0.PrivateIpAddress
} {
printf <!null> %(${@res.Instances.0.tags[{ $.Key=="Name" }] -> [[.0.Value]] || $res.Instances.0.InstanceId} on $AWS_PROFILE (${$res.Instances.0.State.Name}))
}
}
function ec2-tags {
cast json
<stdin> -> :json: foreach kv { out: "$kv.Key: $kv.Value"} -> sort -> :yaml: format json
}
function s3-du (
S3_BUCKET: str "Name of S3 bucket (and optional path): s3://"
) {
cast str
aws s3 ls $S3_BUCKET \
-> :str: grep PRE \
-> [:1] \
-> !regexp 'm,^/$,' \
-> foreach prefix {
out "\n$(prefix)"
aws s3 ls --human-readable --recursive --summarize s3://$(S3_BUCKET)/$prefix \
-> grep Total
}
}
source aws_ssh.mx
function aws-login {
try {
aws sso login
update-aws-hosts
}
}
function vm (
lookup: str "Lookup value for AWS host"
) {
host = @AWS_HOSTS[{$.val =~ $lookup}]
switch ${$host|count} {
case 0 { err "no hosts found" }
case 1 { get-type \$host }
default { err "expecting 1 host. Instead got ${$host|count}" }
}
}