Skip to content

Commit af2bbf6

Browse files
authored
Merge pull request #6 from lamhaison/develop
LGTM
2 parents f64c4fc + 30cd72f commit af2bbf6

File tree

15 files changed

+248
-135
lines changed

15 files changed

+248
-135
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ This is the project to collect helpful aws cli commandline with complex options
88
## Setup dependencies
99
Notes: This document is for macos environment.
1010

11-
## Setup aws-cli
11+
### Setup aws-cli
1212
How to install aws cli - https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
1313
```
1414
aws --version
1515
aws-cli/2.8.1 Python/3.9.11 Darwin/22.1.0 exe/x86_64 prompt/off
1616
```
1717

18+
### Install jq
19+
```
20+
brew install jq
21+
jq --version
22+
jq-1.6
23+
```
24+
1825
### Install peco
1926
To allow searching by console.
2027
![image](./images/aws_help.png)
@@ -40,8 +47,8 @@ echo "random_string" > ~/.password_assume_role_encrypted
4047

4148
## Settings when open terminal (I am using iterm)
4249
```
43-
mkdir -p /opt/lamhaison-tools
44-
git clone git@github.com:lamhaison/aws-cli-utils.git
50+
mkdir -p /opt/lamhaison-tools && cd /opt/lamhaison-tools
51+
git clone https://github.com/lamhaison/helpful-commandlines.git
4552
echo "source /opt/lamhaison-tools/aws-cli-utils/main.sh" >> ~/.bashrc
4653
```
4754

@@ -71,10 +78,14 @@ mfa_serial = arn:aws:iam::ACCOUNT_NAME_MFA:mfa/ACCOUNT_NAME
7178

7279

7380
## How to use
81+
### AssumeRole
82+
```
83+
Ctrl + @ and press enter and choose the assume role that you want to use.
84+
```
7485
### List all functions
7586
```
7687
aws_help
77-
You can search list function and after that you can run which funtion_name to get the detail of bashshell code
88+
You can search list function and after that you can run which funtion_name to get the detail of bashshell code.
7889
```
7990
### Other
8091
```

common/common.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ aws_run_commandline_with_logging() {
8080
local detail_commandline_tee_command="${tee_command} > /dev/null"
8181
fi
8282

83-
aws_commandline_result=$(aws_run_commandline_with_retry "${aws_commandline}" "${ignored_error_when_retry}")
84-
8583
echo "------------------------------STARTED--$(date '+%Y-%m-%d-%H-%M-%S')-----------------------------------------" | eval $tee_command >/dev/null
8684
echo "Running commandline [ ${aws_commandline_logging} ]" | eval $detail_commandline_tee_command
85+
aws_commandline_result=$(aws_run_commandline_with_retry "${aws_commandline}" "${ignored_error_when_retry}")
8786
echo $aws_commandline_result | eval $tee_command
8887
echo "------------------------------FINISHED-$(date '+%Y-%m-%d-%H-%M-%S')-----------------------------------------" | eval $tee_command >/dev/null
8988
}

common/help_ssh.sh

Lines changed: 0 additions & 25 deletions
This file was deleted.

common/peco.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ peco_commandline_input() {
4040

4141
local commandline="${1}"
4242
local result_cached=$2
43+
local input_expired_time="${3:=$peco_input_expired_time}"
4344

4445
local md5_hash=$(echo $commandline | md5)
45-
local input_folder=${aws_cli_input_tmp}/${ASSUME_ROLE}
46+
local input_folder="${aws_cli_input_tmp}/${ASSUME_ROLE:=NOTSET}"
4647
mkdir -p ${input_folder}
4748
local input_file_path="${input_folder}/${md5_hash}.txt"
4849
local empty_file=$(find ${input_folder} -name ${md5_hash}.txt -empty)
49-
local valid_file=$(find ${input_folder} -name ${md5_hash}.txt -mmin +${peco_input_expired_time})
50+
local valid_file=$(find ${input_folder} -name ${md5_hash}.txt -mmin +${input_expired_time})
5051

5152
# The file is existed and not empty and the flag result_cached is not empty
5253
if [ -z "${valid_file}" ] && [ -f "${input_file_path}" ] && [ -z "${empty_file}" ] && [ -n "${result_cached}" ]; then
@@ -116,6 +117,18 @@ peco_aws_list_db_parameter_groups() {
116117
peco_aws_input 'aws rds describe-db-parameter-groups --query "*[].DBParameterGroupName"' 'true'
117118
}
118119

120+
peco_aws_rds_list_db_cluster_snapshots() {
121+
peco_aws_input 'aws rds describe-db-cluster-snapshots \
122+
--snapshot-type manual \
123+
--query "DBClusterSnapshots[].DBClusterSnapshotIdentifier"'
124+
}
125+
126+
peco_aws_rds_list_db_snapshots() {
127+
peco_aws_input 'aws rds describe-db-snapshots \
128+
--snapshot-type manual \
129+
--query "DBSnapshots[].DBSnapshotIdentifier"'
130+
}
131+
119132
peco_aws_list_db_cluster_parameter_groups() {
120133
peco_aws_input 'aws rds describe-db-cluster-parameter-groups --query "*[].DBClusterParameterGroupName"' 'true'
121134
}

main.sh

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export assume_role_password_encrypted="$(cat ~/.password_assume_role_encrypted)"
55
export tmp_credentials="/tmp/aws_temporary_credentials"
66
export aws_cli_results="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results"
77
export aws_cli_logs="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/logs"
8+
export aws_cli_images="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/images"
89
export aws_cli_input_tmp="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/inputs"
910
export aws_tmp_input="/tmp/aws_tmp_input_23647494949484.txt"
1011
export aws_assume_role_print_account_info="false"
@@ -33,15 +34,13 @@ export AWS_DEFAULT_OUTPUT="json"
3334
alias get-account-alias='aws iam list-account-aliases'
3435
alias get-account-id='echo AccountId $(aws sts get-caller-identity --query "Account" --output text)'
3536

36-
# Import sub-commandline.
37-
for module in $(echo "common services"); do
38-
for script in $(ls ${AWS_CLI_SOURCE_SCRIPTS}/${module}); do
39-
source ${AWS_CLI_SOURCE_SCRIPTS}/${module}/$script
40-
done
37+
# Import sub-commandlines.
38+
for script in $(find ${AWS_CLI_SOURCE_SCRIPTS} -type f -name '*.sh' | grep -v main.sh); do
39+
source $script
4140
done
4241

4342
# Add hot-keys
44-
zle -N aws_help
43+
# zle -N aws_help
4544
zle -N aws_main_function
4645
bindkey '^@' aws_main_function
47-
bindkey '^e' aws_help
46+
# bindkey '^e' aws_help

services/assume_role.sh

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import_tmp_credential() {
66
}
77

88
zip_tmp_credential() {
9-
cd $tmp_credentials
9+
cd $tmp_credentials >/dev/null
1010
echo "Encrypt temporary credential for assume-role ${ASSUME_ROLE} at ${tmp_credentials}/${ASSUME_ROLE}.zip"
1111

1212
if [ -f "${tmp_credentials}/${ASSUME_ROLE}.zip" ]; then
1313
rm -rf ${tmp_credentials}/${ASSUME_ROLE}.zip
1414
fi
1515

1616
zip -q -P $assume_role_password_encrypted $ASSUME_ROLE.zip $ASSUME_ROLE && rm -rf $ASSUME_ROLE
17-
cd -
17+
cd - >/dev/null
1818
}
1919

2020
aws_assume_role_reset() {
@@ -42,12 +42,12 @@ aws_assume_role_re_use_current() {
4242
}
4343

4444
aws_assume_role_unzip_tmp_credential() {
45-
cd $tmp_credentials
45+
cd $tmp_credentials >/dev/null
4646
assume_role_name=$1
4747
rm -rf ${assume_role_name}
4848
unzip -P $assume_role_password_encrypted ${assume_role_name}.zip
4949
echo "You credential is save here ${tmp_credentials}/${assume_role_name}"
50-
cd -
50+
cd - >/dev/null
5151
}
5252

5353
aws_assume_role_remove_tmp_credential() {
@@ -145,7 +145,7 @@ aws_assume_role_set_name() {
145145
# cd ${aws_cli_results}
146146

147147
if [ "${aws_assume_role_print_account_info}" = "true" ]; then
148-
aws_account_infos
148+
aws_account_info
149149
fi
150150
else
151151
echo "Please try again, the assume role action was not complete"
@@ -178,7 +178,7 @@ aws_assume_role_set_name_with_hint_peco() {
178178

179179
}
180180

181-
aws_account_infos() {
181+
aws_account_info() {
182182
get-account-alias
183183

184184
local aws_account_id=$(aws_run_commandline_with_retry 'aws sts get-caller-identity --query "Account" --output text' "true")
@@ -187,3 +187,11 @@ aws_account_infos() {
187187

188188
echo AWS Region ${AWS_REGION:?"The AWS_REGION is unset or empty"}
189189
}
190+
191+
aws_assume_role_get_tmp_credentials_for_new_members() {
192+
local tmp_credentials_file="${tmp_credentials}/${ASSUME_ROLE}"
193+
aws_assume_role_set_name_with_hint
194+
aws_assume_role_unzip_tmp_credential $assume_role
195+
cat ${tmp_credentials_file} && rm -rf ${tmp_credentials_file}
196+
197+
}

services/aws_events.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ aws_events_disable_rule() {
2323
set -x
2424
rule_name=$1
2525

26-
aws_account_infos
26+
aws_account_info
2727
echo "Disable rule ${rule_name}"
2828
aws events describe-rule --name $1
2929
aws events disable-rule --name $1

services/cloudfront.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ aws_cloudfront_invalidate_cache() {
4242
--paths '${aws_distribution_path}'
4343
"
4444

45+
echo "\
46+
How to get the status of the invalidate cloudfront request
47+
aws cloudfront get-invalidation --id \$aws_cloudfront_invalidation_id --distribution-id $aws_distribution_id
48+
"
49+
4550
}
4651

4752
aws_cloudfront_invalidate_cache_with_hint() {

services/cloudwatch.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
aws_cloudwatch_list_alarms() {
4+
aws_run_commandline "\
5+
aws cloudwatch describe-alarms
6+
"
7+
}
8+
9+
aws_cloudwatch_list_alb_arn() {
10+
aws_run_commandline "\
11+
aws elbv2 describe-load-balancers --query '*[].LoadBalancerArn'
12+
"
13+
}
14+
15+
aws_cloudwatch_get_graph() {
16+
local aws_cloudwatch_widget_image=$1
17+
local graph_file_path="${aws_cli_images}/${ASSUME_ROLE}"
18+
mkdir -p ${graph_file_path}
19+
local graph_file_name_full_path="$(mktemp ${graph_file_path}/${ASSUME_ROLE}-XXXXXXXXXXXXXX).png"
20+
aws cloudwatch get-metric-widget-image \
21+
--metric-widget ${aws_cloudwatch_widget_image} \
22+
--output-format png --output text | base64 --decode >${graph_file_name_full_path}
23+
24+
echo "Access the graph by the url ${graph_file_name_full_path}"
25+
}
26+
27+
aws_cloudwatch_list_dashboards() {
28+
aws_run_commandline "\
29+
aws cloudwatch list-dashboards \
30+
--query '*[].{DashboardName:DashboardName,LastModified:LastModified}'
31+
"
32+
}
33+
34+
# To return json for the dashboard. But the JSON is not valid.
35+
# TODO Later
36+
aws_cloudfront_get_dashboard() {
37+
aws_run_commandline "\
38+
aws cloudwatch get-dashboard \
39+
--dashboard-name ${1:?'aws_cloudwatch_dashboard_name is unset or empty'} \
40+
--query 'DashboardBody' --output text
41+
"
42+
}
43+
44+
aws_cloudfront_update_dashboard() {
45+
echo "Load dashboard data from file ${2}"
46+
local aws_coudwatch_dashboard_body=$(cat $2)
47+
aws cloudwatch put-dashboard \
48+
--dashboard-name ${1:?'aws_cloudwatch_dashboard_name is unset or empty'} \
49+
--dashboard-body ${aws_coudwatch_dashboard_body}
50+
51+
}

services/ec2.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ aws_subnet_list() {
110110
aws_ec2_list_subnets() {
111111
aws_run_commandline \
112112
"
113-
aws ec2 describe-subnets
113+
aws ec2 describe-subnets \
114+
--query '*[].{VpcId:VpcId,SubnetId:SubnetId,\
115+
AvailabilityZone:AvailabilityZone,Name:Tags[?Key==\`Name\`].Value | [0]}' --output table
114116
"
115117
}
116118

@@ -130,15 +132,15 @@ aws_sg_get() {
130132
"
131133
}
132134

133-
aws_sg_add_rule() {
135+
aws_sg_add_rule_instruction() {
134136
aws_sg_id=$1
135137

136138
echo "\
137139
# Allow access the ssh from a specific IP address
138140
aws ec2 authorize-security-group-ingress \
139-
--group-id ${aws_sg_id:?'aws_sg_id is unset or empty'} \
141+
--group-id ${aws_sg_id:="\$aws_sg_id"} \
140142
--protocol tcp --port 22 \
141-
--cidr $(lamhaison_get_public_ip)/32
143+
--cidr $(dig +short myip.opendns.com @resolver1.opendns.com)/32
142144
"
143145
}
144146

0 commit comments

Comments
 (0)