Skip to content

Commit 87f0f6c

Browse files
authored
Merge pull request #9 from lamhaison/develop
Release and refactor code
2 parents 757dbe8 + 3cadc37 commit 87f0f6c

File tree

13 files changed

+173
-38
lines changed

13 files changed

+173
-38
lines changed

.gitallowed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./aws_cli_results

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# aws-cli-utils
22
This is the project to collect helpful aws cli commandline with complex options to support your working
33
## Why I want to write this project.
4-
- Sometimes, aws cli is list, get, describes. It is pretty hard to remmebers.
4+
- Sometimes, aws cli is list, get, describes. It is pretty hard to remember.
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

common/peco.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ peco_commandline_input() {
4040

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

4545
local md5_hash=$(echo $commandline | md5)
46-
local input_folder="${aws_cli_input_tmp}/${ASSUME_ROLE:=NOTSET}"
46+
local input_folder="${aws_cli_input_tmp}/${ASSUME_ROLE:-NOTSET}"
4747
mkdir -p ${input_folder}
4848
local input_file_path="${input_folder}/${md5_hash}.txt"
4949
local empty_file=$(find ${input_folder} -name ${md5_hash}.txt -empty)
@@ -93,7 +93,7 @@ peco_aws_ecs_list_services() {
9393

9494
# AWS ECR
9595

96-
peco_aws_ecr_list_repositorie_names() {
96+
peco_aws_ecr_list_repo_names() {
9797
peco_aws_input 'aws ecr describe-repositories --query "*[].repositoryName"' 'true'
9898
}
9999

main.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if [[ -z "${AWS_CLI_SOURCE_SCRIPTS}" ]]; then
1414
DEFAULT_AWS_CLI_SOURCE_SCRIPTS='/opt/lamhaison-tools/aws-cli-utils'
1515
fi
1616

17-
export AWS_CLI_SOURCE_SCRIPTS="${LOCAL_AWS_CLI_SOURCE_SCRIPTS:=${DEFAULT_AWS_CLI_SOURCE_SCRIPTS}}"
17+
export AWS_CLI_SOURCE_SCRIPTS="${LOCAL_AWS_CLI_SOURCE_SCRIPTS:-${DEFAULT_AWS_CLI_SOURCE_SCRIPTS}}"
1818
else
1919
export AWS_CLI_SOURCE_SCRIPTS=${AWS_CLI_SOURCE_SCRIPTS}
2020
fi
@@ -67,7 +67,7 @@ if [ "true" = "${aws_cli_load_current_assume_role}" ] && [ -s "${aws_cli_current
6767
aws_assume_role_load_current_assume_role_for_new_tab
6868
fi
6969

70-
LHS_BIND_KEY=${2:='True'}
70+
LHS_BIND_KEY=${2:-'True'}
7171

7272
if [[ "${LHS_BIND_KEY}" = "True" ]]; then
7373
# Add hot-keys

services/assume_role.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ aws_assume_role_unzip_tmp_credential() {
5050
cd - >/dev/null
5151
}
5252

53-
aws_assume_role_remove_tmp_credential() {
53+
aws_assume_role_rm_tmp_credential() {
5454
assume_role_name_input=$1
55-
tmp_credentials_file_zip=${tmp_credentials}/${assume_role_name_input:?"aws_assume_role_remove_tmp_credential is unset or empty"}.zip
55+
tmp_credentials_file_zip=${tmp_credentials}/${assume_role_name_input:?"aws_assume_role_rm_tmp_credential is unset or empty"}.zip
5656
if [ -f "${tmp_credentials_file_zip}" ]; then
5757
rm -r ${tmp_credentials_file_zip}
5858
fi

services/cloudformation.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,19 @@
44
aws_cloudformation_list_stack_sets() {
55
aws_run_commandline "aws cloudformation list-stack-sets"
66
}
7+
8+
aws_cloudformation_get_stack_set() {
9+
aws_run_commandline "\
10+
aws cloudformation describe-stack-set \
11+
--stack-set-name ${1:?'stack_set_name is unset or empty'}
12+
13+
"
14+
}
15+
16+
aws_cloudformation_stack_info() {
17+
18+
local lhs_stack_set_names=$(aws cloudformation list-stack-sets --query '*[].StackSetName' --output text)
19+
for stack_set_name in ${lhs_stack_set_names}; do
20+
aws_cloudformation_get_stack_set ${stack_set_name}
21+
done
22+
}

services/cloudtrail.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
#
3+
# @version 1.0
4+
# @script cloudtrail.sh
5+
# @description TODO : List functions for cloudtrail
6+
#
7+
##
8+
9+
aws_cloudtrail_get_aws_config_set_recorder_event_instruction() {
10+
cat <<-__EOF__
11+
fields @timestamp, @message
12+
| sort @timestamp desc
13+
| limit 20
14+
| filter eventSource = 'config.amazonaws.com'
15+
| stats count() as count by eventName
16+
17+
fields @timestamp, @message
18+
| filter eventSource = 'config.amazonaws.com' and eventName = 'StartConfigurationRecorder'
19+
20+
__EOF__
21+
}
22+
23+
aws_cloudtrail_list_event_names() {
24+
local_aws_cloudtrail_list_event_name_peco_menu() {
25+
local aws_cloudtrail_event_name="https://gist.githubusercontent.com/lamhaison/8fe7937f147a6cb0ed446ef3ac17f972/raw/255a9435d2b71fdb900739355699b3ddffa414c2/cloudTrailEventNames.list"
26+
curl --silent -q --request GET ${aws_cloudtrail_event_name}
27+
}
28+
29+
local_aws_cloudtrail_list_event_name_peco_menu | peco
30+
}

services/ec2.sh

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/bin/bash
22

3+
local_aws_ec2_instance_id_peco_menu() {
4+
local aws_ec2_instance_id=$(peco_create_menu 'peco_aws_ec2_list')
5+
aws_ec2_instance_id=$(echo "${aws_ec2_instance_id}" | awk -F "_" '{print $1}')
6+
echo ${aws_ec2_instance_id}
7+
}
8+
39
# AWS ec2
410
# List all ec2 instance(don't care stopped or running instances)
511
aws_ec2_list_all() {
@@ -31,6 +37,10 @@ aws_ec2_get() {
3137
"
3238
}
3339

40+
aws_ec2_get_with_hint() {
41+
aws_ec2_get $(local_aws_ec2_instance_id_peco_menu)
42+
}
43+
3444
aws_ec2_reboot() {
3545
aws_run_commandline "\
3646
aws ec2 reboot-instances \
@@ -55,7 +65,7 @@ aws_ec2_start() {
5565
aws_ec2_rm_instruction() {
5666
aws_commandline_logging "\
5767
aws ec2 terminate-instances \
58-
--instance-ids ${1:="\$aws_ec2_instance_ids"}
68+
--instance-ids ${1:-"\$aws_ec2_instance_ids"}
5969
"
6070
}
6171

@@ -102,22 +112,19 @@ aws_ec2_connect() {
102112
}
103113

104114
aws_ec2_connect_with_hint() {
105-
aws_ec2_instance_id=$(peco_create_menu 'peco_aws_ec2_list')
106-
aws_ec2_instance_id=$(echo "${aws_ec2_instance_id}" | awk -F "_" '{print $1}')
107-
aws_ssm_connection_ec2 ${aws_ec2_instance_id}
115+
aws_ssm_connection_ec2 $(local_aws_ec2_instance_id_peco_menu)
108116
}
109117

110118
aws_ec2_list_eips() {
111119
aws_run_commandline 'aws ec2 describe-addresses'
112120
}
113121

114122
# VPC
115-
116123
aws_ec2_list_vpcs() {
117-
aws_run_commandline \
118-
"
119-
aws ec2 describe-vpcs --query '*[].{Id:VpcId,CidrBlock:CidrBlock,Name:Tags[?Key == \`Name\`] | [0].Value}' --output table
120-
"
124+
aws_run_commandline "\
125+
aws ec2 describe-vpcs \
126+
--query '*[].{Id:VpcId,CidrBlock:CidrBlock,Name:Tags[?Key == \`Name\`] | [0].Value}' --output table
127+
"
121128
}
122129

123130
aws_vpc_list() {
@@ -131,8 +138,7 @@ aws_subnet_list() {
131138
}
132139

133140
aws_ec2_list_subnets() {
134-
aws_run_commandline \
135-
"
141+
aws_run_commandline "\
136142
aws ec2 describe-subnets \
137143
--query '*[].{VpcId:VpcId,SubnetId:SubnetId,\
138144
AvailabilityZone:AvailabilityZone,Name:Tags[?Key==\`Name\`].Value | [0]}' --output table
@@ -161,7 +167,7 @@ aws_sg_add_rule_instruction() {
161167
echo "\
162168
# Allow access the ssh from a specific IP address
163169
aws ec2 authorize-security-group-ingress \
164-
--group-id ${aws_sg_id:="\$aws_sg_id"} \
170+
--group-id ${aws_sg_id:-"\$aws_sg_id"} \
165171
--protocol tcp --port 22 \
166172
--cidr $(dig +short myip.opendns.com @resolver1.opendns.com)/32
167173
"
@@ -175,3 +181,37 @@ aws_region_list() {
175181
--output table
176182
"
177183
}
184+
185+
aws_ec2_other_commandlines() {
186+
function local_aws_ec2_other_commandlines_menu() {
187+
cat <<-__EOF__
188+
aws_ec2_get \$(local_aws_ec2_instance_id_peco_menu) # Get ec2 instance with hint
189+
aws_ec2_create_image \$(local_aws_ec2_instance_id_peco_menu) # Create ami image from ec2 with hint
190+
aws_ec2_start \$(local_aws_ec2_instance_id_peco_menu) # Start ec2 instance with hint
191+
aws_ec2_stop \$(local_aws_ec2_instance_id_peco_menu) # Stop ec2 instance with hint
192+
aws_ec2_reboot \$(local_aws_ec2_instance_id_peco_menu) # Reboot ec2 instance with hint
193+
aws_ec2_rm_instruction \$(local_aws_ec2_instance_id_peco_menu) # Remove ec2 instance instruction(Don't apply)
194+
195+
__EOF__
196+
}
197+
198+
local aws_cmd=$(lhs_peco_create_menu 'local_aws_ec2_other_commandlines_menu' | awk -F "#" '{print $1}')
199+
200+
if [[ -n "${aws_cmd}" ]]; then
201+
aws_commandline_logging "${aws_cmd}"
202+
eval ${aws_cmd}
203+
else
204+
echo "Do nothing"
205+
fi
206+
207+
}
208+
209+
aws_ec2_get_credential_from_metadata_instruction() {
210+
local aws_meta_data_address="http://169.254.169.254"
211+
cat <<-__EOF__
212+
# Run on ec2
213+
iam_role_name=\$(curl -s '${aws_meta_data_address}/latest/meta-data/iam/security-credentials/')
214+
curl -s ${aws_meta_data_address}/latest/meta-data/iam/security-credentials/\${iam_role_name}
215+
__EOF__
216+
217+
}

services/ecr.sh

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
aws_ecr_list_repo() {
3+
aws_ecr_list_repos() {
44
aws_run_commandline "aws ecr describe-repositories --query \"*[].repositoryArn\""
55
}
66

@@ -9,23 +9,31 @@ aws_ecr_login() {
99
if [[ -z "${AWS_ACCOUNT_ID}" ]]; then
1010
aws_assume_role_get_aws_account_id
1111
fi
12-
aws ecr get-login-password --region ${AWS_REGION} | docker login \
13-
--username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
12+
13+
local aws_ecr_docker_login_password=$(aws ecr get-login-password --region ${AWS_REGION})
14+
15+
local aws_ecr_docker_login_cmd=$(
16+
echo "echo '${aws_ecr_docker_login_password}' | docker login \
17+
--username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
18+
)
19+
20+
aws_run_commandline "${aws_ecr_docker_login_cmd}"
21+
1422
}
1523

1624
aws_ecr_logout() {
17-
1825
if [[ -z "${AWS_ACCOUNT_ID}" ]]; then
1926
aws_assume_role_get_aws_account_id
2027
fi
21-
docker logout ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
28+
aws_run_commandline "docker logout ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
29+
2230
}
2331

2432
aws_ecr_list_images() {
2533
aws_ecr_repo_name=$1
2634
cat <<-_EOF_
2735
Get images from ecr repo ${aws_ecr_repo_name:?Repo is not set or empty}
28-
Pull image [ docker pull ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${aws_ecr_repo_name:?Repo is not set or empty}:${image_tag:=latest} ]
36+
Pull image [ docker pull ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${aws_ecr_repo_name:?Repo is not set or empty}:${image_tag:-latest} ]
2937
_EOF_
3038

3139
aws_run_commandline "aws ecr list-images --repository-name ${aws_ecr_repo_name} --query 'imageIds[].{imageTag:imageTag}'"
@@ -36,20 +44,20 @@ aws_ecr_get_latest_image() {
3644

3745
cat <<-_EOF_
3846
Get images from ecr repo ${aws_ecr_repo_name:?Repo is not set or empty}
39-
Pull image [ docker pull ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${aws_ecr_repo_name:?Repo is not set or empty}:${image_tag:=latest} ]
47+
Pull image [ docker pull ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${aws_ecr_repo_name:?Repo is not set or empty}:${image_tag:-latest} ]
4048
_EOF_
4149

4250
aws_run_commandline "aws ecr list-images --repository-name ${aws_ecr_repo_name} --query 'imageIds[0].{imageTag:imageTag}'"
4351
}
4452

4553
aws_ecr_get_latest_image_with_hint() {
4654
echo "Your repository name >"
47-
aws_ecr_get_latest_image $(peco_create_menu 'peco_aws_ecr_list_repositorie_names')
55+
aws_ecr_get_latest_image $(peco_create_menu 'peco_aws_ecr_list_repo_names')
4856
}
4957

5058
aws_ecr_list_images_with_hint() {
5159
echo "Your repository name >"
52-
aws_ecr_list_images $(peco_create_menu 'peco_aws_ecr_list_repositorie_names')
60+
aws_ecr_list_images $(peco_create_menu 'peco_aws_ecr_list_repo_names')
5361
}
5462

5563
aws_ecr_get_image() {
@@ -58,16 +66,16 @@ aws_ecr_get_image() {
5866

5967
cat <<-_EOF_
6068
Get images from ecr repo ${aws_ecr_repo_name:?Repo is not set or empty}
61-
Pull image [ docker pull ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${aws_ecr_repo_name:?Repo is not set or empty}:${aws_ecr_repo_image_tag:=latest} ]
69+
Pull image [ docker pull ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${aws_ecr_repo_name:?Repo is not set or empty}:${aws_ecr_repo_image_tag:-latest} ]
6270
_EOF_
6371

6472
docker pull \
65-
${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${aws_ecr_repo_name:?Repo is not set or empty}:${aws_ecr_repo_image_tag:=latest}
73+
${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${aws_ecr_repo_name:?Repo is not set or empty}:${aws_ecr_repo_image_tag:-latest}
6674
}
6775

6876
aws_ecr_pull_image_with_hint() {
6977
echo "Your repository name >"
70-
aws_ecr_repo_name=$(peco_create_menu 'peco_aws_ecr_list_repositorie_names')
78+
aws_ecr_repo_name=$(peco_create_menu 'peco_aws_ecr_list_repo_names')
7179
echo "Your image tag for ${aws_ecr_repo_name:?'aws_ecr_repo_name is unset or empty'} >"
7280
# aws_ecr_repo_image_tag=$(echo "$(peco_aws_ecr_list_images ${aws_ecr_repo_name})" | peco)
7381
aws_ecr_repo_image_tag=$(peco_create_menu "peco_aws_ecr_list_images ${aws_ecr_repo_name}")

services/elasticache.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ aws_elasticache_list() {
44
aws_run_commandline 'aws elasticache describe-cache-clusters'
55
}
66

7-
aws_elasticache_remove_redis_cluster_instruction() {
7+
aws_elasticache_rm_redis_cluster_instruction() {
88
aws_elasticache_redis_cluster_name=$1
99

1010
echo "\

0 commit comments

Comments
 (0)