-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplease
49 lines (39 loc) · 1.63 KB
/
please
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
#!/bin/bash
# For the simplest usage, just paste the following line into CloudShell:
# bash<(curl -s https://raw.githubusercontent.com/jg3/aws-access/main/please)
# CloudShell is at the very top of your AWS Console with an icon that looks like [>_]
thisAccount=`aws sts get-caller-identity --output text | cut -f 1`
defaultAccount=975050276957
read -p "Enable role access from AWS Account ID: [$defaultAccount] " fromAccount
thatAccount=${fromAccount:-${defaultAccount}}
MY_ARN="arn:aws:iam::${thatAccount}:root"
ROLE=SwitchRoleFrom${thatAccount}
TMPFILE="/tmp/$1_tmp.json"
cat <<EOF >$TMPFILE
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"AWS": "$MY_ARN"
},
"Action": "sts:AssumeRole"
}]
}
EOF
[ $? -eq 0 ] && echo -n "Role policy document created in /tmp. " || echo -n "Role policy document creation failed."
aws iam create-role --role-name $ROLE --assume-role-policy-document file://$TMPFILE > /dev/null
[ $? -eq 0 ] && echo -n " Role $ROLE created. " || echo -n "Role creation failed."
aws iam attach-role-policy --role-name $ROLE --policy-arn arn:aws:iam::aws:policy/AdministratorAccess
[ $? -eq 0 ] && echo -n " Policy attached." || echo -n " Policy attachment failed."
rm $TMPFILE
[ $? -eq 0 ] && echo -n " Cleaning $TMPFILE " || echo -n " Cleanup $TMPFILE failed."
if [ -f "$0" ]; then
#rm $0
[ $? -eq 0 ] && echo " Cleanup complete." || echo -n " Cleanup $0 failed."
else
echo " Cleanup complete."
fi
#read -n 1 -s -r -p " Press any key to continue,"
echo "Link to switch roles in console: "
echo " https://signin.aws.amazon.com/switchrole?roleName=${ROLE}&account=${thisAccount}"