Skip to content

Commit 8d1bd4a

Browse files
authored
Merge pull request #1 from lamhaison/develop
upgrade feature
2 parents be4bbd8 + 8720b71 commit 8d1bd4a

29 files changed

+707
-123
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
aws_cli_results
2+
.DS_Store

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,44 @@ This is the project to collect helpful aws cli commandline with complex options
55
- Sometimes, you want to add more options on purpose and you want to have a place to collect and reuse it in the future. That is the reason I defined that structure to help me collect helpful commandlines and share on the github.
66

77

8-
98
## Setup dependencies
9+
Notes: This document is for macos environment.
10+
11+
## Setup aws-cli
12+
How to install aws cli - https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
13+
```
14+
aws --version
15+
aws-cli/2.8.1 Python/3.9.11 Darwin/22.1.0 exe/x86_64 prompt/off
16+
```
17+
18+
### Install peco
19+
To allow searching by console.
20+
![image](./images/aws_help.png)
21+
1022
```
11-
brew install remind101/formulae/assume-role
1223
brew install peco
24+
peco --version
25+
peco version v0.5.10 (built with go1.19.2)
26+
```
1327

28+
### Install assume-role
29+
To allow assume role to a target account
30+
```
31+
brew install remind101/formulae/assume-role
32+
```
33+
34+
### Setup your password for compress the plantext your temporary credentail.
35+
```
1436
# This password is used to encrypt your tmp credential.
1537
echo "random_string" > ~/.password_assume_role_encrypted
1638
```
1739

1840

19-
## Settings when open terminal
41+
## Settings when open terminal (I am using iterm)
2042
```
2143
echo "source /opt/lamhaison-tools/aws-cli-utils/main.sh" >> ~/.bashrc
2244
```
2345

24-
2546
## Setting on ~/.aws/config
2647

2748
```

common/common.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
aws_run_commandline() {
2+
aws_commandline=$1
3+
log_file_path=${aws_cli_logs}/${ASSUME_ROLE}.log
4+
if [ "$aws_show_commandline" = "true" ]; then
5+
output="tee -a ${log_file_path}"
6+
else
7+
output=">> ${log_file_path}"
8+
fi
9+
10+
echo "-------------------------------------START--$(date '+%Y-%m-%d-%H-%M-%S')------------------------------------------------" >>${log_file_path}
11+
echo Running commandline \[ ${aws_commandline:?"Commandline is unset or empty"}\ ] | eval $output
12+
eval $aws_commandline | tee -a ${aws_cli_logs}/${ASSUME_ROLE}.log
13+
echo "-------------------------------------FINISH-$(date '+%Y-%m-%d-%H-%M-%S')------------------------------------------------" >>${log_file_path}
14+
}

common/hep_aws_rds.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
aws_help_install_mysql_client_5_7() {
2+
3+
echo \
4+
"
5+
sudo rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
6+
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
7+
yum install -y mysql-community-client
8+
"
9+
}
10+
11+
aws_help_rds_list_bin_logs() {
12+
echo \
13+
"show binary logs;"
14+
}
15+
16+
aws_help_start_stop_replication() {
17+
echo "CALL mysql.rds_start_replication;"
18+
echo "CALL mysql.rds_stop_replication;"
19+
}
20+
21+
aws_help_rds_aurora_set_replication() {
22+
echo \
23+
"
24+
aws_rds_list_events
25+
CALL mysql.rds_set_external_master ('master_address', 3306, \
26+
'repl_user', '123456', 'mysql-bin-changelog.000002', 1234, 0);
27+
"
28+
}
29+
30+
aws_help_rds_aurora_test_replication() {
31+
32+
echo \
33+
"
34+
35+
# On master
36+
CREATE TABLE IF NOT EXISTS tasks (
37+
task_id INT AUTO_INCREMENT PRIMARY KEY
38+
) ENGINE=INNODB;
39+
40+
DB_USER=xxx
41+
DB_PASSWD=xxx
42+
DB_NAME=xxx
43+
DB_ADDRESS=xxx
44+
mysql --user=$DB_USER --password=$DB_PASSWD $DB_NAME -h $DB_ADDRESS << EOF
45+
INSERT INTO tasks (\`task_id\`) VALUES (NULL);
46+
EOF
47+
48+
# On slave
49+
select * from tasks;
50+
"
51+
}

common/peco.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# brew install peco
2+
# PECO
3+
4+
peco_assume_role_name() {
5+
cat ~/.aws/config | grep -e "^\[profile.*\]$" | peco
6+
}
7+
8+
peco_format_aws_output_text() {
9+
peco_input=$1
10+
echo "${peco_input}" | tr "\t" "\n"
11+
}
12+
13+
peco_aws_acm_list() {
14+
aws_acm_list | peco
15+
}
16+
17+
peco_aws_input() {
18+
aws_cli_commandline="${1} --output text"
19+
result_cached=$2
20+
21+
md5_hash=$(echo $aws_cli_commandline | md5)
22+
input_folder=${aws_cli_input_tmp}/${ASSUME_ROLE}
23+
mkdir -p ${input_folder}
24+
input_file_path="${input_folder}/${md5_hash}.txt"
25+
empty_file=$(find ${input_folder} -name ${md5_hash}.txt -empty)
26+
27+
# The file is existed and not empty and the flag result_cached is not empty
28+
if [ -f "${input_file_path}" ] && [ -z "${empty_file}" ] && [ -n "${result_cached}" ]; then
29+
# Ignore the first line.
30+
grep -Ev "\*\*\*\*\*\*\*\* \[.*\]" $input_file_path
31+
else
32+
aws_result=$(eval $aws_cli_commandline)
33+
format_text=$(peco_format_aws_output_text $aws_result)
34+
35+
if [ -n "${format_text}" ]; then
36+
echo "******** [ ${aws_cli_commandline} ] ********" >${input_file_path}
37+
echo ${format_text} | tee -a ${input_file_path}
38+
else
39+
echo "Can not get the data"
40+
fi
41+
42+
fi
43+
}
44+
45+
# AWS Logs
46+
peco_aws_logs_list() {
47+
peco_aws_input 'aws logs describe-log-groups --query "*[].logGroupName"' 'true'
48+
}
49+
50+
# AWS ECS
51+
peco_aws_ecs_list_clusters() {
52+
peco_aws_input 'aws ecs list-clusters --query "*[]"' 'true'
53+
}
54+
55+
peco_aws_ecs_list_services() {
56+
peco_aws_input 'aws ecs list-services --cluster $aws_ecs_cluster_arn --query "*[]"'
57+
}
58+
59+
# AWS ECR
60+
61+
peco_aws_list_repositorie_names() {
62+
peco_aws_input 'aws ecr describe-repositories --query "*[].repositoryName"' 'true'
63+
}
64+
65+
# AWS RDS
66+
peco_aws_list_db_parameter_groups() {
67+
peco_aws_input 'aws rds describe-db-parameter-groups --query "*[].DBParameterGroupName"' 'true'
68+
}
69+
70+
peco_aws_list_db_cluster_parameter_groups() {
71+
peco_aws_input 'aws rds describe-db-cluster-parameter-groups --query "*[].DBClusterParameterGroupName"' 'true'
72+
}
73+
74+
peco_aws_list_db_clusters() {
75+
peco_aws_input 'aws rds describe-db-clusters --query "*[].DBClusterIdentifier"' 'true'
76+
}
77+
78+
peco_aws_list_db_instances() {
79+
peco_aws_input 'aws rds describe-db-instances --query "*[].DBInstanceIdentifier"' 'true'
80+
}
81+
82+
# Lambda
83+
peco_aws_lambda_list() {
84+
peco_aws_input 'aws lambda list-functions --query "*[].FunctionName"' 'true'
85+
}
86+
87+
# S3
88+
peco_aws_s3_list() {
89+
peco_aws_input 'aws s3api list-buckets --query "Buckets[].Name"' 'true'
90+
}
91+
92+
# Codebuild
93+
peco_aws_codebuild_list() {
94+
peco_aws_input 'aws codebuild list-projects --query "*[]"' 'true'
95+
}

images/aws_help.png

497 KB
Loading

main.sh

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1+
# Get the current directory of the main.sh script.
2+
export AWS_CLI_SOURCE_SCRIPTS="$(dirname -- "$0")"
3+
14
export assume_role_password_encrypted="$(cat ~/.password_assume_role_encrypted)"
25
export tmp_credentials="/tmp/aws_temporary_credentials"
3-
export aws_cli_results="$(echo ~/aws_cli_results)"
4-
export aws_cli_history="${aws_cli_results}/history.json"
6+
export aws_cli_results="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results"
7+
export aws_cli_logs="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/logs"
8+
export aws_cli_input_tmp="${AWS_CLI_SOURCE_SCRIPTS}/aws_cli_results/inputs"
9+
export aws_tmp_input="/tmp/aws_tmp_input_23647494949484.txt"
10+
# max session 1h
11+
export aws_assume_role_duration=3600s
512
export aws_assume_role_expired_time=55
6-
7-
# Get the current directory of the main.sh script.
8-
export AWS_CLI_SOURCE_SCRIPTS="$(dirname -- "$0")"
13+
# To allow log detail of the aws cli [true|false]
14+
export aws_show_commandline=true
15+
export aws_log_tail_since=120m
916

1017
mkdir -p ${tmp_credentials}
1118
mkdir -p ${aws_cli_results}
19+
mkdir -p ${aws_cli_logs}
20+
mkdir -p ${aws_cli_input_tmp}
1221

1322
# add some help aliases
1423
alias get-account-alias='aws iam list-account-aliases'
1524
alias get-account-id='echo AccountId $(aws sts get-caller-identity --query "Account" --output text)'
16-
alias aws-cli-save-commandline-to-history='history -1 >> ${aws_cli_history}'
17-
alias aws-cli-save-all-commandlines-to-history='history |grep aws | grep -v history >> ${aws_cli_history}'
18-
# alias assume-role='function(){eval $(command assume-role --duration 1h $@);}'
1925

2026
# Import sub-commandline.
21-
for script in $(ls ${AWS_CLI_SOURCE_SCRIPTS}/services); do
22-
source ${AWS_CLI_SOURCE_SCRIPTS}/services/$script
27+
28+
for module in $(echo "common services"); do
29+
for script in $(ls ${AWS_CLI_SOURCE_SCRIPTS}/${module}); do
30+
source ${AWS_CLI_SOURCE_SCRIPTS}/${module}/$script
31+
done
2332
done

services/acm.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
aws_acm_list() {
55
for item in $(aws acm list-certificates --query "*[].CertificateArn" --output text); do
6-
aws acm describe-certificate --certificate-arn $item --query "*[].{CertificateArn:CertificateArn,DomainName:DomainName,SubjectAlternativeNames:SubjectAlternativeNames,Type:Type}"
6+
aws_run_commandline \
7+
"
8+
aws acm describe-certificate \
9+
--certificate-arn $item \
10+
--query \"*[].{CertificateArn:CertificateArn,DomainName:DomainName,SubjectAlternativeNames:SubjectAlternativeNames,Type:Type}\"
11+
"
712
done
813
}

services/alb.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
aws_alb_list() {
4+
aws_run_commandline "aws elbv2 describe-load-balancers"
5+
}
6+
7+
aws_alb_info() {
8+
9+
for alb_arn in $(aws elbv2 describe-load-balancers --query "*[].LoadBalancerArn" --output text); do
10+
aws_run_commandline "aws elbv2 describe-listeners --load-balancer-arn $alb_arn"
11+
12+
done
13+
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
# Setting for ecs
4+
aws_application_autoscaling_ecs() {
5+
aws application-autoscaling describe-scheduled-actions \
6+
--service-namespace ecs
7+
}

0 commit comments

Comments
 (0)