-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_runner.sh
55 lines (46 loc) · 1.37 KB
/
aws_runner.sh
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
#!/bin/bash
function get_cli_options() {
while getopts ":a:r:" opt; do
case ${opt} in
a )
ACTION=$OPTARG
;;
r )
ASSUME_ARN=$OPTARG
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
: )
echo "Invalid Option: -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
}
function list_s3() {
aws s3 ls
}
function main() {
# Get all cli inputs and validate variables
get_cli_options "$@"
echo "============================================"
echo "==== Get STS for role assumption ===="
echo "*** INFO: Getting AWS Token ***"
SESSIONNAME="AWS-S3-List-Pipeline-${BUILD_NUMBER}"
# Assume the AWS role and set credentials
eval $(aws sts assume-role \
--output json \
--role-arn "${ASSUME_ARN}" \
--role-session-name "${SESSIONNAME}" \
| jq -r '.Credentials | @sh "export AWS_SESSION_TOKEN=\(.SessionToken) \
export AWS_ACCESS_KEY_ID=\(.AccessKeyId) \
export AWS_SECRET_ACCESS_KEY=\(.SecretAccessKey)"')
# Verify the assumed role
aws sts get-caller-identity
# Execute S3 list
list_s3
}
# Execute main function with all script arguments
main "$@"