Skip to content

Instantly share code, notes, and snippets.

@schlomo schlomo/aws-assume-role.sh
Last active May 29, 2018

Embed
What would you like to do?
Understanding AWS IAM Roles and useful scripts to assume a role. See http://blog.schlomo.schapiro.org/2017/06/understanding-iam-roles-in-amazon-aws.html for details.
#!/bin/bash -ue
die() { echo 1>&2 "ERROR: $*" ; exit 1 ; }
info() { echo 1>&2 "INFO: $*" ; }
test "${1:-}" || die "Usage: $0 <role-name | role ARN> [<role-name | role ARN> ...]"
while test "${1:-}" ; do
role="$1"
shift
if [[ "$role" != */* ]] ; then
role=arn:aws:iam::$(aws sts get-caller-identity --output text --query Account):role/$role || \
die "Could not get your AWS account ID"
fi
role_data=$(aws sts assume-role --role-arn $role --role-session-name $USER) || \
die "Could not assume role $role"
eval $(
jq -r ' "AWS_SECRET_ACCESS_KEY=" + .Credentials.SecretAccessKey +
"\nAWS_SESSION_TOKEN=" + .Credentials.SessionToken +
"\nAWS_ACCESS_KEY_ID=" + .Credentials.AccessKeyId
' <<<"$role_data"
) || \
die "Could not process json output:
$role_data"
info "Switched to role $role"
done
echo AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
echo AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN
echo AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
{
"PolicyDocument": {
"Statement": [
{
"Resource": [
"arn:aws:s3:::my-backups",
"arn:aws:s3:::my-backups/*"
],
"Action": [
"s3:GetObject",
"s3:ListBucket",
"s3:ListBucketVersions",
"s3:ListObjects",
"s3:GetBucketLocation",
"s3:PutObject"
],
"Effect": "Allow"
}
],
"Version": "2012-10-17"
},
"RoleName": "backup-role",
"PolicyName": "read-write-backupbucket"
}
{
"Role": {
"Arn": "arn:aws:iam::123456789:role/backup-role",
"CreateDate": "2017-05-18T13:47:44Z",
"RoleId": "ABCDEF0123456789ABCDEF",
"Path": "/",
"RoleName": "backup-role",
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::123456789:role/some-instances-role",
"arn:aws:iam::123456789:role/PowerUser"
]
}
}
],
"Version": "2012-10-17"
}
}
}
AWSTemplateFormatVersion: "2010-09-09"
Resources:
bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: my-backups
LifecycleConfiguration:
Rules:
- Id: DeleteEverythingInThreeMonths
Prefix: ''
Status: Enabled
ExpirationInDays: '180'
role:
Type: AWS::IAM::Role
Properties:
RoleName: backup-role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
AWS: arn:aws:iam::123456789:role/PowerUser
Action:
- sts:AssumeRole
Policies:
- PolicyName: read-write-backupbucket
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:ListBucket
- s3:ListBucketVersions
- s3:ListObjects
- s3:GetBucketLocation
- s3:PutObject
Resource:
- "arn:aws:s3:::my-backups"
- "arn:aws:s3:::my-backups/*"
Copyright 2017 Schlomo Schapiro / Zalando SE
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.